unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 08/13] www: reinstate old thread view as an option
Date: Thu, 30 Jun 2016 09:21:38 +0000	[thread overview]
Message-ID: <20160630092143.31651-9-e@80x24.org> (raw)
In-Reply-To: <20160630092143.31651-1-e@80x24.org>

This hybrid view is better than the old flat, but can
still fall down compared to the old threaded view in
some cases.
---
 lib/PublicInbox/View.pm | 109 ++++++++++++++++++++++++++++++++++++++++++++----
 lib/PublicInbox/WWW.pm  |  13 +++---
 t/plack.t               |   2 +-
 3 files changed, 109 insertions(+), 15 deletions(-)

diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index 0b96bda..17d6de5 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -142,7 +142,19 @@ sub index_entry {
 		" / <a\nhref=\"${mhref}#R\">reply</a>";
 	if (my $pct = $ctx->{pct}) { # used by SearchView.pm
 		$rv .= " [relevance $pct->{$mid_raw}%]";
+	} elsif ($mapping) {
+		my $threaded = 'threaded';
+		my $flat = 'flat';
+		my $end = '';
+		if ($ctx->{flat}) {
+			$flat = "<b>$flat</b>";
+		} else {
+			$threaded = "<b>$threaded</b>";
+		}
+		$rv .= " [<a\nhref=\"${mhref}t/#u\">$threaded</a>";
+		$rv .= "|<a\nhref=\"${mhref}T/#u\">$flat</a>]";
 	}
+
 	$rv .= $more ? "\n\n" : "\n";
 }
 
