unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] lei q: support --thread-id=$MSGID || -T $MSGID
Date: Fri, 12 Apr 2024 02:01:03 +0000	[thread overview]
Message-ID: <20240412020103.2665237-1-e@80x24.org> (raw)

This adds support for the "POST /$INBOX/$MSGID/?x=m?q=..."
added last year to support per-thread searches
764035c83 (www: support POST /$INBOX/$MSGID/?x=m&q=, 2023-03-30)
This only supports instances of public-inbox since 764035c83,
but unfortunately there hasn't been a release since then.
---
 Documentation/lei-q.pod       |  9 +++++++++
 lib/PublicInbox/LEI.pm        |  1 +
 lib/PublicInbox/LeiXSearch.pm | 16 ++++++++++++----
 t/psgi_v2.t                   | 16 ++++++++++++++++
 4 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/Documentation/lei-q.pod b/Documentation/lei-q.pod
index 4476a806..79156750 100644
--- a/Documentation/lei-q.pod
+++ b/Documentation/lei-q.pod
@@ -129,6 +129,15 @@ lei/store unless an MUA unflags it!  (Behavior undecided)
 Caveat: C<-tt> only works on locally-indexed messages at the
 moment, and not on remote (HTTP(S)) endpoints.
 
+=item --thread-id=MSGID
+
+=item -T MSGID
+
+Only search messages in the same thread as the given Message-ID.
+
+For HTTP(S) externals, this only works on instances running
+public-inbox 2.0+ (UNRELEASED).
+
 =item --jobs=QUERY_WORKERS[,WRITE_WORKERS]
 
 =item --jobs=,WRITE_WORKERS
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 7c31ab43..06592358 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -177,6 +177,7 @@ our %CMD = ( # sorted in order of importance/use:
 	'stdin|', # /|\z/ must be first for lone dash
 	@lxs_opt, @net_opt,
 	qw(save! output|mfolder|o=s format|f=s dedupe|d=s threads|t+
+	thread-id|T=s
 	sort|s=s reverse|r offset=i pretty jobs|j=s globoff|g augment|a
 	import-before! lock=s@ rsyncable alert=s@ mua=s verbose|v+
 	shared color! mail-sync!), @c_opt, opt_dash('limit|n=i', '[0-9]+') ],
diff --git a/lib/PublicInbox/LeiXSearch.pm b/lib/PublicInbox/LeiXSearch.pm
index fc95d401..d4f34733 100644
--- a/lib/PublicInbox/LeiXSearch.pm
+++ b/lib/PublicInbox/LeiXSearch.pm
@@ -13,7 +13,7 @@ use File::Temp 0.19 (); # 0.19 for ->newdir
 use File::Spec ();
 use PublicInbox::Search qw(xap_terms);
 use PublicInbox::Spawn qw(popen_rd popen_wr which);
-use PublicInbox::MID qw(mids);
+use PublicInbox::MID qw(mids mid_escape);
 use PublicInbox::Smsg;
 use PublicInbox::Eml;
 use PublicInbox::LEI;
@@ -160,6 +160,8 @@ sub query_one_mset { # for --threads and l2m w/o sort
 	my $can_kw = !!$ibxish->can('msg_keywords');
 	my $threads = $lei->{opt}->{threads} // 0;
 	my $fl = $threads > 1 ? 1 : undef;
+	my $mid = $lei->{opt}->{'thread-id'};
+	$mo->{threadid} = $over->mid2tid($mid) if defined $mid;
 	my $lss = $lei->{lss};
 	my $maxk = "external.$dir.maxuid"; # max of previous, so our min
 	my $min = $lss ? ($lss->{-cfg}->{$maxk} // 0) : 0;
@@ -339,6 +341,12 @@ print STDERR $_;
 	push @$curl, '-s', '-d', '';
 	my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
 	$self->{import_sto} = $lei->{sto} if $lei->{opt}->{'import-remote'};
+	if (defined(my $mid = $opt->{'thread-id'})) {
+		$mid = mid_escape($mid);
+		for my $uri (@$uris) {
+			$uri->path($uri->path.$mid.'/');
+		}
+	}
 	for my $uri (@$uris) {
 		$lei->{-current_url} = $uri->as_string;
 		my $start = time;
@@ -459,7 +467,9 @@ sub concurrency {
 sub start_query ($$) { # always runs in main (lei-daemon) process
 	my ($self, $lei) = @_;
 	local $PublicInbox::LEI::current_lei = $lei;
-	if ($self->{opt_threads} || ($lei->{l2m} && !$self->{opt_sort})) {
+	if ($lei->{opt}->{threads} ||
+			defined($lei->{opt}->{'thread-id'}) ||
+			($lei->{l2m} && !$lei->{opt}->{'sort'})) {
 		for my $ibxish (locals($self)) {
 			$self->wq_io_do('query_one_mset', [], $ibxish);
 		}
@@ -546,8 +556,6 @@ sub do_query {
 	my $op_c = delete $lei->{pkt_op_c};
 	delete $lei->{pkt_op_p};
 	@$end = ();
-	$self->{opt_threads} = $lei->{opt}->{threads};
-	$self->{opt_sort} = $lei->{opt}->{'sort'};
 	$self->{-do_lcat} = !!(delete $lei->{lcat_todo});
 	if ($l2m) {
 		$l2m->net_merge_all_done($lei) unless $lei->{auth};
diff --git a/t/psgi_v2.t b/t/psgi_v2.t
index 54faae9b..56a6ae8e 100644
--- a/t/psgi_v2.t
+++ b/t/psgi_v2.t
@@ -101,6 +101,19 @@ EOM
 	}
 };
 
+my $test_lei_q_threadid = sub {
+	my ($u) = @_;
+	test_lei(sub {
+		lei_ok qw(q -f text --only), $u, qw(-T t@1 s:unrelated);
+		is $lei_out, '', 'no results on unlrelated thread';
+		lei_ok qw(q -f text --only), $u, qw(-T t@1 dt:19931002000300..);
+		my @m = ($lei_out =~ m!^Message-ID: <([^>]+)>\n!gms);
+		is_deeply \@m, ['t@3'], 'got expected result from -T MSGID';
+	});
+};
+
+$test_lei_q_threadid->($m2t->{inboxdir});
+
 my $cfgpath = "$ibx->{inboxdir}/pi_config";
 {
 	open my $fh, '>', $cfgpath or BAIL_OUT $!;
@@ -374,6 +387,9 @@ my $client3 = sub {
 
 	$res = $cb->(POST("/m2t/t\@1/?q=s:unrelated&x=m"));
 	is($res->code, 404, '404 on cross-thread search');
+
+	my $rmt = $ENV{PLACK_TEST_EXTERNALSERVER_URI};
+	$rmt and $test_lei_q_threadid->("$rmt/m2t/");
 };
 test_psgi(sub { $www->call(@_) }, $client3);
 test_httpd($env, $client3, 4);

             reply	other threads:[~2024-04-12  2:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-12  2:01 Eric Wong [this message]
2024-04-12  8:03 ` [PATCH] lei q: support --thread-id=$MSGID || -T $MSGID Štěpán Němec
2024-04-12  9:43   ` Eric Wong

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=20240412020103.2665237-1-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.
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).