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 sub lss_dir_for ($$) {
17 my ($lei, $dstref) = @_;
19 if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized
20 require PublicInbox::URIimap;
21 my $uri = PublicInbox::URIimap->new($$dstref)->canonical;
25 @n = ($$dstref =~ m{([\w\-\.]+)/*\z});
26 $$dstref = $lei->rel2abs($$dstref);
28 push @n, sha256_hex($$dstref);
29 $lei->share_path . '/saved-searches/' . join('-', @n);
33 my ($cls, $lei, $dir) = @_;
34 my $self = bless { ale => $lei->ale }, $cls;
35 if (defined $dir) { # updating existing saved search via "lei up"
36 my $f = "$dir/lei.saved-search";
37 ((-f $f && -r _) || output2lssdir($self, $lei, \$dir, \$f)) or
38 return $lei->fail("$f non-existent or unreadable");
39 $self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
41 } else { # new saved search "lei q --save"
42 my $dst = $lei->{ovv}->{dst};
43 $dir = lss_dir_for($lei, \$dst);
45 File::Path::make_path($dir); # raises on error
47 $self->{'-f'} = "$dir/lei.saved-search";
48 my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
50 cfg_set($self, '--add', 'lei.q', $_) for @$q;
52 cfg_set($self, 'lei.q', $q);
54 my $fmt = $lei->{opt}->{'format'};
55 cfg_set($self, 'lei.q.format', $fmt) if defined $fmt;
56 $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
57 cfg_set($self, 'lei.q.output', $dst);
58 for my $k (qw(only include exclude)) {
59 my $ary = $lei->{opt}->{$k} // next;
61 cfg_set($self, '--add', "lei.q.$k", $x);
64 for my $k (qw(external local remote import-remote
65 import-before threads)) {
66 my $val = $lei->{opt}->{$k} // next;
67 cfg_set($self, "lei.q.$k", $val);
70 bless $self->{-cfg}, 'PublicInbox::Config';
71 $self->{lock_path} = "$self->{-f}.flock";
72 $self->{-ovf} = "$dir/over.sqlite3";
76 sub description { $_[0]->{qstr} } # for WWW
79 my ($self, @args) = @_;
80 my $lk = $self->lock_for_scope; # git-config doesn't wait
81 run_die([qw(git config -f), $self->{'-f'}, @args]);
84 # drop-in for LeiDedupe API
86 my ($self, $eml, $smsg) = @_;
87 my $oidx = $self->{oidx} // die 'BUG: no {oidx}';
88 my $blob = $smsg ? $smsg->{blob} : undef;
89 return 1 if $blob && $oidx->blob_exists($blob);
90 my $lk = $self->lock_for_scope_fast;
91 if (my $xoids = PublicInbox::LeiSearch::xoids_for($self, $eml, 1)) {
92 for my $docid (values %$xoids) {
93 $oidx->add_xref3($docid, -1, $blob, '.');
98 # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed
100 $smsg = bless {}, 'PublicInbox::Smsg';
102 $smsg->populate($eml);
105 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
106 $smsg->{blob} //= git_sha(1, $eml)->hexdigest;
107 $oidx->add_overview($eml, $smsg);
108 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
116 $self->{oidx} //= do {
117 my $creat = !-f $self->{-ovf};
118 my $lk = $self->lock_for_scope; # git-config doesn't wait
119 my $oidx = PublicInbox::OverIdx->new($self->{-ovf});
120 $oidx->{-no_fsync} = 1;
123 $oidx->{dbh}->do('PRAGMA journal_mode = WAL');
124 $oidx->eidx_prep; # for xref3
130 sub over { $_[0]->{oidx} } # for xoids_for
132 sub git { $_[0]->{ale}->git }
136 $self->{ale}->git->cleanup;
137 my $oidx = delete($self->{oidx}) // return;
147 # find existing directory containing a `lei.saved-search' file based on
148 # $dir_ref which is an output
150 my ($self, $lei, $dir_ref, $fn_ref) = @_;
151 my $dst = $$dir_ref; # imap://$MAILBOX, /path/to/maildir, /path/to/mbox
152 my $dir = lss_dir_for($lei, \$dst);
153 my $f = "$dir/lei.saved-search";
155 $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
164 *nntp_url = \&cloneurl;
165 *base_url = \&PublicInbox::Inbox::base_url;
166 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
167 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
168 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
169 *modified = \&PublicInbox::Inbox::modified;
170 *recent = \&PublicInbox::Inbox::recent;
171 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
172 *isrch = *search = \&mm; # TODO
173 *DESTROY = \&pause_dedupe;