public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob dd358ae16a3a086c90b8b0dae8a6b00670149247 2202 bytes (raw)
$ git show HEAD:lib/PublicInbox/LeiForgetSearch.pm	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 
# Copyright (C) 2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# "lei forget-search" forget/remove a saved search "lei q --save"
package PublicInbox::LeiForgetSearch;
use strict;
use v5.10.1;
use parent qw(PublicInbox::LeiUp);
use PublicInbox::LeiSavedSearch;
use File::Path ();
use SelectSaver;

sub do_forget_search {
	my ($lei, @outs) = @_;
	my @dirs; # paths in ~/.local/share/lei/saved-search/
	my $cwd;
	for my $o (@outs) {
		my $d = PublicInbox::LeiSavedSearch::lss_dir_for($lei, \$o, 1);
		if (-e $d) {
			push @dirs, $d
		} else { # keep going, like rm(1):
			$cwd //= $lei->rel2abs('.');
			warn "--save was not used with $o cwd=$cwd\n";
		}
	}
	my $save;
	my $opt = { safe => 1 };
	if ($lei->{opt}->{verbose}) {
		$opt->{verbose} = 1;
		$save = SelectSaver->new($lei->{2});
	}
	File::Path::remove_tree(@dirs, $opt);
	$lei->child_error(0) if defined $cwd;
}

sub lei_forget_search {
	my ($lei, @outs) = @_;
	my $prune = $lei->{opt}->{prune};
	$prune // return do_forget_search($lei, @outs);
	return $lei->fail("--prune and @outs incompatible") if @outs;
	my @tmp = PublicInbox::LeiSavedSearch::list($lei);
	my $self = bless { -mail_sync => 1 }, __PACKAGE__;
	$self->filter_lss($lei, $prune) // return
			$lei->fail("only --prune=$prune not understood");
	if ($self->{o_remote}) { # setup lei->{auth}
		$self->prepare_inputs($lei, $self->{o_remote}) or return;
	}
	$lei->wq1_start($self);
}

sub do_prune {
	my ($self) = @_;
	my $lei = $self->{lei};
	for my $o (@{$self->{o_local} // []}) {
		next if -e $o;
		$lei->qerr("# pruning $o");
		eval { do_forget_search($lei, $o) };
		$lei->child_error(0, "E: $@") if $@;
	}
	for my $o (@{$self->{o_remote} // []}) {
		my $uri = PublicInbox::URIimap->new($o);
		next if $lei->{net}->mic_for_folder($uri);
		$lei->qerr("# pruning $uri");
		eval { do_forget_search($lei, $o) };
		$lei->child_error(0, "E: $@") if $@;
	}
}

# called in top-level lei-daemon when LeiAuth is done
sub net_merge_all_done {
	my ($self) = @_;
	$self->wq_do('do_prune');
	$self->wq_close;
}

*_wq_done_wait = \&PublicInbox::LEI::wq_done_wait;
*_complete_forget_search = \&PublicInbox::LeiUp::_complete_up;

1;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git