Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1910 from metacpan/mickey/author_plussers
Replace query sending with API endpoint call
  • Loading branch information
mickeyn committed Jun 14, 2017
2 parents 3a5a702 + 7ef131e commit 299f20c
Showing 1 changed file with 31 additions and 44 deletions.
75 changes: 31 additions & 44 deletions lib/MetaCPAN/Web/Model/API/Favorite.pm
Expand Up @@ -131,43 +131,44 @@ sub find_plussers {
my @plusser_users = map { $_->{user} }
map { single_valued_arrayref_to_scalar( $_->{_source} ) }
@{ $plusser_data->{hits}->{hits} };
my $total_plussers = @plusser_users;

# find plussers by pause ids.
return Future->done( { hits => { hits => [] } }, $total_plussers )
unless @plusser_users;
$self->plusser_by_id( \@plusser_users )->transform(
done => sub {
return ( $_[0], $total_plussers );
$self->get_plusser_authors( \@plusser_users )->then(
sub {
my @plusser_authors = @{ +shift };
return Future->done(
{
plusser_authors => \@plusser_authors,
plusser_others =>
scalar( @plusser_users - @plusser_authors ),
plusser_data => $distribution
}
);
}
);
}
)->transform(
done => sub {
my ( $data, $total_plussers ) = @_;
my $authors = $data->{hits}{hits};
my @plusser_details = map {
{
id => $_->{_source}->{pauseid},
pic => $_->{_source}->{gravatar_url},
}
} @{$authors};
my $total_authors = @plusser_details;
);

# find total non pauseid users who have ++ed the dist.
my $total_nonauthors = ( $total_plussers - $total_authors );
# find plussers by pause ids.

# number of pauseid users can be more than total plussers
# then set 0 to non pauseid users
$total_nonauthors = 0 if $total_nonauthors < 0;
}

return (
{
plusser_authors => \@plusser_details,
plusser_others => $total_nonauthors,
plusser_data => $distribution
}
);
sub get_plusser_authors {
my ( $self, $users ) = @_;
return Future->done( [] ) unless $users and @{$users};

$self->request( '/author/by_user', undef, { user => $users } )
->transform(
done => sub {
my $res = shift;
return [] unless $res->{authors};

return [
map +{
id => $_->{pauseid},
pic => $_->{gravatar_url},
},
@{ $res->{authors} }
];
}
);
}
Expand All @@ -186,20 +187,6 @@ sub by_dist {
);
}

# finding the authors who have ++ed the distribution.
sub plusser_by_id {
my ( $self, $users ) = @_;
return $self->request(
'/author/_search',
{
query => { terms => { user => $users } },
_source => { includes => [qw(pauseid gravatar_url)] },
size => 1000,
sort => ['pauseid']
}
);
}

__PACKAGE__->meta->make_immutable;

1;

0 comments on commit 299f20c

Please sign in to comment.