Skip to content

Commit

Permalink
Use new permission API endpoints
Browse files Browse the repository at this point in the history
Replace queries send from WEB with calls to the new API
endpoints.
  • Loading branch information
mickeyn committed Jun 6, 2017
1 parent ed0930e commit f263d2a
Showing 1 changed file with 10 additions and 45 deletions.
55 changes: 10 additions & 45 deletions lib/MetaCPAN/Web/Model/API/Permission.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ MetaCPAN::Web::Model::Permission - Catalyst Model for 06perms

sub get {
my ( $self, $type, $name ) = @_;
return undef unless $name;

if ( $type eq 'module' ) {
my $module = $self->request( '/permission/' . $name )->recv;
Expand All @@ -30,57 +31,21 @@ sub get {
}

sub _get_author_modules {
my $self = shift;
my $name = shift;

my $search = {
query => {
bool => {
should => [
{ term => { owner => $name } },
{ term => { co_maintainers => $name } },
],
},
},
size => 5_000,
};

return $self->_search_perms($search);
my ( $self, $name ) = @_;

my $res = $self->request("/permission/by_author/$name")->recv;
return $res->{permissions};
}

sub _get_modules_in_distribution {
my $self = shift;
my $name = shift;
return undef unless $name;

# ugh. Can't see a better way to get a model.
my $model = MetaCPAN::Web::Model::API::Release->new(
api_secure => $self->{api_secure} );
my ( $self, $name ) = @_;

my $res = $self->request("/package/modules/$name")->recv;
my @modules = $res->{modules} ? @{ $res->{modules} } : undef;

return undef unless @modules;

my @perm_search
= map { +{ term => { module_name => $_ } } } @modules;

my $search = {
query => { bool => { should => \@perm_search } },
size => 1_000,
};

return $self->_search_perms($search);
}

sub _search_perms {
my $self = shift;
my $search = shift;
return undef unless keys %{$res};

my $perms_found = $self->request( '/permission/_search', $search )->recv;
my @perms = sort { $a->{module_name} cmp $b->{module_name} }
map { $_->{_source} } @{ $perms_found->{hits}->{hits} };
return @perms ? \@perms : undef;
my $perms_found = $self->request( '/permission/by_module',
{ module => $res->{modules} } )->recv;
return $perms_found->{permissions};
}

__PACKAGE__->meta->make_immutable;
Expand Down

0 comments on commit f263d2a

Please sign in to comment.