]> Sergey Matveev's repositories - public-inbox.git/commitdiff
thread: common sorting code
authorEric Wong <e@80x24.org>
Sat, 15 Aug 2015 23:41:21 +0000 (23:41 +0000)
committerEric Wong <e@80x24.org>
Sat, 15 Aug 2015 23:45:33 +0000 (23:45 +0000)
We'll be sharing the same threading, so it makes sense to sort
replies using the same code and message headers without repeating
ourselves.

This also standardizes on sorting on X-PI-TS (Unix epoch in seconds)
instead over using X-PI-Date differently in two different places

lib/PublicInbox/Feed.pm
lib/PublicInbox/Thread.pm
lib/PublicInbox/View.pm

index f7c2f3293067f2e60d6c72be0921084d8dd7eda7..b53255975a3f309be44059fcc6fc7ec4436d86c8 100644 (file)
@@ -73,11 +73,13 @@ sub generate_html_index {
                '</head><body>' . PRE_WRAP;
 
        # sort child messages in chronological order
-       $th->order(sub { mime_sort_children(@_) });
+       $th->order(*PublicInbox::Thread::sort_ts);
 
        # except we sort top-level messages reverse chronologically
        my $state = [ time, {}, $first, 0 ];
-       for (mime_sort_roots($th)) { dump_msg($_, 0, \$html, $state) }
+       for (PublicInbox::Thread::rsort_ts($th->rootset)) {
+               dump_msg($_, 0, \$html, $state)
+       }
        Email::Address->purge_cache;
 
        my $footer = nav_footer($args->{cgi}, $last, $feed_opts, $state);
@@ -299,26 +301,9 @@ sub mime_load_for_sort {
 
        my $t = eval { str2time($mime->header('Date')) };
        defined($t) or $t = 0;
-       $mime->header_set('X-PI-Date', $t);
+       $mime->header_set('X-PI-TS', $t);
        push @$messages, $mime;
        1;
 }
 
-# children are chronological
-sub mime_sort_children {
-       sort {
-               $a->topmost->message->header('X-PI-Date') <=>
-               $b->topmost->message->header('X-PI-Date')
-       } @_;
-}
-
-# parents are reverse chronological
-sub mime_sort_roots {
-       my ($th) = @_;
-       sort {
-               (eval { $b->message->header('X-PI-Date') } || 0) <=>
-               (eval { $a->message->header('X-PI-Date') } || 0)
-       } $th->rootset;
-}
-
 1;
index 7dabf2439927ee2a6546b3218d060b109a999dc4..58efb8dc5872ed01fc0a3824654c200d4010f26d 100644 (file)
@@ -12,6 +12,20 @@ if ($Mail::Thread::VERSION <= 2.55) {
        eval q(sub _container_class { 'PublicInbox::Thread::Container' });
 }
 
+sub sort_ts {
+       sort {
+               (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
+               (eval { $b->topmost->message->header('X-PI-TS') } || 0)
+       } @_;
+}
+
+sub rsort_ts {
+       sort {
+               (eval { $b->topmost->message->header('X-PI-TS') } || 0) <=>
+               (eval { $a->topmost->message->header('X-PI-TS') } || 0)
+       } @_;
+}
+
 package PublicInbox::Thread::Container;
 use strict;
 use warnings;
index fcc98ab80b60adf0f4dfd39ddcd408e44ff1bb15..dcdb310991dabfd43b127e5d6bba99cb9d2e2533 100644 (file)
@@ -67,7 +67,7 @@ sub index_entry {
        $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
        my $pfx = ('  ' x $level);
 
-       my $ts = $mime->header('X-PI-Date');
+       my $ts = $mime->header('X-PI-TS');
        my $fmt = '%Y-%m-%d %H:%M UTC';
        $ts = POSIX::strftime($fmt, gmtime($ts));
 
@@ -391,14 +391,6 @@ 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;
@@ -441,7 +433,7 @@ sub thread_replies {
        $root->header_set('X-PI-TS', '0');
        my $th = PublicInbox::Thread->new($root, @msgs);
        $th->thread;
-       $th->order(sub { simple_sort_children(@_) });
+       $th->order(*PublicInbox::Thread::sort_ts);
        $root = [ $root->header('Message-ID'),
                  clean_subj($root->header('Subject')) ];
        simple_dump($dst, $root, $_, 0) for $th->rootset;