forked from metacpan/metacpan-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReleaseInfo.pm
114 lines (90 loc) · 3.24 KB
/
ReleaseInfo.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package MetaCPAN::Web::Role::ReleaseInfo;
use Moose::Role;
# TODO: are there other controllers that do (or should) include this?
# TODO: should some of this be in a separate (instantiable) model
# so you don't have to keep passing $data?
# then wouldn't have to pass favorites back in.
# Role/API/Aggregator?, Model/APIAggregator/ReleaseInfo?
# add favorites and myfavorite data into $main hash
sub add_favorites_data {
my ( $self, $main, $favorites, $data ) = @_;
$main->{myfavorite}
= $favorites->{myfavorites}->{ $data->{distribution} };
$main->{favorites} = $favorites->{favorites}->{ $data->{distribution} };
return;
}
# TODO: should the api_requests be in the base controller role,
# and then the default extras be defined in other roles?
# pass in any api request condvars and combine them with these defaults
sub api_requests {
my ( $self, $c, $reqs, $data ) = @_;
return {
author => $c->model('API::Author')->get( $data->{author} ),
favorites => $c->model('API::Favorite')->get(
$c->user_exists ? $c->user->id : undef,
$data->{distribution}
),
rating => $c->model('API::Rating')->get( $data->{distribution} ),
versions =>
$c->model('API::Release')->versions( $data->{distribution} ),
distribution =>
$c->model('API::Release')->distribution( $data->{distribution} ),
%$reqs,
};
}
# organize the api results into simple variables for the template
sub stash_api_results {
my ( $self, $c, $reqs, $data ) = @_;
$c->stash(
{
author => $reqs->{author},
#release => $release->{hits}->{hits}->[0]->{_source},
rating => $reqs->{rating}->{ratings}->{ $data->{distribution} },
distribution => $reqs->{distribution},
versions => [
map { $_->{fields} } @{ $reqs->{versions}->{hits}->{hits} }
],
}
);
}
# call recv() on all values in the provided hashref
sub recv_all {
my ( $self, $condvars ) = @_;
return { map { $_ => $condvars->{$_}->recv } keys %$condvars };
}
# massage the x_contributors field into what we want
sub groom_contributors {
my ( $self, $c, $release ) = @_;
my $contribs = $release->{metadata}{x_contributors} || [];
my $authors = $release->{metadata}{author} || [];
# just in case a lonely contributor makes it as a scalar
$contribs = [$contribs]
if !ref $contribs;
$authors = [$authors]
if !ref $authors;
my %seen = ( lc "$release->{author}\@cpan.org" => 1, );
my @contribs;
for my $contrib ( @$authors, @$contribs ) {
my $name = $contrib;
$name =~ s/\s*<([^<>]+@[^<>]+)>//;
my $info = {
name => $name,
$1 ? ( email => $1 ) : (),
};
next
if $seen{ $info->{email} }++;
# heuristic to autofill pause accounts
if ( !$info->{pauseid}
and $info->{email} =~ /^(.*)\@cpan\.org$/ )
{
$info->{pauseid} = uc $1;
}
if ( $info->{pauseid} ) {
$info->{url}
= $c->uri_for_action( '/author/index', [ $info->{pauseid} ] );
}
push @contribs, $info;
}
return \@contribs;
}
1;