From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.0 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 6D06F20FD2 for ; Thu, 30 Jun 2016 09:21:47 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 03/13] view: show thread context in the thread-aware flat view Date: Thu, 30 Jun 2016 09:21:33 +0000 Message-Id: <20160630092143.31651-4-e@80x24.org> In-Reply-To: <20160630092143.31651-1-e@80x24.org> References: <20160630092143.31651-1-e@80x24.org> List-Id: This lets user have a small window of the context of the current message relative to other threads. --- lib/PublicInbox/Feed.pm | 3 +- lib/PublicInbox/SearchView.pm | 2 +- lib/PublicInbox/View.pm | 160 +++++++++++++++++++++++++----------------- 3 files changed, 100 insertions(+), 65 deletions(-) diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm index 36802fa..73986e8 100644 --- a/lib/PublicInbox/Feed.pm +++ b/lib/PublicInbox/Feed.pm @@ -138,6 +138,7 @@ sub emit_html_index { my $fh = $res->([200,['Content-Type'=>'text/html; charset=UTF-8']]); my $max = $ctx->{max} || MAX_PER_PAGE; + $ctx->{-upfx} = ''; my ($footer, $param, $last); my $state = { ctx => $ctx, seen => {}, anchor_idx => 0, fh => $fh }; @@ -174,7 +175,7 @@ sub emit_index_nosrch { $state->{first} ||= $commit; my $mime = do_cat_mail($ibx, $path) or return 0; - $fh->write(PublicInbox::View::index_entry($mime, $state)); + $fh->write(PublicInbox::View::index_entry($mime, $state, 1)); 1; }); $last; diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm index 488822e..8771d5d 100644 --- a/lib/PublicInbox/SearchView.pm +++ b/lib/PublicInbox/SearchView.pm @@ -169,6 +169,7 @@ sub mset_thread { } my $skel = search_nav_bot($mset, $q). "
";
 	my $inbox = $ctx->{-inbox};
