Use Promise in searchSecretMessages instead of RequestActor.

This commit is contained in:
levlam 2024-05-11 21:54:21 +03:00
parent cf4cfc3c57
commit 3cca264f8d
3 changed files with 29 additions and 89 deletions

View File

@ -20941,36 +20941,20 @@ td_api::object_ptr<td_api::foundMessages> MessagesManager::get_found_messages_ob
found_messages.next_offset); found_messages.next_offset);
} }
MessagesManager::FoundMessages MessagesManager::offline_search_messages(DialogId dialog_id, const string &query, void MessagesManager::offline_search_messages(DialogId dialog_id, const string &query, string offset, int32 limit,
string offset, int32 limit, MessageSearchFilter filter,
MessageSearchFilter filter, int64 &random_id, Promise<td_api::object_ptr<td_api::foundMessages>> &&promise) {
Promise<Unit> &&promise) {
if (!G()->use_message_database()) { if (!G()->use_message_database()) {
promise.set_error(Status::Error(400, "Message database is required to search messages in secret chats")); return promise.set_error(Status::Error(400, "Message database is required to search messages in secret chats"));
return {};
} }
if (random_id != 0) {
// request has already been sent before
auto it = found_fts_messages_.find(random_id);
CHECK(it != found_fts_messages_.end());
auto result = std::move(it->second);
found_fts_messages_.erase(it);
promise.set_value(Unit());
return result;
}
if (query.empty()) { if (query.empty()) {
promise.set_value(Unit()); return promise.set_value(get_found_messages_object({}, "offline_search_messages"));
return {};
} }
if (dialog_id != DialogId() && !have_dialog_force(dialog_id, "offline_search_messages")) { if (dialog_id != DialogId() && !have_dialog_force(dialog_id, "offline_search_messages")) {
promise.set_error(Status::Error(400, "Chat not found")); return promise.set_error(Status::Error(400, "Chat not found"));
return {};
} }
if (limit <= 0) { if (limit <= 0) {
promise.set_error(Status::Error(400, "Limit must be positive")); return promise.set_error(Status::Error(400, "Limit must be positive"));
return {};
} }
if (limit > MAX_SEARCH_MESSAGES) { if (limit > MAX_SEARCH_MESSAGES) {
limit = MAX_SEARCH_MESSAGES; limit = MAX_SEARCH_MESSAGES;
@ -20983,56 +20967,43 @@ MessagesManager::FoundMessages MessagesManager::offline_search_messages(DialogId
if (!offset.empty()) { if (!offset.empty()) {
auto r_from_search_id = to_integer_safe<int64>(offset); auto r_from_search_id = to_integer_safe<int64>(offset);
if (r_from_search_id.is_error()) { if (r_from_search_id.is_error()) {
promise.set_error(Status::Error(400, "Invalid offset specified")); return promise.set_error(Status::Error(400, "Invalid offset specified"));
return {};
} }
fts_query.from_search_id = r_from_search_id.ok(); fts_query.from_search_id = r_from_search_id.ok();
} }
fts_query.limit = limit; fts_query.limit = limit;
do {
random_id = Random::secure_int64();
} while (random_id == 0 || found_fts_messages_.count(random_id) > 0);
found_fts_messages_[random_id]; // reserve place for result
G()->td_db()->get_message_db_async()->get_messages_fts( G()->td_db()->get_message_db_async()->get_messages_fts(
std::move(fts_query), std::move(fts_query), PromiseCreator::lambda([offset = std::move(offset), limit, promise = std::move(promise)](
PromiseCreator::lambda([random_id, offset = std::move(offset), limit, Result<MessageDbFtsResult> fts_result) mutable {
promise = std::move(promise)](Result<MessageDbFtsResult> fts_result) mutable {
send_closure(G()->messages_manager(), &MessagesManager::on_message_db_fts_result, std::move(fts_result), send_closure(G()->messages_manager(), &MessagesManager::on_message_db_fts_result, std::move(fts_result),
std::move(offset), limit, random_id, std::move(promise)); std::move(offset), limit, std::move(promise));
})); }));
return {};
} }
void MessagesManager::on_message_db_fts_result(Result<MessageDbFtsResult> result, string offset, int32 limit, void MessagesManager::on_message_db_fts_result(Result<MessageDbFtsResult> result, string offset, int32 limit,
int64 random_id, Promise<Unit> &&promise) { Promise<td_api::object_ptr<td_api::foundMessages>> &&promise) {
G()->ignore_result_if_closing(result); G()->ignore_result_if_closing(result);
if (result.is_error()) { if (result.is_error()) {
found_fts_messages_.erase(random_id);
return promise.set_error(result.move_as_error()); return promise.set_error(result.move_as_error());
} }
auto fts_result = result.move_as_ok(); auto fts_result = result.move_as_ok();
auto it = found_fts_messages_.find(random_id); FoundMessages found_messages;
CHECK(it != found_fts_messages_.end()); found_messages.message_full_ids.reserve(fts_result.messages.size());
auto &res = it->second.message_full_ids;
res.reserve(fts_result.messages.size());
for (auto &message : fts_result.messages) { for (auto &message : fts_result.messages) {
auto m = on_get_message_from_database(message, false, "on_message_db_fts_result"); auto m = on_get_message_from_database(message, false, "on_message_db_fts_result");
if (m != nullptr) { if (m != nullptr) {
res.emplace_back(message.dialog_id, m->message_id); found_messages.message_full_ids.emplace_back(message.dialog_id, m->message_id);
} }
} }
it->second.next_offset = fts_result.next_search_id <= 1 ? string() : to_string(fts_result.next_search_id); found_messages.next_offset = fts_result.next_search_id <= 1 ? string() : to_string(fts_result.next_search_id);
it->second.total_count = offset.empty() && fts_result.messages.size() < static_cast<size_t>(limit) found_messages.total_count = offset.empty() && fts_result.messages.size() < static_cast<size_t>(limit)
? static_cast<int32>(fts_result.messages.size()) ? static_cast<int32>(fts_result.messages.size())
: -1; : -1;
promise.set_value(Unit()); promise.set_value(get_found_messages_object(found_messages, "on_message_db_fts_result"));
} }
void MessagesManager::on_message_db_calls_result(Result<MessageDbCallsResult> result, int64 random_id, void MessagesManager::on_message_db_calls_result(Result<MessageDbCallsResult> result, int64 random_id,

View File

@ -730,8 +730,9 @@ class MessagesManager final : public Actor {
td_api::object_ptr<td_api::foundMessages> get_found_messages_object(const FoundMessages &found_messages, td_api::object_ptr<td_api::foundMessages> get_found_messages_object(const FoundMessages &found_messages,
const char *source); const char *source);
FoundMessages offline_search_messages(DialogId dialog_id, const string &query, string offset, int32 limit, void offline_search_messages(DialogId dialog_id, const string &query, string offset, int32 limit,
MessageSearchFilter filter, int64 &random_id, Promise<Unit> &&promise); MessageSearchFilter filter,
Promise<td_api::object_ptr<td_api::foundMessages>> &&promise);
void search_messages(DialogListId dialog_list_id, bool ignore_folder_id, bool broadcasts_only, const string &query, void search_messages(DialogListId dialog_list_id, bool ignore_folder_id, bool broadcasts_only, const string &query,
const string &offset, int32 limit, MessageSearchFilter filter, int32 min_date, int32 max_date, const string &offset, int32 limit, MessageSearchFilter filter, int32 min_date, int32 max_date,
@ -2821,8 +2822,8 @@ class MessagesManager final : public Actor {
int32 limit, Result<vector<MessageDbDialogMessage>> r_messages, int32 limit, Result<vector<MessageDbDialogMessage>> r_messages,
Promise<Unit> promise); Promise<Unit> promise);
void on_message_db_fts_result(Result<MessageDbFtsResult> result, string offset, int32 limit, int64 random_id, void on_message_db_fts_result(Result<MessageDbFtsResult> result, string offset, int32 limit,
Promise<Unit> &&promise); Promise<td_api::object_ptr<td_api::foundMessages>> &&promise);
void on_message_db_calls_result(Result<MessageDbCallsResult> result, int64 random_id, MessageId first_db_message_id, void on_message_db_calls_result(Result<MessageDbCallsResult> result, int64 random_id, MessageId first_db_message_id,
MessageSearchFilter filter, Promise<Unit> &&promise); MessageSearchFilter filter, Promise<Unit> &&promise);
@ -3244,8 +3245,6 @@ class MessagesManager final : public Actor {
FlatHashMap<int64, DialogId> found_dialog_messages_dialog_id_; // random_id -> dialog_id FlatHashMap<int64, DialogId> found_dialog_messages_dialog_id_; // random_id -> dialog_id
FlatHashMap<int64, FoundMessages> found_call_messages_; // random_id -> FoundMessages FlatHashMap<int64, FoundMessages> found_call_messages_; // random_id -> FoundMessages
FlatHashMap<int64, FoundMessages> found_fts_messages_; // random_id -> FoundMessages
struct MessageEmbeddingCodes { struct MessageEmbeddingCodes {
FlatHashMap<MessageId, string, MessageIdHash> embedding_codes_; FlatHashMap<MessageId, string, MessageIdHash> embedding_codes_;
}; };

View File

@ -1467,38 +1467,6 @@ class SearchChatMessagesRequest final : public RequestActor<> {
} }
}; };
class SearchSecretMessagesRequest final : public RequestActor<> {
DialogId dialog_id_;
string query_;
string offset_;
int32 limit_;
MessageSearchFilter filter_;
int64 random_id_;
MessagesManager::FoundMessages found_messages_;
void do_run(Promise<Unit> &&promise) final {
found_messages_ = td_->messages_manager_->offline_search_messages(dialog_id_, query_, offset_, limit_, filter_,
random_id_, std::move(promise));
}
void do_send_result() final {
send_result(td_->messages_manager_->get_found_messages_object(found_messages_, "SearchSecretMessagesRequest"));
}
public:
SearchSecretMessagesRequest(ActorShared<Td> td, uint64 request_id, int64 dialog_id, string query, string offset,
int32 limit, tl_object_ptr<td_api::SearchMessagesFilter> filter)
: RequestActor(std::move(td), request_id)
, dialog_id_(dialog_id)
, query_(std::move(query))
, offset_(std::move(offset))
, limit_(limit)
, filter_(get_message_search_filter(filter))
, random_id_(0) {
}
};
class SearchCallMessagesRequest final : public RequestActor<> { class SearchCallMessagesRequest final : public RequestActor<> {
string offset_; string offset_;
int32 limit_; int32 limit_;
@ -5368,8 +5336,10 @@ void Td::on_request(uint64 id, td_api::searchSecretMessages &request) {
CHECK_IS_USER(); CHECK_IS_USER();
CLEAN_INPUT_STRING(request.query_); CLEAN_INPUT_STRING(request.query_);
CLEAN_INPUT_STRING(request.offset_); CLEAN_INPUT_STRING(request.offset_);
CREATE_REQUEST(SearchSecretMessagesRequest, request.chat_id_, std::move(request.query_), std::move(request.offset_), CREATE_REQUEST_PROMISE();
request.limit_, std::move(request.filter_)); messages_manager_->offline_search_messages(DialogId(request.chat_id_), std::move(request.query_),
std::move(request.offset_), request.limit_,
get_message_search_filter(request.filter_), std::move(promise));
} }
void Td::on_request(uint64 id, td_api::searchMessages &request) { void Td::on_request(uint64 id, td_api::searchMessages &request) {