1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # pretends to be like LeiDedupe and also PublicInbox::Inbox
5 package PublicInbox::LeiSavedSearch;
8 use parent qw(PublicInbox::Lock);
9 use PublicInbox::OverIdx;
10 use PublicInbox::LeiSearch;
11 use PublicInbox::Config;
12 use PublicInbox::Spawn qw(run_die);
13 use PublicInbox::ContentHash qw(git_sha);
14 use Digest::SHA qw(sha256_hex);
16 *squote_maybe = \&PublicInbox::Config::squote_maybe;
18 sub lss_dir_for ($$) {
19 my ($lei, $dstref) = @_;
21 if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized
22 require PublicInbox::URIimap;
23 my $uri = PublicInbox::URIimap->new($$dstref)->canonical;
27 @n = ($$dstref =~ m{([\w\-\.]+)/*\z});
28 $$dstref = $lei->rel2abs($$dstref);
30 push @n, sha256_hex($$dstref);
31 $lei->share_path . '/saved-searches/' . join('-', @n);
35 my ($cls, $lei, $dir) = @_;
36 my $self = bless { ale => $lei->ale }, $cls;
37 if (defined $dir) { # updating existing saved search via "lei up"
38 my $f = "$dir/lei.saved-search";
39 ((-f $f && -r _) || output2lssdir($self, $lei, \$dir, \$f)) or
40 return $lei->fail("$f non-existent or unreadable");
41 $self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
43 } else { # new saved search "lei q --save"
44 my $dst = $lei->{ovv}->{dst};
45 $dir = lss_dir_for($lei, \$dst);
47 File::Path::make_path($dir); # raises on error
49 my $f = $self->{'-f'} = "$dir/lei.saved-search";
50 open my $fh, '>', $f or return $lei->fail("open $f: $!");
51 my $sq_dst = squote_maybe($dst);
52 print $fh <<EOM or return $lei->fail("print $f: $!");
53 ; to refresh with new results, run: lei up $sq_dst
55 close $fh or return $lei->fail("close $f: $!");
56 my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
58 cfg_set($self, '--add', 'lei.q', $_) for @$q;
60 cfg_set($self, 'lei.q', $q);
62 $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
63 cfg_set($self, 'lei.q.output', $dst);
64 for my $k (qw(only include exclude)) {
65 my $ary = $lei->{opt}->{$k} // next;
67 cfg_set($self, '--add', "lei.q.$k", $x);
70 for my $k (qw(external local remote import-remote
71 import-before threads)) {
72 my $val = $lei->{opt}->{$k} // next;
73 cfg_set($self, "lei.q.$k", $val);
76 bless $self->{-cfg}, 'PublicInbox::Config';
77 $self->{lock_path} = "$self->{-f}.flock";
78 $self->{-ovf} = "$dir/over.sqlite3";
82 sub description { $_[0]->{qstr} } # for WWW
85 my ($self, @args) = @_;
86 my $lk = $self->lock_for_scope; # git-config doesn't wait
87 run_die([qw(git config -f), $self->{'-f'}, @args]);
90 # drop-in for LeiDedupe API
92 my ($self, $eml, $smsg) = @_;
93 my $oidx = $self->{oidx} // die 'BUG: no {oidx}';
94 my $blob = $smsg ? $smsg->{blob} : undef;
95 return 1 if $blob && $oidx->blob_exists($blob);
96 my $lk = $self->lock_for_scope_fast;
97 if (my $xoids = PublicInbox::LeiSearch::xoids_for($self, $eml, 1)) {
98 for my $docid (values %$xoids) {
99 $oidx->add_xref3($docid, -1, $blob, '.');
104 # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed
106 $smsg = bless {}, 'PublicInbox::Smsg';
108 $smsg->populate($eml);
111 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
112 $smsg->{blob} //= git_sha(1, $eml)->hexdigest;
113 $oidx->add_overview($eml, $smsg);
114 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
122 $self->{oidx} //= do {
123 my $creat = !-f $self->{-ovf};
124 my $lk = $self->lock_for_scope; # git-config doesn't wait
125 my $oidx = PublicInbox::OverIdx->new($self->{-ovf});
126 $oidx->{-no_fsync} = 1;
129 $oidx->{dbh}->do('PRAGMA journal_mode = WAL');
130 $oidx->eidx_prep; # for xref3
136 sub over { $_[0]->{oidx} } # for xoids_for
138 sub git { $_[0]->{ale}->git }
142 $self->{ale}->git->cleanup;
143 my $oidx = delete($self->{oidx}) // return;
153 # find existing directory containing a `lei.saved-search' file based on
154 # $dir_ref which is an output
156 my ($self, $lei, $dir_ref, $fn_ref) = @_;
157 my $dst = $$dir_ref; # imap://$MAILBOX, /path/to/maildir, /path/to/mbox
158 my $dir = lss_dir_for($lei, \$dst);
159 my $f = "$dir/lei.saved-search";
161 $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
170 *nntp_url = \&cloneurl;
171 *base_url = \&PublicInbox::Inbox::base_url;
172 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
173 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
174 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
175 *modified = \&PublicInbox::Inbox::modified;
176 *recent = \&PublicInbox::Inbox::recent;
177 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
178 *isrch = *search = \&mm; # TODO
179 *DESTROY = \&pause_dedupe;