]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwListing.pm
www: improve navigation around contemporary threads
[public-inbox.git] / lib / PublicInbox / WwwListing.pm
1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Provide an HTTP-accessible listing of inboxes.
5 # Used by PublicInbox::WWW
6 package PublicInbox::WwwListing;
7 use strict;
8 use warnings;
9 use PublicInbox::Hval qw(ascii_html prurl fmt_ts);
10 use PublicInbox::Linkify;
11 use PublicInbox::View;
12 use PublicInbox::Inbox;
13 use PublicInbox::GzipFilter qw(gzf_maybe);
14 use bytes (); # bytes::length
15 use HTTP::Date qw(time2str);
16 use Digest::SHA ();
17 use File::Spec ();
18 use IO::Compress::Gzip qw(gzip);
19 *try_cat = \&PublicInbox::Inbox::try_cat;
20 our $json;
21 for my $mod (qw(JSON::MaybeXS JSON JSON::PP)) {
22         eval "require $mod" or next;
23         # ->ascii encodes non-ASCII to "\uXXXX"
24         $json = $mod->new->ascii(1) and last;
25 }
26
27 sub list_all_i {
28         my ($ibx, $arg) = @_;
29         my ($list, $hide_key) = @$arg;
30         push @$list, $ibx unless $ibx->{-hide}->{$hide_key};
31 }
32
33 sub list_all ($$$) {
34         my ($self, $env, $hide_key) = @_;
35         my $list = [];
36         $self->{pi_config}->each_inbox(\&list_all_i, [ $list, $hide_key ]);
37         $list;
38 }
39
40 sub list_match_domain_i {
41         my ($ibx, $arg) = @_;
42         my ($list, $hide_key, $re) = @$arg;
43         if (!$ibx->{-hide}->{$hide_key} && grep(/$re/, @{$ibx->{url}})) {
44                 push @$list, $ibx;
45         }
46 }
47
48 sub list_match_domain ($$$) {
49         my ($self, $env, $hide_key) = @_;
50         my $list = [];
51         my $host = $env->{HTTP_HOST} // $env->{SERVER_NAME};
52         $host =~ s/:[0-9]+\z//;
53         my $arg = [ $list, $hide_key,
54                 qr!\A(?:https?:)?//\Q$host\E(?::[0-9]+)?/!i ];
55         $self->{pi_config}->each_inbox(\&list_match_domain_i, $arg);
56         $list;
57 }
58
59 sub list_404 ($$) { [] }
60
61 # TODO: +cgit
62 my %VALID = (
63         all => *list_all,
64         'match=domain' => *list_match_domain,
65         404 => *list_404,
66 );
67
68 sub set_cb ($$$) {
69         my ($pi_config, $k, $default) = @_;
70         my $v = $pi_config->{lc $k} // $default;
71         $VALID{$v} || do {
72                 warn <<"";
73 `$v' is not a valid value for `$k'
74 $k be one of `all', `match=domain', or `404'
75
76                 $VALID{$default};
77         };
78 }
79
80 sub new {
81         my ($class, $www) = @_;
82         my $pi_config = $www->{pi_config};
83         bless {
84                 pi_config => $pi_config,
85                 style => $www->style("\0"),
86                 www_cb => set_cb($pi_config, 'publicInbox.wwwListing', 404),
87                 manifest_cb => set_cb($pi_config, 'publicInbox.grokManifest',
88                                         'match=domain'),
89         }, $class;
90 }
91
92 sub ibx_entry {
93         my ($mtime, $ibx, $env) = @_;
94         my $ts = fmt_ts($mtime);
95         my $url = prurl($env, $ibx->{url});
96         my $tmp = <<"";
97 * $ts - $url
98   ${\$ibx->description}
99
100         if (defined(my $info_url = $ibx->{infourl})) {
101                 $tmp .= '  ' . prurl($env, $info_url) . "\n";
102         }
103         $tmp;
104 }
105
106 sub html ($$) {
107         my ($env, $list) = @_;
108         my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
109                         'Content-Length', undef ];
110         my $gzf = gzf_maybe($h, $env);
111         $gzf->zmore('<html><head><title>' .
112                                 'public-inbox listing</title>' .
113                                 '</head><body><pre>');
114         my $code = 404;
115         if (@$list) {
116                 $code = 200;
117                 # Schwartzian transform since Inbox->modified is expensive
118                 @$list = sort {
119                         $b->[0] <=> $a->[0]
120                 } map { [ $_->modified, $_ ] } @$list;
121
122                 my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
123                 my $l = PublicInbox::Linkify->new;
124                 $gzf->zmore($l->to_html($tmp));
125         } else {
126                 $gzf->zmore('no inboxes, yet');
127         }
128         my $out = $gzf->zflush('</pre><hr><pre>'.
129                                 PublicInbox::WwwStream::code_footer($env) .
130                                 '</pre></body></html>');
131         $h->[3] = bytes::length($out);
132         [ $code, $h, [ $out ] ];
133 }
134
135 sub fingerprint ($) {
136         my ($git) = @_;
137         # TODO: convert to qspawn for fairness when there's
138         # thousands of repos
139         my ($fh, $pid) = $git->popen('show-ref');
140         my $dig = Digest::SHA->new(1);
141         while (read($fh, my $buf, 65536)) {
142                 $dig->add($buf);
143         }
144         close $fh;
145         waitpid($pid, 0);
146         return if $?; # empty, uninitialized git repo
147         $dig->hexdigest;
148 }
149
150 sub manifest_add ($$;$$) {
151         my ($manifest, $ibx, $epoch, $default_desc) = @_;
152         my $url_path = "/$ibx->{name}";
153         my $git_dir = $ibx->{inboxdir};
154         if (defined $epoch) {
155                 $git_dir .= "/git/$epoch.git";
156                 $url_path .= "/git/$epoch.git";
157         }
158         return unless -d $git_dir;
159         my $git = PublicInbox::Git->new($git_dir);
160         my $fingerprint = fingerprint($git) or return; # no empty repos
161
162         chomp(my $owner = $git->qx('config', 'gitweb.owner'));
163         chomp(my $desc = try_cat("$git_dir/description"));
164         utf8::decode($owner);
165         utf8::decode($desc);
166         $owner = undef if $owner eq '';
167         $desc = 'Unnamed repository' if $desc eq '';
168
169         # templates/hooks--update.sample and git-multimail in git.git
170         # only match "Unnamed repository", not the full contents of
171         # templates/this--description in git.git
172         if ($desc =~ /\AUnnamed repository/) {
173                 $desc = "$default_desc [epoch $epoch]" if defined($epoch);
174         }
175
176         my $reference;
177         chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
178         if ($alt) {
179                 # n.b.: GitPython doesn't seem to handle comments or C-quoted
180                 # strings like native git does; and we don't for now, either.
181                 my @alt = split(/\n+/, $alt);
182
183                 # grokmirror only supports 1 alternate for "reference",
184                 if (scalar(@alt) == 1) {
185                         my $objdir = "$git_dir/objects";
186                         $reference = File::Spec->rel2abs($alt[0], $objdir);
187                         $reference =~ s!/[^/]+/?\z!!; # basename
188                 }
189         }
190         $manifest->{-abs2urlpath}->{$git_dir} = $url_path;
191         my $modified = $git->modified;
192         if ($modified > $manifest->{-mtime}) {
193                 $manifest->{-mtime} = $modified;
194         }
195         $manifest->{$url_path} = {
196                 owner => $owner,
197                 reference => $reference,
198                 description => $desc,
199                 modified => $modified,
200                 fingerprint => $fingerprint,
201         };
202 }
203
204 # manifest.js.gz
205 sub js ($$) {
206         my ($env, $list) = @_;
207         # $json won't be defined if IO::Compress::Gzip is missing
208         $json or return [ 404, [], [] ];
209
210         my $manifest = { -abs2urlpath => {}, -mtime => 0 };
211         for my $ibx (@$list) {
212                 if (defined(my $max = $ibx->max_git_epoch)) {
213                         my $desc = $ibx->description;
214                         for my $epoch (0..$max) {
215                                 manifest_add($manifest, $ibx, $epoch, $desc);
216                         }
217                 } else {
218                         manifest_add($manifest, $ibx);
219                 }
220         }
221         my $abs2urlpath = delete $manifest->{-abs2urlpath};
222         my $mtime = delete $manifest->{-mtime};
223         while (my ($url_path, $repo) = each %$manifest) {
224                 defined(my $abs = $repo->{reference}) or next;
225                 $repo->{reference} = $abs2urlpath->{$abs};
226         }
227         my $out;
228         gzip(\($json->encode($manifest)) => \$out);
229         $manifest = undef;
230         [ 200, [ qw(Content-Type application/gzip),
231                  'Last-Modified', time2str($mtime),
232                  'Content-Length', bytes::length($out) ], [ $out ] ];
233 }
234
235 # not really a stand-alone PSGI app, but maybe it could be...
236 sub call {
237         my ($self, $env) = @_;
238
239         if ($env->{PATH_INFO} eq '/manifest.js.gz') {
240                 # grokmirror uses relative paths, so it's domain-dependent
241                 my $list = $self->{manifest_cb}->($self, $env, 'manifest');
242                 js($env, $list);
243         } else { # /
244                 my $list = $self->{www_cb}->($self, $env, 'www');
245                 html($env, $list);
246         }
247 }
248
249 1;