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);
}
MessagesManager::FoundMessages MessagesManager::offline_search_messages(DialogId dialog_id, const string &query,
string offset, int32 limit,
MessageSearchFilter filter, int64 &random_id,
Promise<Unit> &&promise) {
void MessagesManager::offline_search_messages(DialogId dialog_id, const string &query, string offset, int32 limit,
MessageSearchFilter filter,
Promise<td_api::object_ptr<td_api::foundMessages>> &&promise) {
if (!G()->use_message_database()) {
promise.set_error(Status::Error(400, "Message database is required to search messages in secret chats"));
return {};
return promise.set_error(Status::Error(400, "Message database is required to search messages in secret chats"));
}
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()) {
promise.set_value(Unit());
return {};
return promise.set_value(get_found_messages_object({}, "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 {};
return promise.set_error(Status::Error(400, "Chat not found"));
}
if (limit <= 0) {
promise.set_error(Status::Error(400, "Limit must be positive"));
return {};
return promise.set_error(Status::Error(400, "Limit must be positive"));
}
if (limit > MAX_SEARCH_MESSAGES) {
limit = MAX_SEARCH_MESSAGES;
@ -20983,56 +20967,43 @@ MessagesManager::FoundMessages MessagesManager::offline_search_messages(DialogId
if (!offset.empty()) {
auto r_from_search_id = to_integer_safe<int64>(offset);
if (r_from_search_id.is_error()) {
promise.set_error(Status::Error(400, "Invalid offset specified"));
return {};
return promise.set_error(Status::Error(400, "Invalid offset specified"));
}
fts_query.from_search_id = r_from_search_id.ok();
}
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(
std::move(fts_query),
PromiseCreator::lambda([random_id, offset = std::move(offset), limit,
promise = std::move(promise)](Result<MessageDbFtsResult> fts_result) mutable {
std::move(fts_query), PromiseCreator::lambda([offset = std::move(offset), limit, promise = std::move(promise)](
Result<MessageDbFtsResult> fts_result) mutable {
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,
int64 random_id, Promise<Unit> &&promise) {
Promise<td_api::object_ptr<td_api::foundMessages>> &&promise) {
G()->ignore_result_if_closing(result);
if (result.is_error()) {
found_fts_messages_.erase(random_id);
return promise.set_error(result.move_as_error());
}
auto fts_result = result.move_as_ok();
auto it = found_fts_messages_.find(random_id);
CHECK(it != found_fts_messages_.end());
auto &res = it->second.message_full_ids;
res.reserve(fts_result.messages.size());
FoundMessages found_messages;
found_messages.message_full_ids.reserve(fts_result.messages.size());
for (auto &message : fts_result.messages) {
auto m = on_get_message_from_database(message, false, "on_message_db_fts_result");
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);
it->second.total_count = offset.empty() && fts_result.messages.size() < static_cast<size_t>(limit)
? static_cast<int32>(fts_result.messages.size())
: -1;
found_messages.next_offset = fts_result.next_search_id <= 1 ? string() : to_string(fts_result.next_search_id);
found_messages.total_count = offset.empty() && fts_result.messages.size() < static_cast<size_t>(limit)
? static_cast<int32>(fts_result.messages.size())
: -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,

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,
const char *source);
FoundMessages offline_search_messages(DialogId dialog_id, const string &query, string offset, int32 limit,
MessageSearchFilter filter, int64 &random_id, Promise<Unit> &&promise);
void offline_search_messages(DialogId dialog_id, const string &query, string offset, int32 limit,
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,
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,
Promise<Unit> promise);
void on_message_db_fts_result(Result<MessageDbFtsResult> result, string offset, int32 limit, int64 random_id,
Promise<Unit> &&promise);
void on_message_db_fts_result(Result<MessageDbFtsResult> result, string offset, int32 limit,
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,
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, FoundMessages> found_call_messages_; // random_id -> FoundMessages
FlatHashMap<int64, FoundMessages> found_fts_messages_; // random_id -> FoundMessages
struct MessageEmbeddingCodes {
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<> {
string offset_;
int32 limit_;
@ -5368,8 +5336,10 @@ void Td::on_request(uint64 id, td_api::searchSecretMessages &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.query_);
CLEAN_INPUT_STRING(request.offset_);
CREATE_REQUEST(SearchSecretMessagesRequest, request.chat_id_, std::move(request.query_), std::move(request.offset_),
request.limit_, std::move(request.filter_));
CREATE_REQUEST_PROMISE();
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) {