Skip to content

Commit

Permalink
Merge pull request #731 from metacpan/mickey/more_api_refactoring
Browse files Browse the repository at this point in the history
more api refactoring
  • Loading branch information
oalders committed Nov 1, 2017
2 parents 27400c4 + d55664c commit c98928d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 99 deletions.
60 changes: 60 additions & 0 deletions lib/MetaCPAN/Document/File/Set.pm
Expand Up @@ -3,6 +3,7 @@ package MetaCPAN::Document::File::Set;
use Moose;

use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );
use Ref::Util qw( is_hashref );

extends 'ElasticSearchX::Model::Document::Set';

Expand Down Expand Up @@ -704,5 +705,64 @@ sub interesting_files {
};
}

sub find_changes_files {
my ( $self, $author, $release ) = @_;

# find the most likely file
# TODO: should we do this when the release is indexed
# and store the result as { 'changes_file' => $name }

my @candidates = qw(
CHANGES Changes ChangeLog Changelog CHANGELOG NEWS
);

# use $c->model b/c we can't let any filters apply here
my $file = $self->raw->filter(
{
and => [
{ term => { release => $release } },
{ term => { author => $author } },
{
or => [

# if it's a perl release, get perldelta
{
and => [
{ term => { distribution => 'perl' } },
{
term => {
'name' => 'perldelta.pod'
}
},
]
},

# otherwise look for one of these candidates in the root
{
and => [
{ term => { level => 0 } },
{ term => { directory => 0 } },
{
or => [
map { { term => { 'name' => $_ } } }
@candidates
]
}
]
}
],
}
]
}
)->size(1)

# HACK: Sort by level/desc to put pod/perldeta.pod first (if found)
# otherwise sort root files by name and select the first.
->sort( [ { level => 'desc' }, { name => 'asc' } ] )->first;

return unless is_hashref($file);
return $file->{_source};
}

__PACKAGE__->meta->make_immutable;
1;
28 changes: 28 additions & 0 deletions lib/MetaCPAN/Document/Release/Set.pm
Expand Up @@ -9,6 +9,34 @@ use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

extends 'ElasticSearchX::Model::Document::Set';

sub author_status {
my ( $self, $id, $file ) = @_;
return unless $id and $file;

my $status = $file->{_source}
|| single_valued_arrayref_to_scalar( $file->{fields} );

if ( $status and $status->{pauseid} ) {
$status->{release_count}
= $self->aggregate_status_by_author( $status->{pauseid} );

my ( $id_2, $id_1 ) = $id =~ /^((\w)\w)/;
$status->{links} = {
cpan_directory => "http://cpan.org/authors/id/$id_1/$id_2/$id",
backpan_directory =>
"https://cpan.metacpan.org/authors/id/$id_1/$id_2/$id",
cpants => "http://cpants.cpanauthors.org/author/$id",
cpantesters_reports =>
"http://cpantesters.org/author/$id_1/$id.html",
cpantesters_matrix => "http://matrix.cpantesters.org/?author=$id",
metacpan_explorer =>
"https://explorer.metacpan.org/?url=/author/$id",
};
}

return $status;
}

sub aggregate_status_by_author {
my ( $self, $pauseid ) = @_;
my $agg = $self->es->search(
Expand Down
30 changes: 2 additions & 28 deletions lib/MetaCPAN/Server/Controller/Author.pm
Expand Up @@ -28,37 +28,11 @@ __PACKAGE__->config(
# https://fastapi.metacpan.org/v1/author/LLAP
sub get : Path('') : Args(1) {
my ( $self, $c, $id ) = @_;

$c->add_author_key($id);
$c->cdn_max_age('1y');

my $file = $self->model($c)->raw->get($id);
if ( !defined $file ) {
$c->detach( '/not_found', ['Not found'] );
}
my $st = $file->{_source}
|| single_valued_arrayref_to_scalar( $file->{fields} );
if ( $st and $st->{pauseid} ) {
$st->{release_count}
= $c->model('CPAN::Release')
->aggregate_status_by_author( $st->{pauseid} );

my ( $id_2, $id_1 ) = $id =~ /^((\w)\w)/;
$st->{links} = {
cpan_directory => "http://cpan.org/authors/id/$id_1/$id_2/$id",
backpan_directory =>
"https://cpan.metacpan.org/authors/id/$id_1/$id_2/$id",
cpants => "http://cpants.cpanauthors.org/author/$id",
cpantesters_reports =>
"http://cpantesters.org/author/$id_1/$id.html",
cpantesters_matrix => "http://matrix.cpantesters.org/?author=$id",
metacpan_explorer =>
"https://explorer.metacpan.org/?url=/author/$id",
};
}
$c->stash($st)
|| $c->detach( '/not_found',
['The requested field(s) could not be found'] );
$c->stash_or_detach(
$c->model('CPAN::Release')->author_status( $id, $file ) );
}

# /author/search?q=QUERY
Expand Down
59 changes: 3 additions & 56 deletions lib/MetaCPAN/Server/Controller/Changes.pm
Expand Up @@ -26,62 +26,9 @@ sub get : Chained('index') : PathPart('') : Args(2) {
$c->add_author_key($author);
$c->cdn_max_age('1y');

# find the most likely file
# TODO: should we do this when the release is indexed
# and store the result as { 'changes_file' => $name }

my @candidates = qw(
CHANGES Changes ChangeLog Changelog CHANGELOG NEWS
);

my $file = eval {

# use $c->model b/c we can't let any filters apply here
my $files = $c->model('CPAN::File')->raw->filter(
{
and => [
{ term => { release => $release } },
{ term => { author => $author } },
{
or => [

# if it's a perl release, get perldelta
{
and => [
{ term => { distribution => 'perl' } },
{
term => {
'name' => 'perldelta.pod'
}
},
]
},

# otherwise look for one of these candidates in the root
{
and => [
{ term => { level => 0 } },
{ term => { directory => 0 } },
{
or => [
map {
{ term => { 'name' => $_ } }
} @candidates
]
}
]
}
],
}
]
}
)->size(1)

# HACK: Sort by level/desc to put pod/perldeta.pod first (if found)
# otherwise sort root files by name and select the first.
->sort( [ { level => 'desc' }, { name => 'asc' } ] )
->first->{_source};
} or $c->detach( '/not_found', [] );
my $file
= $c->model('CPAN::File')->find_changes_files( $author, $release );
$file or $c->detach( '/not_found', [] );

my $source = $c->model('Source')->path( @$file{qw(author release path)} )
or $c->detach( '/not_found', [] );
Expand Down
18 changes: 3 additions & 15 deletions lib/MetaCPAN/Server/Controller/Release.pm
Expand Up @@ -26,25 +26,13 @@ sub find : Path('') : Args(1) {
$c->stash($file);
}

# TODO: remove /release/by_author_and_name once merged and used by WEB
sub get : Path('') : Args(2) {
my ( $self, $c, $author, $name ) = @_;

$c->add_author_key($author);
$c->cdn_max_age('1y');

my $file = $self->model($c)->raw->get(
{
author => $author,
name => $name,
}
);
if ( !defined $file ) {
$c->detach( '/not_found', [] );
}
$c->stash( $file->{_source}
|| single_valued_arrayref_to_scalar( $file->{fields} ) )
|| $c->detach( '/not_found',
['The requested field(s) could not be found'] );
$c->stash_or_detach(
$self->model($c)->raw->by_author_and_name( $author, $name ) );
}

sub contributors : Path('contributors') : Args(2) {
Expand Down

0 comments on commit c98928d

Please sign in to comment.