Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use new /release/latest_by_author endpoint
Replace Elasticsearch query with a use of the new API endpoint.
  • Loading branch information
mickeyn committed Jun 2, 2017
1 parent b11b6fe commit 61787e2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 40 deletions.
25 changes: 11 additions & 14 deletions lib/MetaCPAN/Web/Controller/Author.pm
Expand Up @@ -44,34 +44,31 @@ sub root : Chained('/') PathPart('author') CaptureArgs(1) {
sub index : Chained('root') PathPart('') Args(0) {
my ( $self, $c ) = @_;

my $id = $c->stash->{pauseid};
my $pauseid = $c->stash->{pauseid};

my $author_cv = $c->model('API::Author')->get($id);

my $releases_cv = $c->model('API::Release')->latest_by_author($id);

my ( $author, $data ) = ( $author_cv->recv, $releases_cv->recv );
my $author_cv = $c->model('API::Author')->get($pauseid);
my $author = $author_cv->recv;
$c->detach('/not_found') unless ( $author->{pauseid} );

my $took = $data->{took};
my $faves = $c->model('API::Favorite')->by_user( $author->{user} );

my $releases = [ map { $_->{fields} } @{ $data->{hits}->{hits} } ];
single_valued_arrayref_to_scalar($releases);
my $releases = $c->model('API::Release')->latest_by_author($pauseid);

my $date = List::Util::max
map { DateTime::Format::ISO8601->parse_datetime( $_->{date} ) }
@$releases;
@{ $releases->{releases} };
$c->res->last_modified($date) if $date;

my $faves = $c->model('API::Favorite')->by_user( $author->{user} );

my $took = $releases->{took};

$c->stash(
{
author => $author,
faves => $faves,
releases => $releases,
releases => $releases->{releases},
template => 'author.html',
took => $took,
total => $data->{hits}->{total},
total => $releases->{total},
}
);

Expand Down
10 changes: 3 additions & 7 deletions lib/MetaCPAN/Web/Controller/Feed.pm
Expand Up @@ -104,12 +104,6 @@ sub author : Local : Args(1) {
$c->add_author_key($author);

my $author_cv = $c->model('API::Author')->get($author);
my $releases_cv = $c->model('API::Release')->latest_by_author($author);

my $release_data = [
map { single_valued_arrayref_to_scalar($_) }
map { $_->{fields} } @{ $releases_cv->recv->{hits}{hits} }
];
my $author_info = $author_cv->recv;

# If the author can be found, we get the hashref of author info. If it
Expand All @@ -120,14 +114,16 @@ sub author : Local : Args(1) {
$c->detach( '/not_found', [] );
}

my $releases = $c->model('API::Release')->latest_by_author($author);

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_release_entries( $releases->{releases} ) },
@{ $self->_format_favorite_entries( $author, $faves ) }
],
);
Expand Down
23 changes: 4 additions & 19 deletions lib/MetaCPAN/Web/Model/API/Release.pm
Expand Up @@ -49,25 +49,10 @@ sub distribution {
}

sub latest_by_author {
my ( $self, $author ) = @_;
return $self->request(
'/release/_search',
{
query => {
bool => {
must => [
{ term => { author => uc($author) } },
{ term => { status => 'latest' } }
]
}
},
sort => [
'distribution', { 'version_numified' => { reverse => 1 } }
],
fields => [qw(author distribution name status abstract date)],
size => 1000,
}
);
my ( $self, $pauseid ) = @_;
my $data = $self->request("/release/latest_by_author/$pauseid")->recv;
return unless $data;
return +{ releases => $data->{releases}, took => $data->{took} };
}

sub all_by_author {
Expand Down

0 comments on commit 61787e2

Please sign in to comment.