Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' into mickey/sitemap_v1
  • Loading branch information
mickeyn committed Jun 2, 2017
2 parents e9eb804 + fc0ae58 commit f04cf0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
21 changes: 4 additions & 17 deletions lib/MetaCPAN/Web/Controller/Favorite.pm
Expand Up @@ -4,29 +4,16 @@ BEGIN { extends 'MetaCPAN::Web::Controller' }

sub recent : Local : Args(0) {
my ( $self, $c ) = @_;

my $page_size = $c->req->get_page_size(100);

my $data = $c->model('API::Favorite')->recent( $c->req->page, $page_size )
->recv;
my @faves = map { $_->{_source} } @{ $data->{hits}->{hits} };
my @user_ids = map { $_->{user} } @faves;

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

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

my $data
= $c->model('API::Favorite')->recent( $c->req->page, $page_size );
$c->stash(
{
header => 1,
recent => \@faves,
show_clicked_by => 1,
recent => $data->{favorites},
took => $data->{took},
total => $data->{hits}->{total},
total => $data->{total},
page_size => $page_size,
template => 'favorite/recent.html',
}
Expand Down
26 changes: 18 additions & 8 deletions lib/MetaCPAN/Web/Model/API/Favorite.pm
Expand Up @@ -86,15 +86,25 @@ sub by_user {

sub recent {
my ( $self, $page, $page_size ) = @_;
$self->request(
'/favorite/_search',
{
size => $page_size,
from => ( $page - 1 ) * $page_size,
query => { match_all => {} },
sort => [ { 'date' => { order => 'desc' } } ]
my $data = $self->request( '/favorite/recent',
{ size => $page_size, page => $page } )->recv;

my @user_ids = map { $_->{user} } @{ $data->{favorites} };
return $data unless @user_ids;

my $authors
= $self->request( '/author/by_user', undef, { user => \@user_ids } )
->recv;
if ( $authors and exists $authors->{authors} ) {
my %author_for_user_id
= map { $_->{user} => $_->{pauseid} } @{ $authors->{authors} };
for my $fav ( @{ $data->{favorites} } ) {
next unless exists $author_for_user_id{ $fav->{user} };
$fav->{clicked_by_author} = $author_for_user_id{ $fav->{user} };
}
);
}

return $data;
}

sub leaderboard {
Expand Down

0 comments on commit f04cf0e

Please sign in to comment.