use PublicInbox::MsgTime qw(msg_datestamp);
use PublicInbox::Hval qw/ascii_html obfuscate_addrs/;
use PublicInbox::Linkify;
-use PublicInbox::MID qw/mid_clean id_compress mid_mime mid_escape mids
- references/;
+use PublicInbox::MID qw/id_compress mid_escape mids references/;
use PublicInbox::MsgIter;
use PublicInbox::Address;
use PublicInbox::WwwStream;
use PublicInbox::Reply;
require POSIX;
+use Time::Local qw(timegm);
use constant INDENT => ' ';
use constant TCHILD => '` ';
my ($ctx) = @_;
my $mid = $ctx->{mid};
my $srch = $ctx->{srch};
- my $sres = $srch->get_thread($mid);
- my $msgs = load_results($srch, $sres);
- my $nr = $sres->{total};
+ my ($nr, $msgs) = $srch->get_thread($mid);
return missing_thread($ctx) if $nr == 0;
my $skel = '<hr><pre>';
$skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
$ctx->{mapping} = {};
$ctx->{s_nr} = "$nr+ messages in thread";
- my $rootset = thread_results($msgs, $srch);
+ my $rootset = thread_results($ctx, $msgs);
# reduce hash lookups in pre_thread->skel_dump
my $inbox = $ctx->{-inbox};
my ($dst, $ctx, $hdr, $tpfx) = @_;
my $srch = $ctx->{srch};
my $mid = mids($hdr)->[0];
- my $sres = $srch->get_thread($mid);
- my $nr = $sres->{total};
+ my ($nr, $msgs) = $srch->get_thread($mid);
my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
qq(|<a\nhref="${tpfx}t/#u">nested</a>] ) .
qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
$ctx->{prev_attr} = '';
$ctx->{prev_level} = 0;
$ctx->{dst} = $dst;
- $sres = load_results($srch, $sres);
# reduce hash lookups in skel_dump
my $ibx = $ctx->{-inbox};
$ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
- walk_thread(thread_results($sres, $srch), $ctx, *skel_dump);
+ walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump);
$ctx->{parent_msg} = $parent;
}
$level ? INDENT x ($level - 1) : '';
}
-sub load_results {
- my ($srch, $sres) = @_;
- my $msgs = delete $sres->{msgs};
- $srch->retry_reopen(sub { [ map { $_->mid; $_ } @$msgs ] });
-}
-
sub thread_results {
- my ($msgs, $srch) = @_;
+ my ($ctx, $msgs) = @_;
require PublicInbox::SearchThread;
- PublicInbox::SearchThread::thread($msgs, *sort_ds, $srch);
+ PublicInbox::SearchThread::thread($msgs, *sort_ds, $ctx->{-inbox});
}
sub missing_thread {
my ($ctx, $level, $node) = @_;
my $srch = $ctx->{srch};
my $mid = $node->{id};
- my $x = $node->{smsg} || $srch->lookup_mail($mid);
+ my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid);
my ($subj, $ds);
my $topic;
if ($x) {
200;
}
+sub ts2str ($) {
+ my ($ts) = @_;
+ POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts));
+}
+
+sub str2ts ($) {
+ my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]);
+ timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
+}
+
+sub pagination_footer ($$) {
+ my ($ctx, $latest) = @_;
+ delete $ctx->{qp} or return;
+ my $next = $ctx->{next_page} || '';
+ my $prev = $ctx->{prev_page} || '';
+ if ($prev) {
+ $next = $next ? "$next " : ' ';
+ $prev .= qq! <a\nhref='$latest'>latest</a>!;
+ }
+ "<hr><pre>page: $next$prev</pre>";
+}
+
sub index_nav { # callback for WwwStream
my (undef, $ctx) = @_;
- delete $ctx->{qp} or return;
- my ($next, $prev);
- $next = $prev = ' ';
- my $latest = '';
+ pagination_footer($ctx, '.')
+}
+
+sub paginate_recent ($) {
+ my ($ctx) = @_;
+ my $t = $ctx->{qp}->{t} || '';
+ my $lim = 200; # this is our window
+ my $opts = { limit => $lim };
+ my ($after, $before);
- my $next_o = $ctx->{-next_o};
- if ($next_o) {
- $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
+ # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers
+ # if only $after exists "YYYYMMDD.." because "." could be skipped
+ # if interpreted as an end-of-sentence
+ $t =~ s/\A(\d{8,14})-// and $after = str2ts($1);
+ $t =~ /\A(\d{8,14})\z/ and $before = str2ts($1);
+
+ my $ibx = $ctx->{-inbox};
+ my $msgs = $ibx->recent($opts, $after, $before);
+ my $nr = scalar @$msgs;
+ if ($nr < $lim && defined($after)) {
+ $after = $before = undef;
+ $msgs = $ibx->recent($opts);
+ $nr = scalar @$msgs;
}
- if (my $cur_o = $ctx->{-cur_o}) {
- $latest = qq! <a\nhref=.>latest</a>!;
-
- my $o = $cur_o - ($next_o - $cur_o);
- if ($o > 0) {
- $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
- } elsif ($o == 0) {
- $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
+ my $more = $nr == $lim;
+ my ($newest, $oldest);
+ if ($nr) {
+ $newest = $msgs->[0]->{ts};
+ $oldest = $msgs->[-1]->{ts};
+ # if we only had $after, our SQL query in ->recent ordered
+ if ($newest < $oldest) {
+ ($oldest, $newest) = ($newest, $oldest);
+ $more = 0 if defined($after) && $after < $oldest;
}
}
- "<hr><pre>page: $next $prev$latest</pre>";
+ if (defined($oldest) && $more) {
+ my $s = ts2str($oldest);
+ $ctx->{next_page} = qq!<a\nhref="?t=$s"\nrel=next>next</a>!;
+ }
+ if (defined($newest) && (defined($before) || defined($after))) {
+ my $s = ts2str($newest);
+ $ctx->{prev_page} = qq!<a\nhref="?t=$s-"\nrel=prev>prev</a>!;
+ }
+ $msgs;
}
sub index_topics {
my ($ctx) = @_;
- my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
- my $opts = { offset => $off, limit => 200 };
-
- $ctx->{order} = [];
- my $srch = $ctx->{srch};
- my $sres = $srch->query('', $opts);
- my $nr = scalar @{$sres->{msgs}};
- if ($nr) {
- $sres = load_results($srch, $sres);
- walk_thread(thread_results($sres, $srch), $ctx, *acc_topic);
+ my $msgs = paginate_recent($ctx);
+ if (@$msgs) {
+ walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic);
}
- $ctx->{-next_o} = $off+ $nr;
- $ctx->{-cur_o} = $off;
PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
}