]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ManifestJsGz.pm
7fee78dd15417958afb5e37a262c848884ea71b5
[public-inbox.git] / lib / PublicInbox / ManifestJsGz.pm
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # generates manifest.js.gz for grokmirror(1)
5 package PublicInbox::ManifestJsGz;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::WwwListing);
9 use bytes (); # length
10 use PublicInbox::Config;
11 use IO::Compress::Gzip qw(gzip);
12 use HTTP::Date qw(time2str);
13
14 my $json = PublicInbox::Config::json();
15
16 sub url_filter {
17         my ($ctx) = @_;
18         # grokmirror uses relative paths, so it's domain-dependent
19         # SUPER calls PublicInbox::WwwListing::url_filter
20         $ctx->SUPER::url_filter('publicInbox.grokManifest', 'match=domain');
21 }
22
23 sub inject_entry ($$$;$) {
24         my ($ctx, $url_path, $ent, $git_dir) = @_;
25         $ctx->{-abs2urlpath}->{$git_dir // delete $ent->{git_dir}} = $url_path;
26         my $modified = $ent->{modified};
27         $ctx->{-mtime} = $modified if $modified > ($ctx->{-mtime} // 0);
28         $ctx->{manifest}->{$url_path} = $ent;
29 }
30
31 sub manifest_add ($$;$$) { # slow path w/o extindex "all"
32         my ($ctx, $ibx, $epoch, $default_desc) = @_;
33         my $url_path = "/$ibx->{name}";
34         my $git;
35         if (defined $epoch) {
36                 $url_path .= "/git/$epoch.git";
37                 $git = $ibx->git_epoch($epoch) or return;
38         } else {
39                 $git = $ibx->git;
40         }
41         my $ent = $git->manifest_entry($epoch, $default_desc) or return;
42         inject_entry($ctx, $url_path, $ent, $git->{git_dir});
43 }
44
45 sub slow_manifest_add ($$) {
46         my ($ctx, $ibx) = @_;
47         eval {
48                 if (defined(my $max = $ibx->max_git_epoch)) {
49                         my $desc = $ibx->description;
50                         for my $epoch (0..$max) {
51                                 manifest_add($ctx, $ibx, $epoch, $desc);
52                         }
53                 } else {
54                         manifest_add($ctx, $ibx);
55                 }
56         };
57 }
58
59 sub eidx_manifest_add ($$$) {
60         my ($ctx, $ALL, $ibx) = @_;
61         if (my $data = $ALL->misc->inbox_data($ibx)) {
62                 $data = $json->decode($data);
63                 delete $data->{''}; # private
64                 while (my ($url_path, $ent) = each %$data) {
65                         inject_entry($ctx, $url_path, $ent);
66                 }
67         } else {
68                 warn "E: `${\$ibx->eidx_key}' not indexed by $ALL->{topdir}\n";
69         }
70 }
71
72 sub response {
73         my ($class, $ctx) = @_;
74         bless $ctx, $class;
75         my ($re, undef) = $ctx->url_filter;
76         $re // return psgi_triple($ctx);
77         my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
78                                         $ctx->can('list_match_i'), $re, $ctx);
79         sub {
80                 $ctx->{-wcb} = $_[0]; # HTTP server callback
81                 $ctx->{env}->{'pi-httpd.async'} ?
82                                 $iter->event_step : $iter->each_section;
83         }
84 }
85
86 sub ibx_entry {
87         my ($ctx, $ibx) = @_;
88         my $ALL = $ctx->{www}->{pi_cfg}->ALL;
89         if ($ALL) { # FIXME: test this in t/
90                 eidx_manifest_add($ctx, $ALL, $ibx);
91         } else {
92                 slow_manifest_add($ctx, $ibx);
93                 warn "E: $@" if $@;
94         }
95 }
96
97 sub hide_key { 'manifest' } # for WwwListing->list_match_i
98
99 sub psgi_triple {
100         my ($ctx) = @_;
101         my $abs2urlpath = delete($ctx->{-abs2urlpath}) // {};
102         my $manifest = delete($ctx->{manifest}) // {};
103         while (my ($url_path, $repo) = each %$manifest) {
104                 defined(my $abs = $repo->{reference}) or next;
105                 $repo->{reference} = $abs2urlpath->{$abs};
106         }
107         $manifest = $json->encode($manifest);
108         gzip(\$manifest => \(my $out));
109         [ 200, [ qw(Content-Type application/gzip),
110                  'Last-Modified', time2str($ctx->{-mtime}),
111                  'Content-Length', bytes::length($out) ], [ $out ] ]
112 }
113
114 sub per_inbox {
115         my ($ctx) = @_;
116         # only one inbox, slow is probably OK
117         slow_manifest_add($ctx, $ctx->{ibx});
118         psgi_triple($ctx);
119 }
120
121 1;