]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchView.pm
wwwstream: reduce blob fetch paths for ->getline
[public-inbox.git] / lib / PublicInbox / SearchView.pm
index 7e508bb7764238bfcc4958ecfcc131c08108f514..eeebdfa3137903f64426b0fc96773a58b946a3bd 100644 (file)
@@ -6,16 +6,15 @@ package PublicInbox::SearchView;
 use strict;
 use warnings;
 use URI::Escape qw(uri_unescape uri_escape);
-use PublicInbox::SearchMsg;
-use PublicInbox::Hval qw/ascii_html obfuscate_addrs/;
+use PublicInbox::Smsg;
+use PublicInbox::Hval qw(ascii_html obfuscate_addrs mid_href);
 use PublicInbox::View;
 use PublicInbox::WwwAtomStream;
+use PublicInbox::WwwStream qw(html_oneshot);
 use PublicInbox::SearchThread;
 our $LIM = 200;
 my %rmap_inc;
 
-my $noop = sub {};
-
 sub mbox_results {
        my ($ctx) = @_;
        my $q = PublicInbox::SearchQuery->new($ctx->{qp});
@@ -48,7 +47,7 @@ sub sres_top_html {
                relevance => $q->{r},
                asc => $asc,
        };
-       my ($mset, $total, $err, $cb);
+       my ($mset, $total, $err, $html);
 retry:
        eval {
                $mset = $srch->query($query, $opts);
@@ -58,8 +57,7 @@ retry:
        ctx_prepare($q, $ctx);
        if ($err) {
                $code = 400;
-               $ctx->{-html_tip} = '<pre>'.err_txt($ctx, $err).'</pre><hr>';
-               $cb = $noop;
+               $html = '<pre>'.err_txt($ctx, $err).'</pre><hr>';
        } elsif ($total == 0) {
                if (defined($ctx->{-uxs_retried})) {
                        # undo retry damage:
@@ -70,19 +68,16 @@ retry:
                        goto retry;
                }
                $code = 404;
-               $ctx->{-html_tip} = "<pre>\n[No results found]</pre><hr>";
-               $cb = $noop;
+               $html = "<pre>\n[No results found]</pre><hr>";
        } else {
                return adump($_[0], $mset, $q, $ctx) if $x eq 'A';
 
                $ctx->{-html_tip} = search_nav_top($mset, $q, $ctx);
-               if ($x eq 't') {
-                       $cb = mset_thread($ctx, $mset, $q);
-               } else {
-                       $cb = mset_summary($ctx, $mset, $q);
-               }
+               return mset_thread($ctx, $mset, $q) if $x eq 't';
+               mset_summary($ctx, $mset, $q); # appends to {-html_tip}
+               $html = '';
        }
-       PublicInbox::WwwStream->response($ctx, $code, $cb);
+       html_oneshot($ctx, $code);
 }
 
 # display non-nested search results similar to what users expect from
@@ -100,7 +95,7 @@ sub mset_summary {
        foreach my $m ($mset->items) {
                my $rank = sprintf("%${pad}d", $m->get_rank + 1);
                my $pct = get_pct($m);
-               my $smsg = PublicInbox::SearchMsg::from_mitem($m, $srch);
+               my $smsg = PublicInbox::Smsg::from_mitem($m, $srch);
                unless ($smsg) {
                        eval {
                                $m = "$m ".$m->get_docid . " expired\n";
@@ -115,14 +110,14 @@ sub mset_summary {
                        obfuscate_addrs($obfs_ibx, $f);
                }
                my $date = PublicInbox::View::fmt_ts($smsg->{ds});
-               my $mid = PublicInbox::Hval->new_msgid($smsg->{mid})->{href};
+               my $mid = mid_href($smsg->{mid});
                $s = '(no subject)' if $s eq '';
                $$res .= qq{$rank. <b><a\nhref="$mid/">}.
                        $s . "</a></b>\n";
                $$res .= "$pfx  - by $f @ $date UTC [$pct%]\n\n";
        }
        $$res .= search_nav_bot($mset, $q);
-       $noop;
+       undef;
 }
 
 # shorten "/full/path/to/Foo/Bar.pm" to "Foo/Bar.pm" so error
@@ -243,8 +238,8 @@ sub search_nav_bot {
 
 sub sort_relevance {
        [ sort {
-               (eval { $b->topmost->{smsg}->{pct} } // 0) <=>
-               (eval { $a->topmost->{smsg}->{pct} } // 0)
+               (eval { $b->topmost->{pct} } // 0) <=>
+               (eval { $a->topmost->{pct} } // 0)
        } @{$_[0]} ]
 }
 
@@ -260,7 +255,7 @@ sub load_msgs {
        my ($mset) = @_;
        [ map {
                my $mi = $_;
-               my $smsg = PublicInbox::SearchMsg::from_mitem($mi);
+               my $smsg = PublicInbox::Smsg::from_mitem($mi);
                $smsg->{pct} = get_pct($mi);
                $smsg;
        } ($mset->items) ]
@@ -268,7 +263,8 @@ sub load_msgs {
 
 sub mset_thread {
        my ($ctx, $mset, $q) = @_;
-       my $msgs = $ctx->{-inbox}->search->retry_reopen(\&load_msgs, $mset);
+       my $ibx = $ctx->{-inbox};
+       my $msgs = $ibx->search->retry_reopen(\&load_msgs, $mset);
        my $r = $q->{r};
        my $rootset = PublicInbox::SearchThread::thread($msgs,
                $r ? \&sort_relevance : \&PublicInbox::View::sort_ds,
@@ -285,22 +281,23 @@ sub mset_thread {
        $ctx->{s_nr} = scalar(@$msgs).'+ results';
 
        # reduce hash lookups in skel_dump
-       $ctx->{-obfuscate} = $ctx->{-inbox}->{obfuscate};
+       $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
        PublicInbox::View::walk_thread($rootset, $ctx,
                \&PublicInbox::View::pre_thread);
 
        @$msgs = reverse @$msgs if $r;
        $ctx->{msgs} = $msgs;
-       \&mset_thread_i;
+       PublicInbox::WwwStream::response($ctx, 200, \&mset_thread_i);
 }
 
 # callback for PublicInbox::WwwStream::getline
 sub mset_thread_i {
-       my ($nr, $ctx) = @_;
+       my ($ctx) = @_;
+       return $ctx->html_top if exists $ctx->{-html_tip};
        my $msgs = $ctx->{msgs} or return;
        while (my $smsg = pop @$msgs) {
-               $ctx->{-inbox}->smsg_mime($smsg) or next;
-               return PublicInbox::View::index_entry($smsg, $ctx,
+               my $eml = $ctx->{-inbox}->smsg_eml($smsg) or next;
+               return PublicInbox::View::eml_entry($ctx, $smsg, $eml,
                                                        scalar @$msgs);
        }
        my ($skel) = delete @$ctx{qw(skel msgs)};
@@ -337,9 +334,9 @@ sub adump_i {
        my ($ctx) = @_;
        while (my $mi = shift @{$ctx->{items}}) {
                my $smsg = eval {
-                       PublicInbox::SearchMsg::from_mitem($mi, $ctx->{srch});
+                       PublicInbox::Smsg::from_mitem($mi, $ctx->{srch});
                } or next;
-               $ctx->{-inbox}->smsg_mime($smsg) and return $smsg;
+               return $smsg;
        }
 }
 
@@ -365,14 +362,10 @@ sub new {
 }
 
 sub qs_html {
-       my ($self, %over) = @_;
+       my ($self, %override) = @_;
 
-       if (keys %over) {
-               my $tmp = bless { %$self }, ref($self);
-               foreach my $k (keys %over) {
-                       $tmp->{$k} = $over{$k};
-               }
-               $self = $tmp;
+       if (scalar(keys(%override))) {
+               $self = bless { (%$self, %override) }, ref($self);
        }
 
        my $q = uri_escape($self->{'q'}, MID_ESC);