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