Skip to content

Commit

Permalink
Use new /author/by_user API endpoint
Browse files Browse the repository at this point in the history
This will make use of the new API endpoint to make the WEB code
cleaner and less Elasticsearch-aware.
  • Loading branch information
mickeyn committed May 31, 2017
1 parent aab539f commit 15c9d0b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
13 changes: 4 additions & 9 deletions lib/MetaCPAN/Web/Controller/Favorite.pm
Expand Up @@ -12,17 +12,12 @@ sub recent : Local : Args(0) {
my @faves = map { $_->{_source} } @{ $data->{hits}->{hits} };
my @user_ids = map { $_->{user} } @faves;

my $authors
= $c->model('API::Author')->by_user( \@user_ids )->recv->{hits}
->{hits};

my %author_for_user_id
= map { $_->{fields}->{user} => $_->{fields}->{pauseid} } @{$authors};
my $authors = $c->model('API::Author')->by_user( \@user_ids );
my %author_for_user_id = map { $_->{user} => $_->{pauseid} } @{$authors};

foreach my $fave (@faves) {
if ( exists $author_for_user_id{ $fave->{user} } ) {
$fave->{clicked_by_author} = $author_for_user_id{ $fave->{user} };
}
next unless exists $author_for_user_id{ $fave->{user} };
$fave->{clicked_by_author} = $author_for_user_id{ $fave->{user} };
}

$c->stash(
Expand Down
25 changes: 16 additions & 9 deletions lib/MetaCPAN/Web/Model/API/Author.pm
Expand Up @@ -3,6 +3,8 @@ package MetaCPAN::Web::Model::API::Author;
use Moose;
use namespace::autoclean;

use Ref::Util qw( is_arrayref );

extends 'MetaCPAN::Web::Model::API';

=head1 NAME
Expand Down Expand Up @@ -94,15 +96,20 @@ sub search {

sub by_user {
my ( $self, $users ) = @_;

my $query = return $self->request(
'/author/_search',
{
query => { terms => { user => $users } },
fields => [qw(user pauseid)],
size => 100
}
);
return [] unless $users;

my $ret;
if ( is_arrayref($users) ) {
return unless @{$users};
$ret = $self->request( '/author/by_user', undef, { user => $users } );
}
else {
$ret = $self->request("/author/by_user/$users");
}
return unless $ret;

my $data = $ret->recv;
return ( exists $data->{authors} ? $data->{authors} : [] );
}

__PACKAGE__->meta->make_immutable;
Expand Down

0 comments on commit 15c9d0b

Please sign in to comment.