]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei: implicitly watch all Maildirs it knows about
authorEric Wong <e@80x24.org>
Thu, 19 Aug 2021 09:49:34 +0000 (09:49 +0000)
committerEric Wong <e@80x24.org>
Sat, 21 Aug 2021 20:52:46 +0000 (20:52 +0000)
This allows MUA-made flag changes to Maildirs to be instantly
read and acknowledged for future search results.

In the future, it may be used to speed up --augment and
--import-before (the default) with with "lei q".

lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiMailSync.pm
lib/PublicInbox/LeiNoteEvent.pm
lib/PublicInbox/LeiStore.pm

index 79dc9bf9972c7a23e89b9c0bd9fc0af87c09699e..4d5c61fe62bb6560c984900da0376416615c25b6 100644 (file)
@@ -1370,6 +1370,14 @@ sub cancel_maildir_watch ($$) {
        for my $x (@{$w // []}) { $x->cancel }
 }
 
+sub add_maildir_watch ($$) {
+       my ($d, $cfg_f) = @_;
+       if (!exists($MDIR2CFGPATH->{$d}->{$cfg_f})) {
+               my @w = $dir_idle->add_watches(["$d/cur", "$d/new"], 1);
+               push @{$MDIR2CFGPATH->{$d}->{$cfg_f}}, @w if @w;
+       }
+}
+
 sub refresh_watches {
        my ($lei) = @_;
        my $cfg = _lei_cfg($lei) or return;
@@ -1380,7 +1388,7 @@ sub refresh_watches {
        for my $w (grep(/\Awatch\..+\.state\z/, keys %$cfg)) {
                my $url = substr($w, length('watch.'), -length('.state'));
                require PublicInbox::LeiWatch;
-               my $lw = $watches->{$url} //= PublicInbox::LeiWatch->new($url);
+               $watches->{$url} //= PublicInbox::LeiWatch->new($url);
                $seen{$url} = undef;
                my $state = $cfg->get_1("watch.$url", 'state');
                if (!watch_state_ok($state)) {
@@ -1391,16 +1399,36 @@ sub refresh_watches {
                        my $d = canonpath_harder($1);
                        if ($state eq 'pause') {
                                cancel_maildir_watch($d, $cfg_f);
-                       } elsif (!exists($MDIR2CFGPATH->{$d}->{$cfg_f})) {
-                               my @w = $dir_idle->add_watches(
-                                               ["$d/cur", "$d/new"], 1);
-                               push @{$MDIR2CFGPATH->{$d}->{$cfg_f}}, @w if @w;
+                       } else {
+                               add_maildir_watch($d, $cfg_f);
                        }
                } else { # TODO: imap/nntp/jmap
-                       $lei->child_error(1,
-                               "E: watch $url not supported, yet");
+                       $lei->child_error(1, "E: watch $url not supported, yet")
+               }
+       }
+
+       # add all known Maildir folders as implicit watches
+       my $sto = $lei->_lei_store;
+       my $renames = 0;
+       if (my $lms = $sto ? $sto->search->lms : undef) {
+               for my $d ($lms->folders('maildir:')) {
+                       substr($d, 0, length('maildir:')) = '';
+                       my $cd = canonpath_harder($d);
+                       my $f = "maildir:$cd";
+
+                       # fixup old bugs while we're iterating:
+                       if ($d ne $cd) {
+                               $sto->ipc_do('lms_rename_folder',
+                                               "maildir:$d", $f);
+                               ++$renames;
+                       }
+                       next if $watches->{$f}; # may be set to pause
+                       $watches->{$f} = PublicInbox::LeiWatch->new($f);
+                       $seen{$f} = undef;
+                       add_maildir_watch($cd, $cfg_f);
                }
        }
+       my $wait = $renames ? $sto->ipc_do('done') : undef;
        if ($old) { # cull old non-existent entries
                for my $url (keys %$old) {
                        next if exists $seen{$url};
index 6dfa03be2feaf2063d3681626a869271423f36fa..80e1bb9ddfa4343c54fbf5e7ca3a5c7e05854492 100644 (file)
@@ -412,6 +412,16 @@ sub forget_folder {
        $dbh->do('DELETE FROM folders WHERE fid = ?', undef, $fid);
 }
 
+# only used for changing canonicalization errors
+sub rename_folder {
+       my ($self, $old, $new) = @_;
+       my $fid = delete($self->{fmap}->{$old}) //
+               fid_for($self, $old) // return;
+       $self->{dbh}->do(<<EOM, undef, $new, $fid);
+UPDATE folders SET loc = ? WHERE fid = ?
+EOM
+}
+
 sub imap_oidbin ($$$) {
        my ($self, $url, $uid) = @_; # $url MUST have UIDVALIDITY
        my $fid = $self->{fmap}->{$url} //= fid_for($self, $url) // return;
index d6511cf6cd1bb750d9bfe33d874c243510dae68b..1cd1529640f8c80b82b6b09bef4914397192df71 100644 (file)
@@ -74,7 +74,7 @@ sub lei_note_event {
        my $err = $lms->arg2folder($lei, [ $folder ]);
        return if $err->{fail};
        undef $lms;
-       my $state = $cfg->get_1("watch.$folder", 'state') // 'pause';
+       my $state = $cfg->get_1("watch.$folder", 'state') // 'tag-rw';
        return if $state eq 'pause';
        $lei->ale; # prepare
        $sto->write_prepare($lei);
index e8334187021b477ffeb42f27d517744e0dd4483f..bbd853e5a2a40a4523fa13068cb4a4652d3679e1 100644 (file)
@@ -222,6 +222,11 @@ sub lms_forget_folders {
        for my $f (@folders) { $lms->forget_folder($f) }
 }
 
+sub lms_rename_folder {
+       my ($self, $old, $new) = @_;
+       _lms_rw($self)->rename_folder($old, $new);
+}
+
 sub set_sync_info {
        my ($self, $oidhex, $folder, $id) = @_;
        _lms_rw($self)->set_src($oidhex, $folder, $id);