From 628a490e2a4ed69acd1c8424f2eb103495391784 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 9 Oct 2021 22:05:23 +0300 Subject: [PATCH] Use request promise in getBlockedMessageSenders. --- td/telegram/MessagesManager.cpp | 88 +++++++++++---------------------- td/telegram/MessagesManager.h | 13 ++--- td/telegram/Td.cpp | 28 +---------- 3 files changed, 36 insertions(+), 93 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index ad5daefaf..ca8ac228d 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -900,19 +900,18 @@ class GetCommonDialogsQuery final : public Td::ResultHandler { }; class GetBlockedDialogsQuery final : public Td::ResultHandler { - Promise promise_; + Promise> promise_; int32 offset_; int32 limit_; - int64 random_id_; public: - explicit GetBlockedDialogsQuery(Promise &&promise) : promise_(std::move(promise)) { + explicit GetBlockedDialogsQuery(Promise> &&promise) + : promise_(std::move(promise)) { } - void send(int32 offset, int32 limit, int64 random_id) { + void send(int32 offset, int32 limit) { offset_ = offset; limit_ = limit; - random_id_ = random_id; send_query(G()->net_query_creator().create(telegram_api::contacts_getBlocked(offset, limit))); } @@ -932,9 +931,9 @@ class GetBlockedDialogsQuery final : public Td::ResultHandler { td->contacts_manager_->on_get_users(std::move(blocked_peers->users_), "GetBlockedDialogsQuery"); td->contacts_manager_->on_get_chats(std::move(blocked_peers->chats_), "GetBlockedDialogsQuery"); - td->messages_manager_->on_get_blocked_dialogs(offset_, limit_, random_id_, + td->messages_manager_->on_get_blocked_dialogs(offset_, limit_, narrow_cast(blocked_peers->blocked_.size()), - std::move(blocked_peers->blocked_)); + std::move(blocked_peers->blocked_), std::move(promise_)); break; } case telegram_api::contacts_blockedSlice::ID: { @@ -942,19 +941,16 @@ class GetBlockedDialogsQuery final : public Td::ResultHandler { td->contacts_manager_->on_get_users(std::move(blocked_peers->users_), "GetBlockedDialogsQuery"); td->contacts_manager_->on_get_chats(std::move(blocked_peers->chats_), "GetBlockedDialogsQuery"); - td->messages_manager_->on_get_blocked_dialogs(offset_, limit_, random_id_, blocked_peers->count_, - std::move(blocked_peers->blocked_)); + td->messages_manager_->on_get_blocked_dialogs(offset_, limit_, blocked_peers->count_, + std::move(blocked_peers->blocked_), std::move(promise_)); break; } default: UNREACHABLE(); } - - promise_.set_value(Unit()); } void on_error(uint64 id, Status status) final { - td->messages_manager_->on_failed_get_blocked_dialogs(random_id_); promise_.set_error(std::move(status)); } }; @@ -16724,78 +16720,54 @@ void MessagesManager::block_message_sender_from_replies_on_server(MessageId mess ->send(message_id, delete_message, delete_all_messages, report_spam); } -std::pair> MessagesManager::get_blocked_dialogs(int32 offset, int32 limit, int64 &random_id, - Promise &&promise) { - LOG(INFO) << "Get blocked chats with offset = " << offset << " and limit = " << limit; - - if (random_id != 0) { - // request has already been sent before - auto it = found_blocked_dialogs_.find(random_id); - CHECK(it != found_blocked_dialogs_.end()); - auto result = std::move(it->second); - found_blocked_dialogs_.erase(it); - - promise.set_value(Unit()); - return result; - } - +void MessagesManager::get_blocked_dialogs(int32 offset, int32 limit, + Promise> &&promise) { if (offset < 0) { - promise.set_error(Status::Error(400, "Parameter offset must be non-negative")); - return {}; + return promise.set_error(Status::Error(400, "Parameter offset must be non-negative")); } if (limit <= 0) { - promise.set_error(Status::Error(400, "Parameter limit must be positive")); - return {}; + return promise.set_error(Status::Error(400, "Parameter limit must be positive")); } - do { - random_id = Random::secure_int64(); - } while (random_id == 0 || found_blocked_dialogs_.find(random_id) != found_blocked_dialogs_.end()); - found_blocked_dialogs_[random_id]; // reserve place for result - - td_->create_handler(std::move(promise))->send(offset, limit, random_id); - return {}; + td_->create_handler(std::move(promise))->send(offset, limit); } -void MessagesManager::on_get_blocked_dialogs(int32 offset, int32 limit, int64 random_id, int32 total_count, - vector> &&blocked_peers) { +void MessagesManager::on_get_blocked_dialogs(int32 offset, int32 limit, int32 total_count, + vector> &&blocked_peers, + Promise> &&promise) { LOG(INFO) << "Receive " << blocked_peers.size() << " blocked chats from offset " << offset << " out of " << total_count; - auto it = found_blocked_dialogs_.find(random_id); - CHECK(it != found_blocked_dialogs_.end()); - - auto &result = it->second.second; - CHECK(result.empty()); + vector dialog_ids; for (auto &blocked_peer : blocked_peers) { CHECK(blocked_peer != nullptr); DialogId dialog_id(blocked_peer->peer_id_); if (dialog_id.get_type() == DialogType::User) { if (td_->contacts_manager_->have_user(dialog_id.get_user_id())) { - result.push_back(dialog_id); + dialog_ids.push_back(dialog_id); } else { LOG(ERROR) << "Have no info about " << dialog_id.get_user_id(); } } else { - force_create_dialog(dialog_id, "on_get_blocked_dialogs"); - if (have_dialog(dialog_id)) { - result.push_back(dialog_id); + if (have_dialog_info(dialog_id)) { + force_create_dialog(dialog_id, "on_get_blocked_dialogs"); + if (have_dialog(dialog_id)) { + dialog_ids.push_back(dialog_id); + } } else { LOG(ERROR) << "Have no info about " << dialog_id; } } } - if (!result.empty() && offset + result.size() > static_cast(total_count)) { - LOG(ERROR) << "Fix total count of blocked chats from " << total_count << " to " << offset + result.size(); - total_count = offset + narrow_cast(result.size()); + if (!dialog_ids.empty() && offset + dialog_ids.size() > static_cast(total_count)) { + LOG(ERROR) << "Fix total count of blocked chats from " << total_count << " to " << offset + dialog_ids.size(); + total_count = offset + narrow_cast(dialog_ids.size()); } - it->second.first = total_count; -} -void MessagesManager::on_failed_get_blocked_dialogs(int64 random_id) { - auto it = found_blocked_dialogs_.find(random_id); - CHECK(it != found_blocked_dialogs_.end()); - found_blocked_dialogs_.erase(it); + auto senders = transform(dialog_ids, [this](DialogId dialog_id) { + return get_message_sender_object(dialog_id, "on_get_blocked_dialogs"); + }); + promise.set_value(td_api::make_object(total_count, std::move(senders))); } bool MessagesManager::have_message_force(FullMessageId full_message_id, const char *source) { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 924dbf485..1ba2d2908 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -546,13 +546,11 @@ class MessagesManager final : public Actor { void block_message_sender_from_replies(MessageId message_id, bool delete_message, bool delete_all_messages, bool report_spam, Promise &&promise); - std::pair> get_blocked_dialogs(int32 offset, int32 limit, int64 &random_id, - Promise &&promise); + void get_blocked_dialogs(int32 offset, int32 limit, Promise> &&promise); - void on_get_blocked_dialogs(int32 offset, int32 limit, int64 random_id, int32 total_count, - vector> &&blocked_peers); - - void on_failed_get_blocked_dialogs(int64 random_id); + void on_get_blocked_dialogs(int32 offset, int32 limit, int32 total_count, + vector> &&blocked_peers, + Promise> &&promise); bool can_get_message_statistics(FullMessageId full_message_id); @@ -3275,9 +3273,6 @@ class MessagesManager final : public Actor { }; std::unordered_map found_common_dialogs_; - std::unordered_map>> - found_blocked_dialogs_; // random_id -> [total_count, [dialog_id]...] - std::unordered_map get_dialog_message_by_date_results_; std::unordered_map>> diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index cb94f5b48..515d3d527 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -1861,31 +1861,6 @@ class GetChatEventLogRequest final : public RequestOnceActor { } }; -class GetBlockedMessageSendersRequest final : public RequestActor<> { - int32 offset_; - int32 limit_; - int64 random_id_; - - std::pair> message_senders_; - - void do_run(Promise &&promise) final { - message_senders_ = td->messages_manager_->get_blocked_dialogs(offset_, limit_, random_id_, std::move(promise)); - } - - void do_send_result() final { - auto senders = - transform(message_senders_.second, [messages_manager = td->messages_manager_.get()](DialogId dialog_id) { - return messages_manager->get_message_sender_object(dialog_id, "GetBlockedMessageSendersRequest"); - }); - send_result(td_api::make_object(message_senders_.first, std::move(senders))); - } - - public: - GetBlockedMessageSendersRequest(ActorShared td, uint64 request_id, int32 offset, int32 limit) - : RequestActor(std::move(td), request_id), offset_(offset), limit_(limit), random_id_(0) { - } -}; - class ImportContactsRequest final : public RequestActor<> { vector contacts_; int64 random_id_; @@ -6598,7 +6573,8 @@ void Td::on_request(uint64 id, const td_api::blockMessageSenderFromReplies &requ void Td::on_request(uint64 id, const td_api::getBlockedMessageSenders &request) { CHECK_IS_USER(); - CREATE_REQUEST(GetBlockedMessageSendersRequest, request.offset_, request.limit_); + CREATE_REQUEST_PROMISE(); + messages_manager_->get_blocked_dialogs(request.offset_, request.limit_, std::move(promise)); } void Td::on_request(uint64 id, td_api::addContact &request) {