]> Sergey Matveev's repositories - public-inbox.git/commitdiff
view: deduplicate common code for loading search results
authorEric Wong <e@80x24.org>
Sun, 16 Aug 2015 08:53:41 +0000 (08:53 +0000)
committerEric Wong <e@80x24.org>
Sun, 16 Aug 2015 08:53:41 +0000 (08:53 +0000)
More to come later.

lib/PublicInbox/MID.pm
lib/PublicInbox/View.pm
lib/PublicInbox/WWW.pm

index e5a30a1b0924c04ed721837f47ba49b2f411ddcb..d097011bf4ae21b58075def04f2306cd66a22f54 100644 (file)
@@ -4,7 +4,7 @@ package PublicInbox::MID;
 use strict;
 use warnings;
 use base qw/Exporter/;
-our @EXPORT_OK = qw/mid_clean mid_compressed/;
+our @EXPORT_OK = qw/mid_clean mid_compressed mid2path/;
 use Digest::SHA qw/sha1_hex/;
 use constant MID_MAX => 40; # SHA-1 hex length
 
@@ -24,4 +24,16 @@ sub mid_compressed {
        sha1_hex($mid);
 }
 
+sub mid2path {
+       my ($mid) = @_;
+       my ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
+
+       unless (defined $x38) {
+               # compatibility with old links (or short Message-IDs :)
+               $mid = sha1_hex($mid);
+               ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
+       }
+       "$x2/$x38";
+}
+
 1;
index 696d7d5a0984bdad067564d932b2490b51d7b7e3..575c5ffd31c098b4c5eb07a4322e47c27772cde1 100644 (file)
@@ -9,7 +9,7 @@ use Encode qw/find_encoding/;
 use Encode::MIME::Header;
 use Email::MIME::ContentType qw/parse_content_type/;
 use PublicInbox::Hval;
-use PublicInbox::MID qw/mid_clean mid_compressed/;
+use PublicInbox::MID qw/mid_clean mid_compressed mid2path/;
 use Digest::SHA;
 require POSIX;
 
@@ -138,37 +138,11 @@ sub thread_html {
        my $mid = mid_compressed($ctx->{mid});
        my $res = $srch->get_thread($mid);
        my $rv = '';
-       require PublicInbox::GitCatFile;
-       my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
-       my $nr = scalar @{$res->{msgs}};
+       my $msgs = load_results($ctx, $res);
+       my $nr = scalar @$msgs;
        return $rv if $nr == 0;
-       my @msgs;
-       while (my $smsg = shift @{$res->{msgs}}) {
-               my $m = $smsg->mid;
-
-               # Duplicated from WWW.pm
-               my ($x2, $x38) = ($m =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
-
-               unless (defined $x38) {
-                       require Digest::SHA;
-                       $m = Digest::SHA::sha1_hex($m);
-                       ($x2, $x38) = ($m =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
-               }
-
-               # FIXME: duplicated code from Feed.pm
-               my $mime = eval {
-                       my $str = $git->cat_file("HEAD:$x2/$x38");
-                       Email::MIME->new($str);
-               };
-               unless ($@) {
-                       my $t = eval { str2time($mime->header('Date')) };
-                       defined($t) or $t = 0;
-                       $mime->header_set('X-PI-TS', $t);
-                       push @msgs, $mime;
-               }
-       }
        require PublicInbox::Thread;
-       my $th = PublicInbox::Thread->new(@msgs);
+       my $th = PublicInbox::Thread->new(@$msgs);
        $th->thread;
        $th->order(*PublicInbox::Thread::sort_ts);
        my $state = [ undef, { root_anchor => anchor_for($mid) }, undef, 0 ];
@@ -184,37 +158,11 @@ sub subject_path_html {
        my $path = $ctx->{subject_path};
        my $res = $srch->get_subject_path($path);
        my $rv = '';
-       require PublicInbox::GitCatFile;
-       my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
-       my $nr = scalar @{$res->{msgs}};
+       my $msgs = load_results($ctx, $res);
+       my $nr = scalar @$msgs;
        return $rv if $nr == 0;
-       my @msgs;
-       while (my $smsg = shift @{$res->{msgs}}) {
-               my $m = $smsg->mid;
-
-               # Duplicated from WWW.pm
-               my ($x2, $x38) = ($m =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
-
-               unless (defined $x38) {
-                       require Digest::SHA;
-                       $m = Digest::SHA::sha1_hex($m);
-                       ($x2, $x38) = ($m =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
-               }
-
-               # FIXME: duplicated code from Feed.pm
-               my $mime = eval {
-                       my $str = $git->cat_file("HEAD:$x2/$x38");
-                       Email::MIME->new($str);
-               };
-               unless ($@) {
-                       my $t = eval { str2time($mime->header('Date')) };
-                       defined($t) or $t = 0;
-                       $mime->header_set('X-PI-TS', $t);
-                       push @msgs, $mime;
-               }
-       }
        require PublicInbox::Thread;
-       my $th = PublicInbox::Thread->new(@msgs);
+       my $th = PublicInbox::Thread->new(@$msgs);
        $th->thread;
        $th->order(*PublicInbox::Thread::sort_ts);
        my $state = [ undef, { root_anchor => 'dummy' }, undef, 0 ];
@@ -597,4 +545,29 @@ sub thread_entry {
        thread_entry($dst, $state, $node->next, $level) if $node->next;
 }
 
+sub load_results {
+       my ($ctx, $res) = @_;
+
+       require PublicInbox::GitCatFile;
+       my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
+       my @msgs;
+       while (my $smsg = shift @{$res->{msgs}}) {
+               my $m = $smsg->mid;
+               my $path = mid2path($m);
+
+               # FIXME: duplicated code from Feed.pm
+               my $mime = eval {
+                       my $str = $git->cat_file("HEAD:$path");
+                       Email::MIME->new($str);
+               };
+               unless ($@) {
+                       my $t = eval { str2time($mime->header('Date')) };
+                       defined($t) or $t = 0;
+                       $mime->header_set('X-PI-TS', $t);
+                       push @msgs, $mime;
+               }
+       }
+       \@msgs;
+}
+
 1;
index 7fe9b85be77dd02f22552e2f8199fd13ca7317d0..bbd438a2d939bc1d42d9fbbcc83b1634ec74d0e8 100644 (file)
@@ -141,19 +141,10 @@ sub get_index {
 # just returns a string ref for the blob in the current ctx
 sub mid2blob {
        my ($ctx) = @_;
-       my $hex = $ctx->{mid};
-       my ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
-
-       unless (defined $x38) {
-               # compatibility with old links
-               require Digest::SHA;
-               $hex = Digest::SHA::sha1_hex($hex);
-               ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
-               defined $x38 or die "BUG: not a SHA-1 hex: $hex";
-       }
-
+       require PublicInbox::MID;
+       my $path = PublicInbox::MID::mid2path($ctx->{mid});
        my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
-                       qw(cat-file blob), "HEAD:$x2/$x38");
+                       qw(cat-file blob), "HEAD:$path");
        my $cmd = join(' ', @cmd);
        my $pid = open my $fh, '-|';
        defined $pid or die "fork failed: $!\n";