Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1895 from metacpan/mickey/use_new_endpoints_by_user
use new endpoints by_user
  • Loading branch information
oalders committed May 31, 2017
2 parents 557621f + 15c9d0b commit 1f94ec4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 70 deletions.
12 changes: 1 addition & 11 deletions lib/MetaCPAN/Web/Controller/Account/Favorite.pm
Expand Up @@ -80,20 +80,10 @@ sub _cache_key_for_user {

sub _add_fav_list_to_stash {
my ( $self, $c, $size ) = @_;

my $user = $c->user;

my $faves_cv = $c->model('API::Favorite')->by_user( $user->id, $size );
my $faves_data = $faves_cv->recv;
my $faves = [
sort { $b->{date} cmp $a->{date} }
map { $_->{fields} } @{ $faves_data->{hits}{hits} }
];

my $faves = $c->model('API::Favorite')->by_user( $user->id, $size );
$c->stash( { faves => $faves } );

return $user;

}

__PACKAGE__->meta->make_immutable;
Expand Down
22 changes: 1 addition & 21 deletions lib/MetaCPAN/Web/Controller/Author.pm
Expand Up @@ -54,27 +54,7 @@ sub index : Chained('root') PathPart('') Args(0) {
$c->detach('/not_found') unless ( $author->{pauseid} );

my $took = $data->{took};
my $faves = [];

if ( $author->{user} ) {
my $faves_data
= $c->model('API::Favorite')->by_user( $author->{user} )->recv;
$took += $faves_data->{took} || 0;

my @all_fav = map { $_->{fields}->{distribution} }
@{ $faves_data->{hits}->{hits} };
my $noLatest = $c->model('API::Release')->no_latest(@all_fav);
$took += $noLatest->{took} || 0;

$faves = [
map {
my $distro = $_->{fields}->{distribution};
$noLatest->{no_latest}->{$distro} ? () : $_->{fields};
} @{ $faves_data->{hits}->{hits} }
];
single_valued_arrayref_to_scalar($faves);
$faves = [ sort { $b->{date} cmp $a->{date} } @{$faves} ];
}
my $faves = $c->model('API::Favorite')->by_user( $author->{user} );

my $releases = [ map { $_->{fields} } @{ $data->{hits}->{hits} } ];
single_valued_arrayref_to_scalar($releases);
Expand Down
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
12 changes: 2 additions & 10 deletions lib/MetaCPAN/Web/Controller/Feed.pm
Expand Up @@ -120,23 +120,15 @@ sub author : Local : Args(1) {
$c->detach( '/not_found', [] );
}

my $faves_cv = $author_info->{user}
&& $c->model('API::Favorite')->by_user( $author_info->{user} );
my $faves_data
= $faves_cv
? [
map { single_valued_arrayref_to_scalar($_) }
map { $_->{fields} } @{ $faves_cv->recv->{hits}{hits} }
]
: [];
my $faves = $c->model('API::Favorite')->by_user( $author_info->{user} );

$c->stash->{feed} = $self->build_feed(
host => $c->config->{web_host},
title => "Recent CPAN activity of $author - MetaCPAN",
entries => [
sort { $b->{date} cmp $a->{date} }
@{ $self->_format_release_entries($release_data) },
@{ $self->_format_favorite_entries( $author, $faves_data ) }
@{ $self->_format_favorite_entries( $author, $faves ) }
],
);
}
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
15 changes: 5 additions & 10 deletions lib/MetaCPAN/Web/Model/API/Favorite.pm
Expand Up @@ -77,16 +77,11 @@ sub get {
sub by_user {
my ( $self, $user, $size ) = @_;
$size ||= 250;
return $self->request(
'/favorite/_search',
{
query => { match_all => {} },
filter => { term => { user => $user }, },
sort => ['distribution'],
fields => [qw(date author distribution)],
size => $size,
}
);
my $ret = $self->request( "/favorite/by_user/$user", { size => $size } );
return unless $ret;
my $data = $ret->recv;
return [] unless exists $data->{favorites};
return $data->{favorites};
}

sub recent {
Expand Down

0 comments on commit 1f94ec4

Please sign in to comment.