From d6b53d61fcc00cd134f63886e32566b514869b0d Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 9 Apr 2023 01:35:42 +0300 Subject: [PATCH] Use request promise in td_api::getChatFolder. --- td/telegram/DialogFilterManager.cpp | 27 +++++++++++++++++++++------ td/telegram/DialogFilterManager.h | 13 ++++++++----- td/telegram/Td.cpp | 21 ++------------------- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/td/telegram/DialogFilterManager.cpp b/td/telegram/DialogFilterManager.cpp index 83ae126f2..0aac02597 100644 --- a/td/telegram/DialogFilterManager.cpp +++ b/td/telegram/DialogFilterManager.cpp @@ -820,6 +820,7 @@ td_api::object_ptr DialogFilterManager::get_chat_folder_obje unknown_dialog_ids.push_back(dialog_id); left_dialog_ids.push_back(dialog_id); } else { + // possible if the chat folder has just been edited from another device LOG(ERROR) << "Can't find " << dialog_id << " from " << dialog_filter_id; unknown_dialog_ids.push_back(dialog_id); } @@ -866,7 +867,7 @@ void DialogFilterManager::on_get_recommended_dialog_filters( if (recommended_dialog_filter.dialog_filter == nullptr) { continue; } - load_dialog_filter(recommended_dialog_filter.dialog_filter.get(), false, mpas.get_promise()); + load_dialog_filter(recommended_dialog_filter.dialog_filter.get(), mpas.get_promise()); recommended_dialog_filter.description = std::move(suggested_filter->description_); filters.push_back(std::move(recommended_dialog_filter)); @@ -897,7 +898,8 @@ void DialogFilterManager::on_load_recommended_dialog_filters( promise.set_value(td_api::make_object(std::move(chat_folders))); } -void DialogFilterManager::load_dialog_filter(DialogFilterId dialog_filter_id, bool force, Promise &&promise) { +void DialogFilterManager::get_dialog_filter(DialogFilterId dialog_filter_id, + Promise> &&promise) { CHECK(!td_->auth_manager_->is_bot()); if (!dialog_filter_id.is_valid()) { return promise.set_error(Status::Error(400, "Invalid chat folder identifier specified")); @@ -905,13 +907,26 @@ void DialogFilterManager::load_dialog_filter(DialogFilterId dialog_filter_id, bo auto dialog_filter = get_dialog_filter(dialog_filter_id); if (dialog_filter == nullptr) { - return promise.set_value(Unit()); + return promise.set_value(nullptr); } - load_dialog_filter(dialog_filter, force, std::move(promise)); + auto load_promise = PromiseCreator::lambda( + [actor_id = actor_id(this), dialog_filter_id, promise = std::move(promise)](Result &&result) mutable { + if (result.is_error()) { + return promise.set_error(result.move_as_error()); + } + send_closure(actor_id, &DialogFilterManager::on_load_dialog_filter, dialog_filter_id, std::move(promise)); + }); + load_dialog_filter(dialog_filter, std::move(load_promise)); } -void DialogFilterManager::load_dialog_filter(const DialogFilter *dialog_filter, bool force, Promise &&promise) { +void DialogFilterManager::on_load_dialog_filter(DialogFilterId dialog_filter_id, + Promise> &&promise) { + TRY_STATUS_PROMISE(promise, G()->close_status()); + promise.set_value(get_chat_folder_object(dialog_filter_id)); +} + +void DialogFilterManager::load_dialog_filter(const DialogFilter *dialog_filter, Promise &&promise) { CHECK(!td_->auth_manager_->is_bot()); vector needed_dialog_ids; dialog_filter->for_each_dialog([&](const InputDialogId &input_dialog_id) { @@ -935,7 +950,7 @@ void DialogFilterManager::load_dialog_filter(const DialogFilter *dialog_filter, } } - if (!input_dialog_ids.empty() && !force) { + if (!input_dialog_ids.empty()) { return load_dialog_filter_dialogs(dialog_filter->get_dialog_filter_id(), std::move(input_dialog_ids), std::move(promise)); } diff --git a/td/telegram/DialogFilterManager.h b/td/telegram/DialogFilterManager.h index 8e3451af3..e5e519bad 100644 --- a/td/telegram/DialogFilterManager.h +++ b/td/telegram/DialogFilterManager.h @@ -59,6 +59,8 @@ class DialogFilterManager final : public Actor { Status add_dialog(DialogFilterId dialog_filter_id, InputDialogId input_dialog_id); + void get_dialog_filter(DialogFilterId dialog_filter_id, Promise> &&promise); + void create_dialog_filter(td_api::object_ptr filter, Promise> &&promise); @@ -74,8 +76,6 @@ class DialogFilterManager final : public Actor { void reorder_dialog_filters(vector dialog_filter_ids, int32 main_dialog_list_position, Promise &&promise); - td_api::object_ptr get_chat_folder_object(DialogFilterId dialog_filter_id); - void create_dialog_filter_invite_link(DialogFilterId dialog_filter_id, string invite_link_name, vector dialog_ids, Promise> promise); @@ -111,8 +111,6 @@ class DialogFilterManager final : public Actor { void get_recommended_dialog_filters(Promise> &&promise); - void load_dialog_filter(DialogFilterId dialog_filter_id, bool force, Promise &&promise); - void load_dialog_filter_dialogs(DialogFilterId dialog_filter_id, vector &&input_dialog_ids, Promise &&promise); @@ -153,6 +151,8 @@ class DialogFilterManager final : public Actor { void synchronize_dialog_filters(); + td_api::object_ptr get_chat_folder_object(DialogFilterId dialog_filter_id); + td_api::object_ptr get_chat_folder_object(const DialogFilter *dialog_filter); void send_update_chat_folders(); @@ -199,7 +199,10 @@ class DialogFilterManager final : public Actor { void on_load_recommended_dialog_filters(Result &&result, vector &&filters, Promise> &&promise); - void load_dialog_filter(const DialogFilter *dialog_filter, bool force, Promise &&promise); + void load_dialog_filter(const DialogFilter *dialog_filter, Promise &&promise); + + void on_load_dialog_filter(DialogFilterId dialog_filter_id, + Promise> &&promise); void on_load_dialog_filter_dialogs(DialogFilterId dialog_filter_id, vector &&dialog_ids, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 7729cbdbd..f48a29ca6 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -727,24 +727,6 @@ class GetChatRequest final : public RequestActor<> { } }; -class GetChatFolderRequest final : public RequestActor<> { - DialogFilterId dialog_filter_id_; - - void do_run(Promise &&promise) final { - td_->dialog_filter_manager_->load_dialog_filter(dialog_filter_id_, get_tries() < 2, std::move(promise)); - } - - void do_send_result() final { - send_result(td_->dialog_filter_manager_->get_chat_folder_object(dialog_filter_id_)); - } - - public: - GetChatFolderRequest(ActorShared td, uint64 request_id, int32 dialog_filter_id) - : RequestActor(std::move(td), request_id), dialog_filter_id_(dialog_filter_id) { - set_tries(3); - } -}; - class SearchUserByPhoneNumberRequest final : public RequestActor<> { string phone_number_; @@ -6116,7 +6098,8 @@ void Td::on_request(uint64 id, const td_api::addChatToList &request) { void Td::on_request(uint64 id, const td_api::getChatFolder &request) { CHECK_IS_USER(); - CREATE_REQUEST(GetChatFolderRequest, request.chat_folder_id_); + CREATE_REQUEST_PROMISE(); + dialog_filter_manager_->get_dialog_filter(DialogFilterId(request.chat_folder_id_), std::move(promise)); } void Td::on_request(uint64 id, const td_api::getRecommendedChatFolders &request) {