]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Mbox.pm
msgmap: replace id_batch with ids_after
[public-inbox.git] / lib / PublicInbox / Mbox.pm
index 381bcadad85893bb5b034911a62f374dcadbf6ec..c66ccaa794fd1f5413a545b09bcd3cc5db22944f 100644 (file)
@@ -92,7 +92,7 @@ sub emit_raw {
 }
 
 sub msg_str {
-       my ($ctx, $simple) = @_; # Email::Simple object
+       my ($ctx, $simple, $mid) = @_; # Email::Simple object
        my $header_obj = $simple->header_obj;
 
        # drop potentially confusing headers, ssoma already should've dropped
@@ -102,7 +102,7 @@ sub msg_str {
        }
        my $ibx = $ctx->{-inbox};
        my $base = $ibx->base_url($ctx->{env});
-       my $mid = mid_clean($header_obj->header('Message-ID'));
+       $mid = $ctx->{mid} unless defined $mid;
        $mid = mid_escape($mid);
        my @append = (
                'Archived-At', "<$base$mid/>",
@@ -138,8 +138,12 @@ sub thread_mbox {
        my ($ctx, $srch, $sfx) = @_;
        eval { require IO::Compress::Gzip };
        return sub { need_gzip(@_) } if $@;
-
-       my $cb = sub { $srch->get_thread($ctx->{mid}, @_) };
+       my $prev = 0;
+       my $cb = sub {
+               my $msgs = $srch->get_thread($ctx->{mid}, $prev);
+               $prev = $msgs->[-1]->{num} if scalar(@$msgs);
+               $msgs;
+       };
        PublicInbox::MboxGz->response($ctx, $cb);
 }
 
@@ -160,7 +164,18 @@ sub mbox_all {
 
        eval { require IO::Compress::Gzip };
        return sub { need_gzip(@_) } if $@;
-       my $cb = sub { $ctx->{srch}->query($query, @_) };
+       if ($query eq '') {
+               my $prev = 0;
+               my $cb = sub { $ctx->{-inbox}->mm->ids_after(\$prev) };
+               return PublicInbox::MboxGz->response($ctx, $cb, 'all');
+       }
+       my $opts = { offset => 0 };
+       my $srch = $ctx->{srch};
+       my $cb = sub { # called by MboxGz->getline
+               my $msgs = $srch->query($query, $opts);
+               $opts->{offset} += scalar @$msgs;
+               $msgs;
+       };
        PublicInbox::MboxGz->response($ctx, $cb, 'results-'.$query);
 }
 
@@ -192,7 +207,6 @@ sub new {
                cb => $cb,
                ctx => $ctx,
                msgs => [],
-               opts => { offset => 0 },
        }, $class;
 }
 
@@ -217,15 +231,20 @@ sub set_filename ($$) {
 sub getline {
        my ($self) = @_;
        my $ctx = $self->{ctx} or return;
-       my $res;
        my $ibx = $ctx->{-inbox};
        my $gz = $self->{gz};
+       my $msgs = $self->{msgs};
        do {
                # work on existing result set
-               while (defined(my $smsg = shift @{$self->{msgs}})) {
+               while (defined(my $smsg = shift @$msgs)) {
+                       # ids_after may return integers
+                       ref($smsg) or
+                               $smsg = $ctx->{srch}->{over_ro}->get_art($smsg);
+
                        my $msg = eval { $ibx->msg_by_smsg($smsg) } or next;
                        $msg = Email::Simple->new($msg);
-                       $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg));
+                       $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg,
+                                                               $smsg->mid));
 
                        # use subject of first message as subject
                        if (my $hdr = delete $self->{hdr}) {
@@ -246,12 +265,10 @@ sub getline {
                }
 
                # refill result set
-               $res = $self->{cb}->($self->{opts});
-               $self->{msgs} = $res->{msgs};
-               $res = scalar @{$self->{msgs}};
-               $self->{opts}->{offset} += $res;
-       } while ($res);
+               $msgs = $self->{msgs} = $self->{cb}->();
+       } while (@$msgs);
        $gz->close;
+       # signal that we're done and can return undef next call:
        delete $self->{ctx};
        ${delete $self->{buf}};
 }