]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/View.pm
view: reduce visual noise in conversation view
[public-inbox.git] / lib / PublicInbox / View.pm
index 17d6de56a18b5b8ccf63b5ecd14a79de513b4f1a..7be72cfc642d19995f6d78cd29ac57700b07bc7f 100644 (file)
@@ -24,14 +24,13 @@ sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
 
 # public functions: (unstable)
 sub msg_html {
-       my ($ctx, $mime, $footer) = @_;
+       my ($ctx, $mime) = @_;
        my $hdr = $mime->header_obj;
        my $tip = _msg_html_prepare($hdr, $ctx);
-       PublicInbox::WwwStream->new($ctx, sub {
+       PublicInbox::WwwStream->response($ctx, 200, sub {
                my ($nr, undef) = @_;
                if ($nr == 1) {
-                       $tip . multipart_text_as_html($mime, '') .
-                               '</pre><hr />'
+                       $tip . multipart_text_as_html($mime, '') . '</pre><hr>'
                } elsif ($nr == 2) {
                        # fake an EOF if generating the footer fails;
                        # we want to at least show the message if something
@@ -49,25 +48,40 @@ sub msg_html {
 # /$INBOX/$MESSAGE_ID/#R
 sub msg_reply {
        my ($ctx, $hdr) = @_;
-       my $se_url = 'https://git-htmldocs.bogomips.org/git-send-email.html';
+       my $se_url =
+        'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
+       my $p_url =
+        'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
 
        my ($arg, $link) = mailto_arg_link($hdr);
        push @$arg, '/path/to/YOUR_REPLY';
+       $arg = join(" \\\n    ", '', @$arg);
+       <<EOF
+<pre
+id=R>You may reply publically to <a
+href=#t>this message</a> via
+plain-text email using any one of the following methods:
 
-       "<pre\nid=R>".
-       "You may reply publically to <a\nhref=#t>this message</a> via\n".
-       "plain-text email using any one of the following methods:\n\n" .
-       "* Save the following mbox file, import it into your mail client,\n" .
-       "  and reply-to-all from there: <a\nhref=raw>mbox</a>\n\n" .
-       "* Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,\n" .
-       "  and <b>--in-reply-to</b> switches of git-send-email(1):\n\n" .
-       "  git send-email \\\n    " .
-       join(" \\\n    ", @$arg ). "\n\n" .
-       qq(  <a\nhref="$se_url">$se_url</a>\n\n) .
-       "* If your mail client supports setting the <b>In-Reply-To</b>" .
-       " header\n  via mailto: links, try the " .
-       qq(<a\nhref="$link">mailto: link</a>\n) .
-       '</pre>';
+* Save the following mbox file, import it into your mail client,
+  and reply-to-all from there: <a
+href=raw>mbox</a>
+
+  Avoid top-posting and favor interleaved quoting:
+  <a
+href="$p_url">$p_url</a>
+
+* Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,
+  and <b>--in-reply-to</b> switches of git-send-email(1):
+
+  git send-email$arg
+
+  <a
+href="$se_url">$se_url</a>
+
+* If your mail client supports setting the <b>In-Reply-To</b> header
+  via mailto: links, try the <a
+href="$link">mailto: link</a></pre>
+EOF
 }
 
 sub in_reply_to {
@@ -103,45 +117,49 @@ sub index_entry {
        my $subj = $hdr->header('Subject');
 
        my $mid_raw = mid_clean(mid_mime($mime));
-       my $id = id_compress($mid_raw);
+       my $id = id_compress($mid_raw, 1);
        my $id_m = 'm'.$id;
        my $mid = PublicInbox::Hval->new_msgid($mid_raw);
 
        my $root_anchor = $ctx->{root_anchor} || '';
        my $irt = in_reply_to($hdr);
 
-       my $rv = '<b>'.ascii_html($subj).'</b>';
-       $rv = "<u\nid=u>$rv</u>" if $root_anchor eq $id_m;
-       $rv .= "\n";
+       my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
+       $subj = '<b>'.ascii_html($subj).'</b>';
+       $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
+       $rv .= $subj . "\n";
        $rv .= _th_index_lite($mid_raw, $irt, $id, $ctx);
        my @tocc;
        foreach my $f (qw(To Cc)) {
                my $dst = _hdr_names($hdr, $f);
                push @tocc, "$f: $dst" if $dst ne '';
        }
-       my $mapping = $ctx->{mapping};
        $rv .= "From: "._hdr_names($hdr, 'From').' @ '._msg_date($hdr)." UTC";
        my $upfx = $ctx->{-upfx};
-       $rv .= qq{ (<a\nhref="$upfx$mid_raw/">permalink</a> / };
-       $rv .= qq{<a\nhref="$upfx$mid_raw/raw">raw</a>)\n};
+       my $mhref = $upfx . $mid->as_href . '/';
+       $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
+       $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
        $rv .= '  '.join('; +', @tocc) . "\n" if @tocc;
+
+       my $mapping = $ctx->{mapping};
        if (!$mapping && $irt) {
-               $rv .= qq(In-Reply-To: &lt;<a\nhref="$upfx$irt/">$irt</a>&gt;\n)
+               my $mirt = PublicInbox::Hval->new_msgid($irt);
+               my $href = $upfx . $mirt->as_href . '/';
+               my $html = $mirt->as_html;
+               $rv .= qq(In-Reply-To: &lt;<a\nhref="$href">$html</a>&gt;\n)
        }
        $rv .= "\n";
 
        # scan through all parts, looking for displayable text
-       my $href = $mid->as_href;
-       my $mhref = $ctx->{-upfx}.$href.'/';
        msg_iter($mime, sub { $rv .= add_text_body($mhref, $_[0]) });
 
        # add the footer
        $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
                "<a\nhref=\"$mhref\">permalink</a>" .
-               " <a\nhref=\"${mhref}raw\">raw</a>" .
-               " <a\nhref=\"${mhref}#R\">reply</a>";
+               " <a\nhref=\"${mhref}raw\">raw</a>" .
+               " <a\nhref=\"${mhref}#R\">reply</a>";
        if (my $pct = $ctx->{pct}) { # used by SearchView.pm
-               $rv .= " [relevance $pct->{$mid_raw}%]";
+               $rv .= "\t[relevance $pct->{$mid_raw}%]";
        } elsif ($mapping) {
                my $threaded = 'threaded';
                my $flat = 'flat';
@@ -151,8 +169,9 @@ sub index_entry {
                } else {
                        $threaded = "<b>$threaded</b>";
                }
-               $rv .= " [<a\nhref=\"${mhref}t/#u\">$threaded</a>";
-               $rv .= "|<a\nhref=\"${mhref}T/#u\">$flat</a>]";
+               $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
+               $rv .= "|<a\nhref=\"${mhref}t/#u\">$threaded</a>]";
+               $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
        }
 
        $rv .= $more ? "\n\n" : "\n";
@@ -199,6 +218,7 @@ sub _th_index_lite {
        my $this = $map->[1];
        $this =~ s!\n\z!</b>\n!s;
        $this =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
+       $this =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
        $rv .= "<b>@ $this";
        my $node = $map->[2];
        if (my $child = $node->child) {
@@ -222,7 +242,7 @@ sub _th_index_lite {
                        $rv .= $pad . $mapping->{$nn->messageid}->[1];
                }
        }
-       $rv .= "<a\nhref=#e$id\nid=m$id>_</a> <a\nhref=#r$id>$s_s, $s_c</a>\n";
+       $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
 }
 
 sub walk_thread {
@@ -272,7 +292,7 @@ sub stream_thread ($$) {
        $mime = Email::MIME->new($mime);
        $ctx->{-title_html} = ascii_html($mime->header('Subject'));
        $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
-       my $body = PublicInbox::WwwStream->new($ctx, sub {
+       PublicInbox::WwwStream->response($ctx, 200, sub {
                return unless $ctx;
                while (@q) {
                        $level = shift @q;
@@ -291,7 +311,6 @@ sub stream_thread ($$) {
                $ctx = undef;
                $ret;
        });
-       [ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
 }
 
 sub thread_html {
@@ -301,7 +320,7 @@ sub thread_html {
        my $msgs = load_results($sres);
        my $nr = $sres->{total};
        return missing_thread($ctx) if $nr == 0;
-       my $skel = '<hr /><pre>';
+       my $skel = '<hr><pre>';
        $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
        $skel .= ", back to <a\nhref=\"../../\">index</a>";
        $skel .= "\n<a\nid=t>$nr+ messages in thread:</a> (download: ";
@@ -315,6 +334,7 @@ sub thread_html {
        $ctx->{root_anchor} = anchor_for($mid);
        $ctx->{seen} = {};
        $ctx->{mapping} = {};
+       $ctx->{s_nr} = "$nr+ messages in thread";
 
        my $th = thread_results($msgs);
        walk_thread($th, $ctx, *pre_thread);
@@ -332,7 +352,7 @@ sub thread_html {
        $ctx->{-title_html} = ascii_html($mime->header('Subject'));
        $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
        $mime = undef;
-       my $body = PublicInbox::WwwStream->new($ctx, sub {
+       PublicInbox::WwwStream->response($ctx, 200, sub {
                return unless $msgs;
                while ($mime = shift @$msgs) {
                        $mid = mid_clean(mid_mime($mime));
@@ -345,7 +365,6 @@ sub thread_html {
                $msgs = undef;
                '</pre>'.$skel;
        });
-       [ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
 }
 
 sub multipart_text_as_html {
@@ -495,7 +514,7 @@ sub thread_skel {
        my $mid = mid_clean($hdr->header_raw('Message-ID'));
        my $sres = $srch->get_thread($mid);
        my $nr = $sres->{total};
-       my $expand = qq(<a\nhref="${tpfx}t/#u">expand</a> ) .
+       my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
                        qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
                        qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
 
@@ -531,7 +550,7 @@ sub _parent_headers {
 
        my $irt = in_reply_to($hdr);
        if (defined $irt) {
-               my $v = PublicInbox::Hval->new_msgid($irt, 1);
+               my $v = PublicInbox::Hval->new_msgid($irt);
                my $html = $v->as_html;
                my $href = $v->as_href;
                $rv .= "In-Reply-To: &lt;";
@@ -641,7 +660,7 @@ sub html_footer {
 }
 
 sub linkify_ref_nosrch {
-       my $v = PublicInbox::Hval->new_msgid($_[0], 1);
+       my $v = PublicInbox::Hval->new_msgid($_[0]);
        my $html = $v->as_html;
        my $href = $v->as_href;
        "&lt;<a\nhref=\"../$href/\">$html</a>&gt;";
@@ -834,13 +853,15 @@ sub add_topic {
        }
 }
 
-sub emit_topics {
+sub topics {
        my ($ctx) = @_;
        my $order = $ctx->{order};
        my $subjs = $ctx->{subjs};
        my $latest = $ctx->{latest};
-       my $fh = $ctx->{fh};
-       return $fh->write("\n[No topics in range]</pre>") unless scalar @$order;
+       if (!@$order) {
+               $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
+               return 404;
+       }
        my $pfx;
        my $prev = 0;
        my $prev_attr = '';
@@ -850,7 +871,7 @@ sub emit_topics {
                my ($level, $subj) = @$info;
                my $n = delete $subjs->{$subj};
                my ($mid, $ts) = @{delete $latest->{$subj}};
-               $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
+               my $href = PublicInbox::Hval->new_msgid($mid)->as_href;
                $pfx = indent_for($level);
                my $nl = $level == $prev ? "\n" : '';
                if ($nl && $cur) {
@@ -866,7 +887,7 @@ sub emit_topics {
                }
 
                $subj = PublicInbox::Hval->new($subj)->as_html;
-               $cur->[1] .= "<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";
+               $cur->[1] .= "<a\nhref=\"$href/T/#u\"><b>$subj</b></a>\n";
                $ts = fmt_ts($ts);
                my $attr = " $ts UTC";
 
@@ -875,8 +896,8 @@ sub emit_topics {
                $n = $n == 1 ? '' : " ($n+ messages)";
 
                if ($level == 0 || $attr ne $prev_attr) {
-                       my $mbox = qq(<a\nhref="$mid/t.mbox.gz">mbox.gz</a>);
-                       my $atom = qq(<a\nhref="$mid/t.atom">Atom</a>);
+                       my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
+                       my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
                        $pfx .= INDENT if $level > 0;
                        $cur->[1] .= $pfx . $attr . $n . " - $mbox / $atom\n";
                        $prev_attr = $attr;
@@ -884,13 +905,38 @@ sub emit_topics {
        }
        push @recent, $cur if $cur;
        @recent = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @recent;
-       $fh->write(join('', @recent) . '</pre>');
+       $ctx->{-html_tip} = join('', '<pre>', @recent, '</pre>');
+       200;
 }
 
-sub emit_index_topics {
+sub index_nav { # callback for WwwStream
+       my (undef, $ctx) = @_;
+       delete $ctx->{qp} or return;
+       my ($next, $prev);
+       $next = $prev = '    ';
+       my $latest = '';
+
+       my $next_o = $ctx->{-next_o};
+       if ($next_o) {
+               $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
+       }
+       if (my $cur_o = $ctx->{-cur_o}) {
+               $latest = qq! <a\nhref=.>latest</a>!;
+
+               my $o = $cur_o - ($next_o - $cur_o);
+               if ($o > 0) {
+                       $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
+               } elsif ($o == 0) {
+                       $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
+               }
+       }
+       "<hr><pre>page: $next $prev$latest</pre>";
+}
+
+sub index_topics {
        my ($ctx) = @_;
-       my ($off) = (($ctx->{cgi}->param('o') || '0') =~ /(\d+)/);
-       $ctx->{order} = [];
+       my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
+       my $order = $ctx->{order} = [];
        $ctx->{subjs} = {};
        $ctx->{latest} = {};
        my $max = 25;
@@ -902,9 +948,9 @@ sub emit_index_topics {
                walk_thread(thread_results($sres), $ctx, *add_topic);
                $opts{offset} += $nr;
        }
-
-       emit_topics($ctx);
-       $opts{offset};
+       $ctx->{-next_o} = $opts{offset};
+       $ctx->{-cur_o} = $off;
+       PublicInbox::WwwStream->response($ctx, topics($ctx), *index_nav);
 }
 
 sub thread_adj_level {