]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Feed.pm
74d0bbde476f069642526d06329e4161762f9b65
[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 = recent_blobs($ctx);
16         return _no_thread() unless @$oids;
17
18         my $git = $ctx->{-inbox}->git;
19         PublicInbox::WwwAtomStream->response($ctx, 200, sub {
20                 while (my $oid = shift @$oids) {
21                         my $msg = $git->cat_file($oid) or next;
22                         return PublicInbox::MIME->new($msg);
23                 }
24         });
25 }
26
27 sub generate_thread_atom {
28         my ($ctx) = @_;
29         my $mid = $ctx->{mid};
30         my $res = $ctx->{srch}->get_thread($mid);
31         return _no_thread() unless $res->{total};
32
33         my $ibx = $ctx->{-inbox};
34         my $html_url = $ibx->base_url($ctx->{env});
35         $html_url .= PublicInbox::Hval->new_msgid($mid)->{href};
36         $ctx->{-html_url} = $html_url;
37         my $msgs = $res->{msgs};
38         PublicInbox::WwwAtomStream->response($ctx, 200, sub {
39                 while (my $msg = shift @$msgs) {
40                         $msg = $ibx->msg_by_smsg($msg) and
41                                 return PublicInbox::MIME->new($msg);
42                 }
43         });
44 }
45
46 sub generate_html_index {
47         my ($ctx) = @_;
48         # if the 'r' query parameter is given, it is a legacy permalink
49         # which we must continue supporting:
50         my $qp = $ctx->{qp};
51         if ($qp && !$qp->{r} && $ctx->{srch}) {
52                 return PublicInbox::View::index_topics($ctx);
53         }
54
55         my $env = $ctx->{env};
56         my $url = $ctx->{-inbox}->base_url($env) . 'new.html';
57         my $qs = $env->{QUERY_STRING};
58         $url .= "?$qs" if $qs ne '';
59         [302, [ 'Location', $url, 'Content-Type', 'text/plain'],
60                 [ "Redirecting to $url\n" ] ];
61 }
62
63 sub new_html {
64         my ($ctx) = @_;
65         my $oids = recent_blobs($ctx);
66         if (!@$oids) {
67                 return [404, ['Content-Type', 'text/plain'],
68                         ["No messages, yet\n"] ];
69         }
70         $ctx->{-html_tip} = '<pre>';
71         $ctx->{-upfx} = '';
72         $ctx->{-hr} = 1;
73         my $git = $ctx->{-inbox}->git;
74         PublicInbox::WwwStream->response($ctx, 200, sub {
75                 while (my $oid = shift @$oids) {
76                         my $msg = $git->cat_file($oid) or next;
77                         my $m = PublicInbox::MIME->new($msg);
78                         my $more = scalar @$oids;
79                         return PublicInbox::View::index_entry($m, $ctx, $more);
80                 }
81                 new_html_footer($ctx);
82         });
83 }
84
85 # private subs
86
87 sub _no_thread () {
88         [404, ['Content-Type', 'text/plain'], ["No feed found for thread\n"]];
89 }
90
91 sub new_html_footer {
92         my ($ctx) = @_;
93         my $qp = delete $ctx->{qp} or return;
94         my $latest = '';
95         my $next = delete $ctx->{next_page} || '';
96         if ($next) {
97                 $next = qq!<a\nhref="?$next"\nrel=next>next</a>!;
98         }
99         if (!$qp) {
100                 $latest = qq! <a\nhref='./new.html'>latest</a>!;
101                 $next ||= '    ';
102         }
103         "<hr><pre>page: $next$latest</pre>";
104 }
105
106 sub recent_blobs {
107         my ($ctx) = @_;
108         my $ibx = $ctx->{-inbox};
109         my $max = $ibx->{feedmax};
110         my $qp = $ctx->{qp};
111         my $v = $ibx->{version} || 1;
112         if ($v > 2) {
113                 die "BUG: unsupported inbox version: $v\n";
114         }
115         if (my $srch = $ibx->search) {
116                 my $o = $qp ? $qp->{o} : 0;
117                 $o += 0;
118                 $o = 0 if $o < 0;
119                 my $res = $srch->query('', { limit => $max, offset => $o });
120                 my $next = $o + $max;
121                 $ctx->{next_page} = "o=$next" if $res->{total} >= $next;
122                 return [ map { $_->{blob} } @{$res->{msgs}} ];
123         }
124
125         my $hex = '[a-f0-9]';
126         my $addmsg = qr!^:000000 100644 \S+ (\S+) A\t${hex}{2}/${hex}{38}$!;
127         my $delmsg = qr!^:100644 000000 (\S+) \S+ D\t(${hex}{2}/${hex}{38})$!;
128         my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
129
130         # revision ranges may be specified
131         my $range = 'HEAD';
132         my $r = $qp->{r} if $qp;
133         if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
134                 $range = $r;
135         }
136
137         # get recent messages
138         # we could use git log -z, but, we already know ssoma will not
139         # leave us with filenames with spaces in them..
140         my $log = $ibx->git->popen(qw/log
141                                 --no-notes --no-color --raw -r
142                                 --no-abbrev --abbrev-commit/,
143                                 "--format=%h", $range);
144         my %deleted; # only an optimization at this point
145         my $last;
146         my $last_commit;
147         local $/ = "\n";
148         my @oids;
149         while (defined(my $line = <$log>)) {
150                 if ($line =~ /$addmsg/o) {
151                         my $add = $1;
152                         next if $deleted{$add}; # optimization-only
153                         push @oids, $add;
154                         if (scalar(@oids) >= $max) {
155                                 $last = 1;
156                                 last;
157                         }
158                 } elsif ($line =~ /$delmsg/o) {
159                         $deleted{$1} = 1;
160                 }
161         }
162
163         if ($last) {
164                 local $/ = "\n";
165                 while (my $line = <$log>) {
166                         if ($line =~ /^(${hex}{7,40})/) {
167                                 $last_commit = $1;
168                                 last;
169                         }
170                 }
171         }
172
173         $ctx->{next_page} = "r=$last_commit" if $last_commit;
174         \@oids;
175 }
176
177 1;