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