user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 3/3] lei forget-mail-sync: new command to drop sync information
Date: Tue, 25 May 2021 11:01:36 +0000	[thread overview]
Message-ID: <20210525110136.6223-4-e@80x24.org> (raw)
In-Reply-To: <20210525110136.6223-1-e@80x24.org>

Sometimes a user stops caring to sync an IMAP or Maildir
folder, or wants to force a resync.  Let them run this
command to have lei forget all the sync information about
the mail folder.

This won't delete any stored messages in git, but will
leave "lei index" users with dangling references.
---
 MANIFEST                             |  1 +
 lib/PublicInbox/LEI.pm               |  3 ++-
 lib/PublicInbox/LeiForgetMailSync.pm | 30 ++++++++++++++++++++++++++++
 lib/PublicInbox/LeiMailSync.pm       | 11 ++++++++++
 t/lei-import-imap.t                  |  4 ++++
 5 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 lib/PublicInbox/LeiForgetMailSync.pm

diff --git a/MANIFEST b/MANIFEST
index 2d1ad5c3..23423e0b 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -204,6 +204,7 @@ lib/PublicInbox/LeiDedupe.pm
 lib/PublicInbox/LeiEditSearch.pm
 lib/PublicInbox/LeiExportKw.pm
 lib/PublicInbox/LeiExternal.pm
+lib/PublicInbox/LeiForgetMailSync.pm
 lib/PublicInbox/LeiForgetSearch.pm
 lib/PublicInbox/LeiHelp.pm
 lib/PublicInbox/LeiImport.pm
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 628908b5..c8d2f315 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -243,7 +243,8 @@ our %CMD = ( # sorted in order of importance/use:
 	qw(stdin| offset=i recursive|r exclude=s include|I=s
 	lock=s@ in-format|F=s kw! verbose|v+ incremental! mail-sync!),
 	qw(no-torsocks torsocks=s), PublicInbox::LeiQuery::curl_opt(), @c_opt ],
-
+'forget-mail-sync' => [ 'LOCATION...',
+	'forget sync information for a mail folder', @c_opt ],
 'export-kw' => [ 'LOCATION...|--all',
 	'one-time export of keywords of sync sources',
 	qw(all:s mode=s), @c_opt ],
diff --git a/lib/PublicInbox/LeiForgetMailSync.pm b/lib/PublicInbox/LeiForgetMailSync.pm
new file mode 100644
index 00000000..46dde1a7
--- /dev/null
+++ b/lib/PublicInbox/LeiForgetMailSync.pm
@@ -0,0 +1,30 @@
+# Copyright (C) 2021 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# "lei forget-mail-sync" drop synchronization information
+# TODO: figure out what to do about "lei index" users having
+# dangling references.  Perhaps just documenting "lei index"
+# use being incompatible with "forget-mail-sync" use is
+# sufficient.
+
+package PublicInbox::LeiForgetMailSync;
+use strict;
+use v5.10.1;
+use PublicInbox::LeiExportKw;
+
+sub lei_forget_mail_sync {
+	my ($lei, @folders) = @_;
+	my $sto = $lei->_lei_store or return;
+	my $lms = $sto->search->lms or return;
+	my $err = $lms->arg2folder($lei, \@folders);
+	$lei->qerr(@{$err->{qerr}}) if $err->{qerr};
+	return $lei->fail($err->{fail}) if $err->{fail};
+	delete $lms->{dbh};
+	$lms->lms_begin;
+	$lms->forget_folder($_) for @folders;
+	$lms->lms_commit;
+}
+
+*_complete_forget_mail_sync = \&PublicInbox::LeiExportKw::_complete_export_kw;
+
+1;
diff --git a/lib/PublicInbox/LeiMailSync.pm b/lib/PublicInbox/LeiMailSync.pm
index 094cf1fd..d9c30580 100644
--- a/lib/PublicInbox/LeiMailSync.pm
+++ b/lib/PublicInbox/LeiMailSync.pm
@@ -345,4 +345,15 @@ EOF
 	$err;
 }
 
+sub forget_folder {
+	my ($self, $folder) = @_;
+	my ($fid, $sth);
+	$fid = delete($self->{fmap}->{$folder}) //
+		_fid_for($self, $folder) // return;
+	my $dbh = $self->{dbh};
+	$dbh->do('DELETE FROM blob2name WHERE fid = ?', undef, $fid);
+	$dbh->do('DELETE FROM blob2num WHERE fid = ?', undef, $fid);
+	$dbh->do('DELETE FROM folders WHERE fid = ?', undef, $fid);
+}
+
 1;
diff --git a/t/lei-import-imap.t b/t/lei-import-imap.t
index d3935c82..5283cc23 100644
--- a/t/lei-import-imap.t
+++ b/t/lei-import-imap.t
@@ -71,5 +71,9 @@ test_lei({ tmpdir => $tmpdir }, sub {
 	like($lei_out, qr!^Content-Type: multipart/mixed;!sm, 'got full blob');
 	lei_ok('blob', "$psgi_attach:2");
 	is($lei_out, "b64\xde\xad\xbe\xef\n", 'got attachment');
+
+	lei_ok 'forget-mail-sync', $url;
+	lei_ok 'ls-mail-sync';
+	unlike($lei_out, qr!\Q$host_port\E!, 'sync info gone after forget');
 });
 done_testing;

      parent reply	other threads:[~2021-05-25 11:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-25 11:01 [PATCH 0/3] lei forget-mail-sync: drop sync information Eric Wong
2021-05-25 11:01 ` [PATCH 1/3] lei export-kw: use lei->abs_path instead of rel2abs Eric Wong
2021-05-25 11:01 ` [PATCH 2/3] lei_mail_sync: args2folder: common folder lookup sub Eric Wong
2021-05-25 11:01 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210525110136.6223-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).