about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-08-15 09:28:34 +0000
committerEric Wong <e@80x24.org>2015-08-15 23:34:34 +0000
commit226657eb31e326cc8329229e8ba0f63ff4c75083 (patch)
treeec6afeed7dde21b1bda566ad9bf29eb508139fdd /lib
parent7edf30e5349ab5566815e5050e9ba0f53e1d0bb9 (diff)
downloadpublic-inbox-226657eb31e326cc8329229e8ba0f63ff4c75083.tar.gz
This can be used to quickly scan for replies in a message without
displaying an entire thread.
Diffstat (limited to 'lib')
-rw-r--r--lib/PublicInbox/SearchMsg.pm30
-rw-r--r--lib/PublicInbox/View.pm87
-rw-r--r--lib/PublicInbox/WWW.pm20
3 files changed, 126 insertions, 11 deletions
diff --git a/lib/PublicInbox/SearchMsg.pm b/lib/PublicInbox/SearchMsg.pm
index 619a19d4..550521aa 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 c2dbb7ed..fcc98ab8 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><hr />' . PRE_WRAP .
-                html_footer($mime, 1) . $footer .
+                html_footer($mime, 1, $full_pfx, $srch) . $footer .
                 '</pre></body></html>';
 }
 
@@ -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 ? " <a\nhref=\"../\">index</a>" : '';
+        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 = "<a\nhref=\"$irt\">parent</a> ";
+                } else {
+                        $irt = ' ' x length('parent ');
+                }
+        }
 
-        "<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
+        "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $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 .= "` <a\nhref=\"$m\">$s$f @ $d UTC</a>\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 1814286b..32cc0b27 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;