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>
4 # Used for generating Atom feeds for web-accessible mailing list archives.
5 package PublicInbox::Feed;
10 use PublicInbox::WwwAtomStream;
16 each_recent_blob($ctx, sub { push @oids, $_[0] });
17 return _no_thread() unless @oids;
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);
28 sub generate_thread_atom {
30 my $mid = $ctx->{mid};
31 my $res = $ctx->{srch}->get_thread($mid);
32 return _no_thread() unless $res->{total};
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);
47 sub generate_html_index {
49 # if the 'r' query parameter is given, it is a legacy permalink
50 # which we must continue supporting:
52 if ($qp && !$qp->{r} && $ctx->{srch}) {
53 return PublicInbox::View::index_topics($ctx);
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" ] ];
66 die "BUG: new_html is not used with search" if $ctx->{srch};
68 my (undef, $last) = each_recent_blob($ctx, sub {
69 my ($oid, $commit, $ts, $u, $subj) = @_;
70 $ctx->{first} ||= $commit;
74 return [404, ['Content-Type', 'text/plain'],
75 ["No messages, yet\n"] ];
77 $ctx->{-html_tip} = '<pre>';
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);
88 new_html_footer($ctx, $last);
95 [404, ['Content-Type', 'text/plain'], ["No feed found for thread\n"]];
99 my ($ctx, $last) = @_;
100 my $qp = delete $ctx->{qp} or return;
101 my $old_r = $qp->{r};
106 $next = qq!<a\nhref="?r=$last"\nrel=next>next</a>!;
109 $latest = qq! <a\nhref='./new.html'>latest</a>!;
111 "<hr><pre>page: $next$latest</pre>";
114 sub each_recent_blob {
116 my $ibx = $ctx->{-inbox};
117 my $max = $ibx->{feedmax};
118 my $v = $ibx->{version} || 1;
120 wantarray and die "each_recent_blob return ignored for v2";
122 die "BUG: unsupported inbox version: $v\n";
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});
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+)?/;
139 # revision ranges may be specified
141 my $r = $qp->{r} if $qp;
142 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
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",
154 my %deleted; # only an optimization at this point
157 my ($cur_commit, $first_commit, $last_commit);
160 while (defined(my $line = <$log>)) {
161 if ($line =~ /$addmsg/o) {
163 next if $deleted{$add}; # optimization-only
164 $cb->($add, $cur_commit, $ts, $u, $subj) and $nr++;
169 } elsif ($line =~ /$delmsg/o) {
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;
181 while (my $line = <$log>) {
182 if ($line =~ /^(${hex}{7,40})/o) {
190 ($first_commit, $last_commit);