From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-3.0 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, URIBL_BLOCKED shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: meta@public-inbox.org Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 4BC5C1F81F; Sun, 16 Aug 2015 08:38:05 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Cc: Eric Wong Subject: [RFC 04/11] view: display replies in per-message view Date: Sun, 16 Aug 2015 08:37:52 +0000 Message-Id: <1439714279-21923-5-git-send-email-e@80x24.org> In-Reply-To: <1439714279-21923-1-git-send-email-e@80x24.org> References: <1439714279-21923-1-git-send-email-e@80x24.org> List-Id: This can be used to quickly scan for replies in a message without displaying an entire thread. --- lib/PublicInbox/SearchMsg.pm | 30 ++++++++++++++- lib/PublicInbox/View.pm | 87 +++++++++++++++++++++++++++++++++++++++++--- lib/PublicInbox/WWW.pm | 20 ++++++++-- 3 files changed, 126 insertions(+), 11 deletions(-) diff --git a/lib/PublicInbox/SearchMsg.pm b/lib/PublicInbox/SearchMsg.pm index 619a19d..550521a 100644 --- a/lib/PublicInbox/SearchMsg.pm +++ b/lib/PublicInbox/SearchMsg.pm @@ -6,9 +6,12 @@ use strict; use warnings; use Search::Xapian; use Email::Address qw//; +use Email::Simple qw//; use POSIX qw//; use Date::Parse qw/str2time/; use PublicInbox::MID qw/mid_clean mid_compressed/; +use Encode qw/find_encoding/; +my $enc_utf8 = find_encoding('UTF-8'); our $PFX2TERM_RE = undef; sub new { @@ -26,7 +29,9 @@ sub wrap { sub load_doc { my ($class, $doc) = @_; - my ($mid, $subj, $from, $date) = split(/\n/, $doc->get_data); + my $data = $doc->get_data; + $data = $enc_utf8->decode($data); + my ($mid, $subj, $from, $date) = split(/\n/, $data); bless { doc => $doc, mid => $mid, @@ -52,11 +57,11 @@ sub from { my @from; if ($from) { + $from =~ tr/\n/ /; @from = Email::Address->parse($from); $self->{from} = $from[0]; $from = $from[0]->name; } - $from =~ tr/\n/ /; $self->{from_name} = $from; $self->{from}; } @@ -122,6 +127,27 @@ sub ensure_metadata { } } +# for threading only +sub mini_mime { + my ($self) = @_; + $self->ensure_metadata; + my @h = ( + Subject => $self->subject, + 'X-PI-From' => $self->from_name, + 'X-PI-Date' => $self->date, + 'X-PI-TS' => $self->ts, + 'Message-ID' => "<$self->{mid}>", + ); + if (my $refs = $self->{references}) { + push @h, References => '<' . join('> <', @$refs) . '>'; + } + if (my $irt = $self->{inreplyto}) { + push @h, 'In-Reply-To' => "<$irt>"; + } + + Email::MIME->create(header_str => \@h); +} + sub mid { my ($self, $mid) = @_; diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index c2dbb7e..fcc98ab 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -22,7 +22,7 @@ my $enc_utf8 = find_encoding('UTF-8'); # public functions: sub msg_html { - my ($class, $mime, $full_pfx, $footer) = @_; + my ($class, $mime, $full_pfx, $footer, $srch) = @_; if (defined $footer) { $footer = "\n" . $footer; } else { @@ -31,7 +31,7 @@ sub msg_html { headers_to_html_header($mime, $full_pfx) . multipart_text_as_html($mime, $full_pfx) . '
' . PRE_WRAP . - html_footer($mime, 1) . $footer . + html_footer($mime, 1, $full_pfx, $srch) . $footer . ''; } @@ -325,7 +325,7 @@ sub headers_to_html_header { } sub html_footer { - my ($mime, $standalone) = @_; + my ($mime, $standalone, $full_pfx, $srch) = @_; my %cc; # everyone else my $to; # this is the From address @@ -344,8 +344,8 @@ sub html_footer { my $subj = $mime->header('Subject') || ''; $subj = "Re: $subj" unless $subj =~ /\bRe:/; - my $irp = uri_escape_utf8( - $mime->header_obj->header_raw('Message-ID') || ''); + my $mid = $mime->header_obj->header_raw('Message-ID'); + my $irp = uri_escape_utf8($mid); delete $cc{$to}; $to = uri_escape_utf8($to); $subj = uri_escape_utf8($subj); @@ -353,9 +353,28 @@ sub html_footer { my $cc = uri_escape_utf8(join(',', sort values %cc)); my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj"; + my $irt = ''; my $idx = $standalone ? " index" : ''; + if ($idx && $srch) { + my $res = $srch->get_replies($mid); + if (my $c = $res->{count}) { + $c = $c == 1 ? '1 reply' : "$c replies"; + $idx .= "\n$c:\n"; + thread_replies(\$idx, $mime, $res); + } else { + $idx .= "\n(no replies yet)\n"; + } + $irt = $mime->header_obj->header_raw('In-Reply-To'); + if ($irt) { + $irt = PublicInbox::Hval->new_msgid($irt); + $irt = $irt->as_href; + $irt = "parent "; + } else { + $irt = ' ' x length('parent '); + } + } - "reply' . $idx; + "$irtreply' . $idx; } sub linkify_refs { @@ -372,4 +391,60 @@ sub anchor_for { 'm' . mid_compressed(mid_clean($msgid)); } +# children are chronological +sub simple_sort_children { + sort { + (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=> + (eval { $b->topmost->message->header('X-PI-TS') } || 0) + } @_; +} + +sub simple_dump { + my ($dst, $root, $node, $level) = @_; + $$dst .= ' ' x $level; + if (my $x = $node->message) { + my $mid = $x->header('Message-ID'); + if ($root->[0] ne $mid) { + my $s = clean_subj($x->header('Subject')); + if ($root->[1] eq $s) { + $s = ' '; + } else { + $s = PublicInbox::Hval->new($s); + $s = $s->as_html . ' '; + } + my $m = PublicInbox::Hval->new_msgid($mid); + my $f = PublicInbox::Hval->new($x->header('X-PI-From')); + my $d = PublicInbox::Hval->new($x->header('X-PI-Date')); + $m = $m->as_href . '.html'; + $f = $f->as_html; + $d = $d->as_html; + $$dst .= "` $s$f @ $d UTC\n"; + } + } + simple_dump($dst, $root, $node->child, $level + 1) if $node->child; + simple_dump($dst, $root, $node->next, $level) if $node->next; +} + +sub clean_subj { + my ($subj) = @_; + $subj =~ s/\A\s+//; + $subj =~ s/\s+\z//; + $subj =~ s/^(?:re|aw):\s*//i; # remove reply prefix (aw: German) + $subj =~ s/\s+/ /; + $subj; +} + +sub thread_replies { + my ($dst, $root, $res) = @_; + my @msgs = map { $_->mini_mime } @{$res->{msgs}}; + require PublicInbox::Thread; + $root->header_set('X-PI-TS', '0'); + my $th = PublicInbox::Thread->new($root, @msgs); + $th->thread; + $th->order(sub { simple_sort_children(@_) }); + $root = [ $root->header('Message-ID'), + clean_subj($root->header('Subject')) ]; + simple_dump($dst, $root, $_, 0) for $th->rootset; +} + 1; diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm index 1814286..32cc0b2 100644 --- a/lib/PublicInbox/WWW.pm +++ b/lib/PublicInbox/WWW.pm @@ -175,8 +175,10 @@ sub get_mid_html { my $pfx = "../f/$mid_href.html"; my $foot = footer($ctx); require Email::MIME; + my $mime = Email::MIME->new($x); + my $srch = searcher($ctx); [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ], - [ PublicInbox::View->msg_html(Email::MIME->new($x), $pfx, $foot) ] ]; + [ PublicInbox::View->msg_html($mime, $pfx, $foot, $srch) ] ]; } # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes) @@ -185,10 +187,12 @@ sub get_full_html { my $x = mid2blob($ctx); return r404() unless $x; require PublicInbox::View; - require Email::MIME; my $foot = footer($ctx); + require Email::MIME; + my $mime = Email::MIME->new($x); + my $srch = searcher($ctx); [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ], - [ PublicInbox::View->msg_html(Email::MIME->new($x), undef, $foot)] ]; + [ PublicInbox::View->msg_html($mime, undef, $foot, $srch)] ]; } sub self_url { @@ -281,4 +285,14 @@ sub footer { ); } +# search support is optional, returns undef if Xapian is not installed +# or not configured for the given GIT_DIR +sub searcher { + my ($ctx) = @_; + eval { + require PublicInbox::Search; + PublicInbox::Search->new($ctx->{git_dir}); + }; +} + 1; -- EW