]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Feed.pm
feed: $INBOX/new.atom endpoint supports v2 inboxes
[public-inbox.git] / lib / PublicInbox / Feed.pm
1 # Copyright (C) 2013-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Used for generating Atom feeds for web-accessible mailing list archives.
5 package PublicInbox::Feed;
6 use strict;
7 use warnings;
8 use PublicInbox::MIME;
9 use PublicInbox::View;
10 use PublicInbox::WwwAtomStream;
11
12 # main function
13 sub generate {
14         my ($ctx) = @_;
15         my @oids;
16         each_recent_blob($ctx, sub { push @oids, $_[0] });
17         return _no_thread() unless @oids;
18
19         my $git = $ctx->{-inbox}->git;
20         PublicInbox::WwwAtomStream->response($ctx, 200, sub {
21                 while (my $oid = shift @oids) {
22                         my $msg = $git->cat_file($oid) or next;
23                         return PublicInbox::MIME->new($msg);
24                 }
25         });
26 }
27
28 sub generate_thread_atom {
29         my ($ctx) = @_;
30         my $mid = $ctx->{mid};
31         my $res = $ctx->{srch}->get_thread($mid);
32         return _no_thread() unless $res->{total};
33
34         my $ibx = $ctx->{-inbox};
35         my $html_url = $ibx->base_url($ctx->{env});
36         $html_url .= PublicInbox::Hval->new_msgid($mid)->{href};
37         $ctx->{-html_url} = $html_url;
38         my $msgs = $res->{msgs};
39         PublicInbox::WwwAtomStream->response($ctx, 200, sub {
40                 while (my $msg = shift @$msgs) {
41                         $msg = $ibx->msg_by_smsg($msg) and
42                                 return PublicInbox::MIME->new($msg);
43                 }
44         });
45 }
46
47 sub generate_html_index {
48         my ($ctx) = @_;
49         # if the 'r' query parameter is given, it is a legacy permalink
50         # which we must continue supporting:
51         my $qp = $ctx->{qp};
52         if ($qp && !$qp->{r} && $ctx->{srch}) {
53                 return PublicInbox::View::index_topics($ctx);
54         }
55
56         my $env = $ctx->{env};
57         my $url = $ctx->{-inbox}->base_url($env) . 'new.html';
58         my $qs = $env->{QUERY_STRING};
59         $url .= "?$qs" if $qs ne '';
60         [302, [ 'Location', $url, 'Content-Type', 'text/plain'],
61                 [ "Redirecting to $url\n" ] ];
62 }
63
64 sub new_html {
65         my ($ctx) = @_;
66         die "BUG: new_html is not used with search" if $ctx->{srch};
67         my @oids;
68         my (undef, $last) = each_recent_blob($ctx, sub {
69                 my ($oid, $commit, $ts, $u, $subj) = @_;
70                 $ctx->{first} ||= $commit;
71                 push @oids, $oid;
72         });
73         if (!@oids) {
74                 return [404, ['Content-Type', 'text/plain'],
75                         ["No messages, yet\n"] ];
76         }
77         $ctx->{-html_tip} = '<pre>';
78         $ctx->{-upfx} = '';
79         $ctx->{-hr} = 1;
80         my $git = $ctx->{-inbox}->git;
81         PublicInbox::WwwStream->response($ctx, 200, sub {
82                 while (my $oid = shift @oids) {
83                         my $msg = $git->cat_file($oid) or next;
84                         my $m = PublicInbox::MIME->new($msg);
85                         my $more = scalar @oids;
86                         return PublicInbox::View::index_entry($m, $ctx, $more);
87                 }
88                 new_html_footer($ctx, $last);
89         });
90 }
91
92 # private subs
93
94 sub _no_thread () {
95         [404, ['Content-Type', 'text/plain'], ["No feed found for thread\n"]];
96 }
97
98 sub new_html_footer {
99         my ($ctx, $last) = @_;
100         my $qp = delete $ctx->{qp} or return;
101         my $old_r = $qp->{r};
102         my $latest = '';
103         my $next = '    ';
104
105         if ($last) {
106                 $next = qq!<a\nhref="?r=$last"\nrel=next>next</a>!;
107         }
108         if ($old_r) {
109                 $latest = qq! <a\nhref='./new.html'>latest</a>!;
110         }
111         "<hr><pre>page: $next$latest</pre>";
112 }
113
114 sub each_recent_blob {
115         my ($ctx, $cb) = @_;
116         my $ibx = $ctx->{-inbox};
117         my $max = $ibx->{feedmax};
118         my $v = $ibx->{version} || 1;
119         if ($v == 2) {
120                 wantarray and die "each_recent_blob return ignored for v2";
121         } elsif ($v != 1) {
122                 die "BUG: unsupported inbox version: $v\n";
123         }
124         if (my $srch = $ibx->search) {
125                 my $res = $srch->query('', { limit => $max });
126                 foreach my $smsg (@{$res->{msgs}}) {
127                         # search-enabled callers do not need author/date/subject
128                         $cb->($smsg->{blob});
129                 }
130                 return;
131         }
132
133         my $hex = '[a-f0-9]';
134         my $addmsg = qr!^:000000 100644 \S+ (\S+) A\t${hex}{2}/${hex}{38}$!;
135         my $delmsg = qr!^:100644 000000 (\S+) \S+ D\t(${hex}{2}/${hex}{38})$!;
136         my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
137         my $qp = $ctx->{qp};
138
139         # revision ranges may be specified
140         my $range = 'HEAD';
141         my $r = $qp->{r} if $qp;
142         if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
143                 $range = $r;
144         }
145
146         # get recent messages
147         # we could use git log -z, but, we already know ssoma will not
148         # leave us with filenames with spaces in them..
149         my $log = $ibx->git->popen(qw/log
150                                 --no-notes --no-color --raw -r
151                                 --no-abbrev --abbrev-commit/,
152                                 "--format=%h%x00%ct%x00%an%x00%s%x00",
153                                 $range);
154         my %deleted; # only an optimization at this point
155         my $last;
156         my $nr = 0;
157         my ($cur_commit, $first_commit, $last_commit);
158         my ($ts, $subj, $u);
159         local $/ = "\n";
160         while (defined(my $line = <$log>)) {
161                 if ($line =~ /$addmsg/o) {
162                         my $add = $1;
163                         next if $deleted{$add}; # optimization-only
164                         $cb->($add, $cur_commit, $ts, $u, $subj) and $nr++;
165                         if ($nr >= $max) {
166                                 $last = 1;
167                                 last;
168                         }
169                 } elsif ($line =~ /$delmsg/o) {
170                         $deleted{$1} = 1;
171                 } elsif ($line =~ /^${hex}{7,40}/o) {
172                         ($cur_commit, $ts, $u, $subj) = split("\0", $line);
173                         unless (defined $first_commit) {
174                                 $first_commit = $cur_commit;
175                         }
176                 }
177         }
178
179         if ($last) {
180                 local $/ = "\n";
181                 while (my $line = <$log>) {
182                         if ($line =~ /^(${hex}{7,40})/o) {
183                                 $last_commit = $1;
184                                 last;
185                         }
186                 }
187         }
188
189         # for pagination
190         ($first_commit, $last_commit);
191 }
192
193 1;