Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replace query sending with API endpoint call
  • Loading branch information
mickeyn committed Jun 7, 2017
1 parent 4f92929 commit fd1fef1
Showing 1 changed file with 20 additions and 37 deletions.
57 changes: 20 additions & 37 deletions lib/MetaCPAN/Web/Model/API/Favorite.pm
Expand Up @@ -124,40 +124,37 @@ 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.
my $authors
= @plusser_users
? $self->plusser_by_id( \@plusser_users )->recv->{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 );

# number of pauseid users can be more than total plussers
# then set 0 to non pauseid users
$total_nonauthors = 0 if $total_nonauthors < 0;
my @plusser_authors = @{ $self->get_plusser_authors( \@plusser_users ) };

return (
{
plusser_authors => \@plusser_details,
plusser_others => $total_nonauthors,
plusser_authors => \@plusser_authors,
plusser_others => scalar( @plusser_users - @plusser_authors ),
plusser_data => $distribution
}
);

}

sub get_plusser_authors {
my ( $self, $users ) = @_;
return [] unless $users and @{$users};

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

return [
map +{
id => $_->{pauseid},
pic => $_->{gravatar_url},
},
@{ $res->{authors} }
];
}

# to search for v0/favorite/_search/{user} for the particular $distribution.
sub by_dist {
my ( $self, $distribution ) = @_;
Expand All @@ -172,20 +169,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 fd1fef1

Please sign in to comment.