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