<commit_before>=pod
GADS - Globally Accessible Data Store
Copyright (C) 2014 Ctrl O Ltd

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
=cut

package GADS::Views;

use Log::Report 'linkspace';
use Moo;
use MooX::Types::MooseLike::Base qw/ArrayRef HashRef Int Maybe/;

# Whether the logged-in user has the layout permission
has user_has_layout => (
    is => 'lazy',
);

sub _build_user_has_layout
{   my $self = shift;
    $self->layout->user_can("layout");
}

# Whether to show another user's views
has other_user_id => (
    is  => 'ro',
    isa => Maybe[Int],
);

has schema => (
    is       => 'rw',
    required => 1,
);

has instance_id => (
    is       => 'ro',
    isa      => Int,
    required => 1,
);

has user_views => (
    is      => 'rw',
    lazy    => 1,
    builder => sub { $_[0]->_user_views },
);

has global => (
    is  => 'lazy',
    isa => ArrayRef,
);

has all => (
    is  => 'lazy',
    isa => ArrayRef,
);

# Only required a full view is retrieved
has layout => (
    is => 'rw',
);

sub _user_views
{   my $self = shift;
    # Allow user ID to be overridden, but only if the logged-in user has permission
    my $user_id = ($self->user_has_layout && $self->other_user_id) || ($self->layout->user && $self->layout->user->id);
    my $search = [
        'me.user_id' => $user_id,
        {
            global  => 1,
            -or     => [
                'me.group_id'         => undef,
                'user_groups.user_id' => $user_id,
            ],
        }
    ];
    push @$search, (is_admin => 1) if $self->user_has_layout;
    my @views = $self->schema->resultset('View')->search({
        -or         => $search,
        instance_id => $self->instance_id,
    },{
        join     => {
            group => 'user_groups',
        },
        order_by => ['me.global', 'me.is_admin', 'me.name'],
    })->all;
    \@views;
}

has views_limit_extra => (
    is => 'lazy',
);

sub _build_views_limit_extra
{   my $self = shift;
    return [] if !$self->layout->user_can('view_limit_extra');
    my @views = $self->schema->resultset('View')->search({
        is_limit_extra => 1,<commit_msg>Fix personal views being listed multiple times<commit_after>=pod
GADS - Globally Accessible Data Store
Copyright (C) 2014 Ctrl O Ltd

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
=cut

package GADS::Views;

use Log::Report 'linkspace';
use Moo;
use MooX::Types::MooseLike::Base qw/ArrayRef HashRef Int Maybe/;

# Whether the logged-in user has the layout permission
has user_has_layout => (
    is => 'lazy',
);

sub _build_user_has_layout
{   my $self = shift;
    $self->layout->user_can("layout");
}

# Whether to show another user's views
has other_user_id => (
    is  => 'ro',
    isa => Maybe[Int],
);

has schema => (
    is       => 'rw',
    required => 1,
);

has instance_id => (
    is       => 'ro',
    isa      => Int,
    required => 1,
);

has user_views => (
    is      => 'rw',
    lazy    => 1,
    builder => sub { $_[0]->_user_views },
);

has global => (
    is  => 'lazy',
    isa => ArrayRef,
);

has all => (
    is  => 'lazy',
    isa => ArrayRef,
);

# Only required a full view is retrieved
has layout => (
    is => 'rw',
);

sub _user_views
{   my $self = shift;
    # Allow user ID to be overridden, but only if the logged-in user has permission
    my $user_id = ($self->user_has_layout && $self->other_user_id) || ($self->layout->user && $self->layout->user->id);
    my $search = [
        'me.user_id' => $user_id,
        {
            global  => 1,
            -or     => [
                'me.group_id'         => undef,
                'user_groups.user_id' => $user_id,
            ],
        }
    ];
    push @$search, (is_admin => 1) if $self->user_has_layout;
    my @views = $self->schema->resultset('View')->search({
        -or         => $search,
        instance_id => $self->instance_id,
    },{
        join     => {
            group => 'user_groups',
        },
        order_by => ['me.global', 'me.is_admin', 'me.name'],
        collapse => 1,
    })->all;
    \@views;
}

has views_limit_extra => (
    is => 'lazy',
);

sub _build_views_limit_extra
{   my $self = shift;
    return [] if !$self->layout->user_can('view_limit_extra');
    my @views = $self->schema->resultset('View')->search({
        is_limit_extra => 1,
{'fraction_non_alphanumeric': 0.13772345859175483, 'fraction_numerical': 0.004560379423568041, 'mean_word_length': 2.5696614583333335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 124, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '574954', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1736, 'n_words': 565}