@@ -150,7 +162,7 @@ sub pad_link ($$;$) {
 	my ($mid, $level, $s) = @_;
 	$s ||= '...';
 	my $id = id_compress($mid, 1);
-	(' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)<a>\n";
+	(' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
 }
 
 sub _th_index_lite {
@@ -186,7 +198,7 @@ sub _th_index_lite {
 	my $s_c = nr_to_s($nr_c, 'reply', 'replies');
 	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> !!s; # no point in duplicating subject
 	$rv .= "<b>@ $this";
 	my $node = $map->[2];
 	if (my $child = $node->child) {
@@ -210,7 +222,7 @@ sub _th_index_lite {
 			$rv .= $pad . $mapping->{$nn->messageid}->[1];
 		}
 	}
-	$rv .= "<a\nhref=#e$id\nid=m$id>_<a> <a\nhref=#r$id\n>$s_s, $s_c</a>\n";
+	$rv .= "<a\nhref=#e$id\nid=m$id>_</a> <a\nhref=#r$id>$s_s, $s_c</a>\n";
 }
 
 sub walk_thread {
@@ -237,6 +249,51 @@ sub pre_thread  {
 	skel_dump($ctx, $level, $node);
 }
 
+sub thread_index_entry {
+	my ($ctx, $level, $mime) = @_;
+	my ($beg, $end) = thread_adj_level($ctx, $level);
+	$beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
+}
+
+sub stream_thread ($$) {
+	my ($th, $ctx) = @_;
+	my $inbox = $ctx->{-inbox};
+	my $mime;
+	my @q = map { (0, $_) } $th->rootset;
+	my $level;
+	while (@q) {
+		$level = shift @q;
+		my $node = shift @q or next;
+		unshift @q, $level+1, $node->child, $level, $node->next;
+		$mime = $inbox->msg_by_mid($node->messageid) and last;
+	}
+	return missing_thread($ctx) unless $mime;
+
+	$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 {
+		return unless $ctx;
+		while (@q) {
+			$level = shift @q;
+			my $node = shift @q or next;
+			unshift @q, $level+1, $node->child, $level, $node->next;
+			my $mid = $node->messageid;
+			if ($mime = $inbox->msg_by_mid($mid)) {
+				$mime = Email::MIME->new($mime);
+				return thread_index_entry($ctx, $level, $mime);
+			} else {
+				return ghost_index_entry($ctx, $level, $mid);
+			}
+		}
+		my $ret = join('', thread_adj_level($ctx, 0));
+		$ret .= ${$ctx->{dst}}; # skel
+		$ctx = undef;
+		$ret;
+	});
+	[ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
+}
+
 sub thread_html {
 	my ($ctx) = @_;
 	my $mid = $ctx->{mid};
@@ -244,30 +301,34 @@ sub thread_html {
 	my $msgs = load_results($sres);
 	my $nr = $sres->{total};
 	return missing_thread($ctx) if $nr == 0;
-	my $skel = '</pre><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: ";
 	$skel .= "<a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
 	$skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
+	$ctx->{-upfx} = '../../';
 	$ctx->{cur_level} = 0;
 	$ctx->{dst} = \$skel;
-	$ctx->{mapping} = {}; # mid -> [ reply count, from@date, node ];
 	$ctx->{prev_attr} = '';
 	$ctx->{prev_level} = 0;
 	$ctx->{root_anchor} = anchor_for($mid);
 	$ctx->{seen} = {};
+	$ctx->{mapping} = {};
 
-	walk_thread(thread_results($msgs), $ctx, *pre_thread);
+	my $th = thread_results($msgs);
+	walk_thread($th, $ctx, *pre_thread);
+	$skel .= '</pre>';
+	return stream_thread($th, $ctx) unless $ctx->{flat};
 
-	# lazy load the full message from mini_mime:
+	# flat display: lazy load the full message from mini_mime:
 	my $inbox = $ctx->{-inbox};
 	my $mime;
 	while ($mime = shift @$msgs) {
 		$mime = $inbox->msg_by_mid(mid_clean(mid_mime($mime))) and last;
 	}
+	return missing_thread($ctx) unless $mime;
 	$mime = Email::MIME->new($mime);
-	$ctx->{-upfx} = '../../';
 	$ctx->{-title_html} = ascii_html($mime->header('Subject'));
 	$ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
 	$mime = undef;
@@ -282,7 +343,7 @@ sub thread_html {
 			return index_entry($mime, $ctx, scalar @$msgs);
 		}
 		$msgs = undef;
-		$skel .= '</pre>';
+		'</pre>'.$skel;
 	});
 	[ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
 }
@@ -846,4 +907,34 @@ sub emit_index_topics {
 	$opts{offset};
 }
 
+sub thread_adj_level {
+	my ($ctx, $level) = @_;
+
+	my $max = $ctx->{cur_level};
+	if ($level <= 0) {
+		return ('', '') if $max == 0; # flat output
+
+		# reset existing lists
+		my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
+		$ctx->{cur_level} = 0;
+		("$beg</ul>", '');
+	} elsif ($level == $max) { # continue existing list
+		qw(<li> </li>);
+	} elsif ($level < $max) {
+		my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
+		$ctx->{cur_level} = $level;
+		("$beg<li>", '</li>');
+	} else { # ($level > $max) # start a new level
+		$ctx->{cur_level} = $level;
+		my $beg = ($max ? '<li>' : '') . '<ul><li>';
+		($beg, '</li>');
+	}
+}
+
+sub ghost_index_entry {
+	my ($ctx, $level, $mid) = @_;
+	my ($beg, $end) = thread_adj_level($ctx,  $level);
+	$beg . '<pre>'. ghost_parent($ctx->{-upfx}, $mid) . '</pre>' . $end;
+}
+
 1;
diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm
index da5c1d3..cbd3142 100644
--- a/lib/PublicInbox/WWW.pm
+++ b/lib/PublicInbox/WWW.pm
@@ -23,7 +23,7 @@ require PublicInbox::Git;
 use PublicInbox::GitHTTPBackend;
 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
 our $MID_RE = qr!([^/]+)!;
-our $END_RE = qr!(t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
+our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
 
 sub new {
@@ -92,9 +92,10 @@ sub call {
 		invalid_inbox_mid($self, $ctx, $1, $2) ||
 			get_attach($ctx, $idx, $fn);
 	# in case people leave off the trailing slash:
-	} elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(?:T|T/|t)\z!o) {
-		my ($inbox, $mid) = ($1, $2);
-		r301($ctx, $inbox, $mid, 't/#u');
+	} elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
+		my ($inbox, $mid, $suffix) = ($1, $2, $3);
+		$suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
+		r301($ctx, $inbox, $mid, $suffix);
 
 	} elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
 		my ($inbox, $mid) = ($1, $2);
@@ -241,8 +242,9 @@ sub get_mid_html {
 
 # /$INBOX/$MESSAGE_ID/t/
 sub get_thread {
-	my ($ctx) = @_;
+	my ($ctx, $flat) = @_;
 	searcher($ctx) or return need_search($ctx);
+	$ctx->{flat} = $flat;
 	require PublicInbox::View;
 	PublicInbox::View::thread_html($ctx);
 }
@@ -416,6 +418,7 @@ sub msg_page {
 	my $ret;
 	$ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
 	'' eq $e and return get_mid_html($ctx);
+	'T/' eq $e and return get_thread($ctx, 1);
 	't/' eq $e and return get_thread($ctx);
 	't.atom' eq $e and return get_thread_atom($ctx);
 	't.mbox' eq $e and return get_thread_mbox($ctx);
diff --git a/t/plack.t b/t/plack.t
index 209c6f9..a4f3245 100644
--- a/t/plack.t
+++ b/t/plack.t
@@ -101,7 +101,7 @@ EOF
 			my $res = $cb->(GET($u));
 			is(301, $res->code, "redirect for missing /");
 			my $location = $res->header('Location');
-			like($location, qr!/t/#u\z!,
+			like($location, qr!/\Q$t\E/#u\z!,
 				'redirected with missing /');
 		});
 	}
-- 
EW


  parent reply	other threads:[~2016-06-30  9:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-30  9:21 [PATCH 0/13] www: hybrid flat+thread conversation view Eric Wong
2016-06-30  9:21 ` [PATCH 01/13] www: implement " Eric Wong
2016-06-30  9:21 ` [PATCH 02/13] www: use WwwStream for dumping thread and search views Eric Wong
2016-06-30  9:21 ` [PATCH 03/13] view: show thread context in the thread-aware flat view Eric Wong
2016-06-30  9:21 ` [PATCH 04/13] view: merge $state hash with existing $ctx Eric Wong
2016-06-30  9:21 ` [PATCH 05/13] feed: add $INBOX/new.html endpoint Eric Wong
2016-06-30  9:21 ` [PATCH 06/13] view: tweak thread/index header slightly Eric Wong
2016-06-30  9:21 ` [PATCH 07/13] view: show more nearby messages in flat thread view Eric Wong
2016-06-30  9:21 ` Eric Wong [this message]
2016-06-30  9:21 ` [PATCH 09/13] view: fix up some HTML injection via Message-ID vectors Eric Wong
2016-06-30 19:03   ` [PATCH 14/13] view: fix permalink and raw links at the top Eric Wong
2016-06-30  9:21 ` [PATCH 10/13] view: default to flat/hybrid thread display Eric Wong
2016-06-30  9:21 ` [PATCH 11/13] view: show thread size when linking to summary Eric Wong
2016-06-30  9:21 ` [PATCH 12/13] view: fixup bad reference to new_msgid Eric Wong
2016-06-30  9:21 ` [PATCH 13/13] www_stream: add response wrapper sub Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160630092143.31651-9-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).