From 0989a80a5727d628b4b15518fe94e7459a8f1a06 Mon Sep 17 00:00:00 2001
From: 23rd <23rd@vivaldi.net>
Date: Fri, 17 Jan 2025 23:40:32 +0300
Subject: [PATCH] Added ability to provide top msg id to api message search
 class.

---
 .../SourceFiles/api/api_messages_search.cpp     |  3 ++-
 Telegram/SourceFiles/api/api_messages_search.h  |  1 +
 .../api/api_messages_search_merged.cpp          |  4 ++++
 .../api/api_messages_search_merged.h            |  1 +
 .../controls/history_view_compose_search.cpp    | 17 ++++++++++++++++-
 .../view/controls/history_view_compose_search.h |  2 ++
 6 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/Telegram/SourceFiles/api/api_messages_search.cpp b/Telegram/SourceFiles/api/api_messages_search.cpp
index 4fbcaebf0..8bc7a4e3d 100644
--- a/Telegram/SourceFiles/api/api_messages_search.cpp
+++ b/Telegram/SourceFiles/api/api_messages_search.cpp
@@ -103,6 +103,7 @@ void MessagesSearch::searchRequest() {
 		_requestId = _history->session().api().request(MTPmessages_Search(
 			MTP_flags((fromPeer ? Flag::f_from_id : Flag())
 				| (savedPeer ? Flag::f_saved_peer_id : Flag())
+				| (_request.topMsgId ? Flag::f_top_msg_id : Flag())
 				| (_request.tags.empty() ? Flag() : Flag::f_saved_reaction)),
 			_history->peer->input,
 			MTP_string(_request.query),
@@ -111,7 +112,7 @@ void MessagesSearch::searchRequest() {
 			MTP_vector_from_range(_request.tags | ranges::views::transform(
 				Data::ReactionToMTP
 			)),
-			MTPint(), // top_msg_id
+			MTP_int(_request.topMsgId), // top_msg_id
 			MTP_inputMessagesFilterEmpty(),
 			MTP_int(0), // min_date
 			MTP_int(0), // max_date
diff --git a/Telegram/SourceFiles/api/api_messages_search.h b/Telegram/SourceFiles/api/api_messages_search.h
index 76046aaa9..b650b8799 100644
--- a/Telegram/SourceFiles/api/api_messages_search.h
+++ b/Telegram/SourceFiles/api/api_messages_search.h
@@ -32,6 +32,7 @@ public:
 		QString query;
 		PeerData *from = nullptr;
 		std::vector<Data::ReactionId> tags;
+		MsgId topMsgId;
 
 		friend inline bool operator==(
 			const Request &,
diff --git a/Telegram/SourceFiles/api/api_messages_search_merged.cpp b/Telegram/SourceFiles/api/api_messages_search_merged.cpp
index 1990cfd21..5a26e4386 100644
--- a/Telegram/SourceFiles/api/api_messages_search_merged.cpp
+++ b/Telegram/SourceFiles/api/api_messages_search_merged.cpp
@@ -64,6 +64,10 @@ MessagesSearchMerged::MessagesSearchMerged(not_null<History*> history)
 	}
 }
 
+void MessagesSearchMerged::disableMigrated() {
+	_migratedSearch = std::nullopt;
+}
+
 void MessagesSearchMerged::addFound(const FoundMessages &data) {
 	for (const auto &message : data.messages) {
 		_concatedFound.messages.push_back(message);
diff --git a/Telegram/SourceFiles/api/api_messages_search_merged.h b/Telegram/SourceFiles/api/api_messages_search_merged.h
index 1e7c0493b..1a949c6ed 100644
--- a/Telegram/SourceFiles/api/api_messages_search_merged.h
+++ b/Telegram/SourceFiles/api/api_messages_search_merged.h
@@ -29,6 +29,7 @@ public:
 	void clear();
 	void search(const Request &search);
 	void searchMore();
+	void disableMigrated();
 
 	[[nodiscard]] const FoundMessages &messages() const;
 	[[nodiscard]] const Request &request() const;
diff --git a/Telegram/SourceFiles/history/view/controls/history_view_compose_search.cpp b/Telegram/SourceFiles/history/view/controls/history_view_compose_search.cpp
index 00066a59f..98064d020 100644
--- a/Telegram/SourceFiles/history/view/controls/history_view_compose_search.cpp
+++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_search.cpp
@@ -840,6 +840,7 @@ public:
 	void hideAnimated();
 	void setInnerFocus();
 	void setQuery(const QString &query);
+	void setTopMsgId(MsgId topMsgId);
 
 	[[nodiscard]] rpl::producer<Activation> activations() const;
 	[[nodiscard]] rpl::producer<> destroyRequests() const;
@@ -865,6 +866,8 @@ private:
 		rpl::event_stream<BottomBar::Index> jumps;
 	} _pendingJump;
 
+	MsgId _topMsgId;
+
 	rpl::event_stream<Activation> _activations;
 	rpl::event_stream<> _destroyRequests;
 
@@ -901,12 +904,13 @@ ComposeSearch::Inner::Inner(
 	}, _topBar->lifetime());
 
 	_topBar->searchRequests(
-	) | rpl::start_with_next([=](const SearchRequest &search) {
+	) | rpl::start_with_next([=](SearchRequest search) {
 		if (search.query.isEmpty() && search.tags.empty()) {
 			if (!search.from || _history->peer->isSelf()) {
 				return;
 			}
 		}
+		search.topMsgId = _topMsgId;
 		_apiSearch.clear();
 		_apiSearch.search(search);
 	}, _topBar->lifetime());
@@ -1045,6 +1049,13 @@ void ComposeSearch::Inner::setQuery(const QString &query) {
 	_topBar->setQuery(query);
 }
 
+void ComposeSearch::Inner::setTopMsgId(MsgId topMsgId) {
+	if (topMsgId) {
+		_apiSearch.disableMigrated();
+	}
+	_topMsgId = topMsgId;
+}
+
 void ComposeSearch::Inner::showAnimated() {
 	// Don't animate bottom bar.
 	_bottomBar->show();
@@ -1103,6 +1114,10 @@ void ComposeSearch::setQuery(const QString &query) {
 	_inner->setQuery(query);
 }
 
+void ComposeSearch::setTopMsgId(MsgId topMsgId) {
+	_inner->setTopMsgId(topMsgId);
+}
+
 rpl::producer<ComposeSearch::Activation> ComposeSearch::activations() const {
 	return _inner->activations();
 }
diff --git a/Telegram/SourceFiles/history/view/controls/history_view_compose_search.h b/Telegram/SourceFiles/history/view/controls/history_view_compose_search.h
index 834f82d68..dc8c3140d 100644
--- a/Telegram/SourceFiles/history/view/controls/history_view_compose_search.h
+++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_search.h
@@ -33,6 +33,8 @@ public:
 	void setInnerFocus();
 	void setQuery(const QString &query);
 
+	void setTopMsgId(MsgId topMsgId);
+
 	struct Activation {
 		not_null<HistoryItem*> item;
 		QString query;