+	$ctx->{-upfx} = '';
 	my $state = {
 		-inbox => $inbox,
 		anchor_idx => 1,
@@ -181,7 +182,6 @@ sub mset_thread {
 		prev_level => 0,
 		seen => {},
 		srch => $ctx->{srch},
-		upfx => './',
 	};
 
 	PublicInbox::View::walk_thread($th, $state,
diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index a774feb..eac541d 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -97,7 +97,7 @@ sub nr_to_s ($$$) {
 
 # this is already inside a 
 sub index_entry {
-	my ($mime, $state) = @_;
+	my ($mime, $state, $more) = @_;
 	my $ctx = $state->{ctx};
 	my $srch = $ctx->{srch};
 	my $hdr = $mime->header_obj;
@@ -109,51 +109,76 @@ sub index_entry {
 	my $mid = PublicInbox::Hval->new_msgid($mid_raw);
 
 	my $root_anchor = $state->{root_anchor} || '';
-	my $path = $root_anchor ? '../../' : '';
-	my $href = $mid->as_href;
 	my $irt = in_reply_to($hdr);
 
-	$subj = ''.ascii_html($subj).'';
-	$subj = "$subj" if $root_anchor eq $id_m;
-
-	my $ts = _msg_date($hdr);
-	my $rv = "# ";
-	$rv .= $subj;
-	my $mhref = $path.$href.'/';
-	my $from = _hdr_names($hdr, 'From');
-	$rv .= "\n- $from @ $ts UTC\n";
+	my $rv = ''.ascii_html($subj).'';
+	$rv = "$rv" if $root_anchor eq $id_m;
+	$rv .= "\n";
+	$rv .= _th_index_lite($mid_raw, $irt, $id, $state);
 	my @tocc;
 	foreach my $f (qw(To Cc)) {
 		my $dst = _hdr_names($hdr, $f);
 		push @tocc, "$f: $dst" if $dst ne '';
 	}
+	$rv .= "From: "._hdr_names($hdr, 'From').' @ '._msg_date($hdr)." UTC\n";
 	$rv .= '  '.join('; +', @tocc) . "\n" if @tocc;
 	$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]) });
-	$rv .= "\npermalink" .
-		" / raw / ";
-	my $mapping = $state->{mapping};
-	my $nr_c = $mapping->{$mid_raw} || 0;
+
+	# add the footer
+	$rv .= "\n^ ".
+		"permalink" .
+		" / raw" .
+		" / reply";
+	if (my $pct = $state->{pct}) { # used by SearchView.pm
+		$rv .= " [relevance $pct->{$mid_raw}%]";
+	}
+	$rv .= $more ? "\n\n" : "\n";
+}
+
+sub _th_index_lite {
+	my ($mid_raw, $irt, $id, $state) = @_;
+	my $rv = '';
+	my $mapping = $state->{mapping} or return $rv;
+	my $pad = '  ';
+	# map = [children, attr, node, idx, level]
+	my $map = $mapping->{$mid_raw};
+	my $nr_c = scalar @{$map->[0]};
 	my $nr_s = 0;
 	if (defined $irt) {
-		$nr_s = ($mapping->{$irt} || 0) - 1;
+		my $irt_map = $mapping->{$irt};
+		my $siblings = $irt_map->[0];
+		$nr_s = scalar(@$siblings) - 1;
 		$nr_s = 0 if $nr_s < 0;
-		$irt = anchor_for($irt);
-		$rv .= "#parent,";
-	} else {
-		$rv .= 'root message:';
+		$rv .= $pad . $irt_map->[1];
+		my $idx = $map->[3];
+		if ($idx > 0) {
+			my $prev = $siblings->[$idx - 1];
+			$rv .= $pad . $mapping->{$prev->messageid}->[1];
+		}
 	}
-	$nr_s = nr_to_s($nr_s, 'sibling', 'siblings');
-	$nr_c = nr_to_s($nr_c, 'reply', 'replies');
-	$rv .= " $nr_s, $nr_c";
-	$rv .= " / reply";
-
-	if (my $pct = $state->{pct}) { # used by SearchView.pm
-		$rv .= " [relevance $pct->{$mid_raw}%]";
+	my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
+	my $s_c = nr_to_s($nr_c, 'reply', 'replies');
+	my $this = $map->[1];
+	$this =~ s!\n\z!\n!s;
+	$this =~ s! !!s; # no point in duplicating subject
+	$rv .= "@ $this";
+	my $node = $map->[2];
+	if (my $child = $node->child) {
+		$rv .= $pad . $mapping->{$child->messageid}->[1];
+	}
+	if (my $next = $node->next) {
+		$rv .= $pad .  $mapping->{$next->messageid}->[1];
 	}
-	$rv .= "\n\n";
+	$rv .= ".\t\t\t";
+	$rv .= "($s_s, $s_c / ";
+	my $upfx = $state->{ctx}->{-upfx};
+	$rv .= qq{permalink / };
+	$rv .= qq{raw)\n};
 }
 
 sub walk_thread {
@@ -169,13 +194,14 @@ sub walk_thread {
 
 sub pre_thread  {
 	my ($state, $level, $node) = @_;
-	my $parent = $node->parent;
-	if ($parent) {
-		my $mid = $parent->messageid;
-		my $m = $state->{mapping};
-		$m->{$mid} ||= 0;
-		$m->{$mid}++;
+	my $mapping = $state->{mapping};
+	my $idx = -1;
+	if (my $parent = $node->parent) {
+		my $m = $mapping->{$parent->messageid}->[0];
+		$idx = scalar @$m;
+		push @$m, $node;
 	}
+	$mapping->{$node->messageid} = [ [], '', $node, $idx ];
 	skel_dump($state, $level, $node);
 }
 
@@ -196,13 +222,12 @@ sub thread_html {
 		ctx => $ctx,
 		cur_level => 0,
 		dst => \$skel,
-		mapping => {}, # mid -> reply count
+		mapping => {}, # mid -> [ reply count, from@date, node ];
 		prev_attr => '',
 		prev_level => 0,
 		root_anchor => anchor_for($mid),
 		seen => {},
 		srch => $ctx->{srch},
-		upfx => '../../',
 	};
 
 	walk_thread(thread_results($msgs), $state, *pre_thread);
@@ -216,7 +241,7 @@ sub thread_html {
 	$mime = Email::MIME->new($mime);
 	$ctx->{-upfx} = '../../';
 	$ctx->{-title_html} = ascii_html($mime->header('Subject'));
-	$ctx->{-html_tip} = '
'.index_entry($mime, $state);
+	$ctx->{-html_tip} = '
'.index_entry($mime, $state, scalar @$msgs);
 	$mime = undef;
 	my $body = PublicInbox::WwwStream->new($ctx, sub {
 		return unless $msgs;
@@ -224,9 +249,12 @@ sub thread_html {
 			$mid = mid_clean(mid_mime($mime));
 			$mime = $inbox->msg_by_mid($mid) and last;
 		}
-		return index_entry(Email::MIME->new($mime), $state) if $mime;
+		if ($mime) {
+			$mime = Email::MIME->new($mime);
+			return index_entry($mime, $state, scalar @$msgs);
+		}
 		$msgs = undef;
-		$skel .= "
"; + $skel .= '
'; }); [ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ]; } @@ -405,7 +433,6 @@ sub thread_skel { cur => $mid, prev_attr => '', prev_level => 0, - upfx => "$tpfx../", dst => $dst, }; walk_thread(thread_results(load_results($sres)), $state, *skel_dump); @@ -598,8 +625,7 @@ sub _skel_header { my $cur = $state->{cur}; my $mid = mid_clean($hdr->header_raw('Message-ID')); my $f = ascii_html($hdr->header('X-PI-From')); - my $d = _msg_date($hdr); - my $pfx = "$d " . indent_for($level) . th_pfx($level); + my $d = _msg_date($hdr) . ' ' . indent_for($level) . th_pfx($level); my $attr = $f; $state->{first_level} ||= $level; @@ -613,9 +639,9 @@ sub _skel_header { if ($cur) { if ($cur eq $mid) { delete $state->{cur}; - $$dst .= "$pfx". + $$dst .= $d; + $$dst .= "". "$attr [this message]\n"; - return; } } else { @@ -636,15 +662,18 @@ sub _skel_header { } my $m = PublicInbox::Hval->new_msgid($mid); my $id = ''; - if ($state->{mapping}) { + my $mapping = $state->{mapping}; + my $end = defined($s) ? "$s $f\n" : "$f\n"; + if ($mapping) { + my $map = $mapping->{$mid}; $id = id_compress($mid, 1); $m = '#m'.$id; + $map->[1] = "$d$end"; $id = "\nid=r".$id; } else { - $m = $state->{upfx}.$m->as_href.'/'; + $m = $state->{ctx}->{-upfx}.$m->as_href.'/'; } - $$dst .= "$pfx"; - $$dst .= defined($s) ? "$s $f\n" : "$f\n"; + $$dst .= $d . "" . $end; } sub skel_dump { @@ -654,25 +683,30 @@ sub skel_dump { } else { my $mid = $node->messageid; my $dst = $state->{dst}; + my $mapping = $state->{mapping}; + my $map = $mapping->{$mid} if $mapping; if ($mid eq 'subject dummy') { - $$dst .= "\t[no common parent]\n"; + my $ncp = "\t[no common parent]\n"; + $map->[1] = $ncp if $map; + $$dst .= $ncp; return; } - if ($state->{pct}) { # search result - $$dst .= ' [irrelevant] '; + my $d = $state->{pct} ? ' [irrelevant] ' # search result + : ' [not found] '; + $d .= indent_for($level) . th_pfx($level); + my $upfx = $state->{ctx}->{-upfx}; + my $m = PublicInbox::Hval->new_msgid($mid); + my $href = $upfx . $m->as_href . '/'; + my $html = $m->as_html; + + if ($map) { + my $id = id_compress($mid, 1); + $map->[1] = $d . qq{<$html>\n}; + $d .= qq{<$html>\n}; } else { - $$dst .= ' [not found] '; - } - $$dst .= indent_for($level) . th_pfx($level); - my $upfx = $state->{upfx}; - my $id = ''; - if ($state->{mapping}) { # thread index view - $id = "\nid=".anchor_for($mid); + $d .= qq{<$html>\n}; } - $mid = PublicInbox::Hval->new_msgid($mid); - my $href = $upfx . $mid->as_href . '/'; - my $html = $mid->as_html; - $$dst .= qq{<$html>\n}; + $$dst .= $d; } } -- EW