From 4f1e5db8cd358f8c149f2a6183eb9f4cf0a86468 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 9 Nov 2021 10:19:04 +0300 Subject: [PATCH] Simplify message deletion. --- td/telegram/Global.h | 3 +- td/telegram/MessagesManager.cpp | 103 +++++++++++++++----------------- td/telegram/MessagesManager.h | 2 + 3 files changed, 52 insertions(+), 56 deletions(-) diff --git a/td/telegram/Global.h b/td/telegram/Global.h index 09eb3be49..9bdee22bc 100644 --- a/td/telegram/Global.h +++ b/td/telegram/Global.h @@ -496,7 +496,8 @@ class Global final : public ActorContext { inline Global *G_impl(const char *file, int line) { ActorContext *context = Scheduler::context(); - LOG_CHECK(context != nullptr && context->get_id() == Global::ID) << "In " << file << " at " << line; + LOG_CHECK(context != nullptr && context->get_id() == Global::ID) + << "Context = " << context << " in " << file << " at " << line; return static_cast(context); } diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 775353c2c..828072ec4 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -3961,31 +3961,20 @@ class SetTypingQuery final : public Td::ResultHandler { class DeleteMessagesQuery final : public Td::ResultHandler { Promise promise_; DialogId dialog_id_; - int32 query_count_; public: explicit DeleteMessagesQuery(Promise &&promise) : promise_(std::move(promise)) { } - void send(DialogId dialog_id, vector &&message_ids, bool revoke) { + void send(DialogId dialog_id, vector &&server_message_ids, bool revoke) { dialog_id_ = dialog_id; int32 flags = 0; if (revoke) { flags |= telegram_api::messages_deleteMessages::REVOKE_MASK; } - query_count_ = 0; - auto server_message_ids = MessagesManager::get_server_message_ids(message_ids); - const size_t MAX_SLICE_SIZE = 100; - for (size_t i = 0; i < server_message_ids.size(); i += MAX_SLICE_SIZE) { - auto end_i = i + MAX_SLICE_SIZE; - auto end = end_i < server_message_ids.size() ? server_message_ids.begin() + end_i : server_message_ids.end(); - vector slice(server_message_ids.begin() + i, end); - - query_count_++; - send_query(G()->net_query_creator().create( - telegram_api::messages_deleteMessages(flags, false /*ignored*/, std::move(slice)))); - } + send_query(G()->net_query_creator().create( + telegram_api::messages_deleteMessages(flags, false /*ignored*/, std::move(server_message_ids)))); } void on_result(BufferSlice packet) final { @@ -3995,14 +3984,11 @@ class DeleteMessagesQuery final : public Td::ResultHandler { } auto affected_messages = result_ptr.move_as_ok(); - CHECK(affected_messages->get_id() == telegram_api::messages_affectedMessages::ID); - if (affected_messages->pts_count_ > 0) { td_->updates_manager_->add_pending_pts_update(make_tl_object(), affected_messages->pts_, - affected_messages->pts_count_, Time::now(), Promise(), + affected_messages->pts_count_, Time::now(), std::move(promise_), "delete messages query"); - } - if (--query_count_ == 0) { + } else { promise_.set_value(Unit()); } } @@ -4022,29 +4008,18 @@ class DeleteMessagesQuery final : public Td::ResultHandler { class DeleteChannelMessagesQuery final : public Td::ResultHandler { Promise promise_; - int32 query_count_; ChannelId channel_id_; public: explicit DeleteChannelMessagesQuery(Promise &&promise) : promise_(std::move(promise)) { } - void send(ChannelId channel_id, vector &&message_ids) { + void send(ChannelId channel_id, vector &&server_message_ids) { channel_id_ = channel_id; - query_count_ = 0; - auto server_message_ids = MessagesManager::get_server_message_ids(message_ids); - const size_t MAX_SLICE_SIZE = 100; - for (size_t i = 0; i < server_message_ids.size(); i += MAX_SLICE_SIZE) { - auto end_i = i + MAX_SLICE_SIZE; - auto end = end_i < server_message_ids.size() ? server_message_ids.begin() + end_i : server_message_ids.end(); - vector slice(server_message_ids.begin() + i, end); - - query_count_++; - auto input_channel = td_->contacts_manager_->get_input_channel(channel_id); - CHECK(input_channel != nullptr); - send_query(G()->net_query_creator().create( - telegram_api::channels_deleteMessages(std::move(input_channel), std::move(slice)))); - } + auto input_channel = td_->contacts_manager_->get_input_channel(channel_id); + CHECK(input_channel != nullptr); + send_query(G()->net_query_creator().create( + telegram_api::channels_deleteMessages(std::move(input_channel), std::move(server_message_ids)))); } void on_result(BufferSlice packet) final { @@ -4054,15 +4029,11 @@ class DeleteChannelMessagesQuery final : public Td::ResultHandler { } auto affected_messages = result_ptr.move_as_ok(); - LOG(INFO) << "Receive result for DeleteChannelMessagesQuery: " << to_string(affected_messages); - CHECK(affected_messages->get_id() == telegram_api::messages_affectedMessages::ID); - if (affected_messages->pts_count_ > 0) { td_->messages_manager_->add_pending_channel_update(DialogId(channel_id_), make_tl_object(), affected_messages->pts_, affected_messages->pts_count_, - Promise(), "DeleteChannelMessagesQuery"); - } - if (--query_count_ == 0) { + std::move(promise_), "DeleteChannelMessagesQuery"); + } else { promise_.set_value(Unit()); } } @@ -10536,7 +10507,7 @@ void MessagesManager::delete_messages(DialogId dialog_id, const vectorclose_flag()) { + binlog_erase(G()->td_db()->get_binlog(), log_event_id); + } +} + void MessagesManager::delete_sent_message_on_server(DialogId dialog_id, MessageId message_id) { // being sent message was deleted by the user or is in an inaccessible channel // don't need to send an update to the user, because the message has already been deleted @@ -10643,18 +10620,35 @@ void MessagesManager::delete_messages_on_server(DialogId dialog_id, vectorcreate_handler(std::move(promise))->send(dialog_id, std::move(message_ids), revoke); - break; - case DialogType::Channel: - td_->create_handler(std::move(promise)) - ->send(dialog_id.get_channel_id(), std::move(message_ids)); + case DialogType::Channel: { + auto server_message_ids = MessagesManager::get_server_message_ids(message_ids); + const size_t MAX_SLICE_SIZE = 100; // server side limit + for (size_t i = 0; i < server_message_ids.size(); i += MAX_SLICE_SIZE) { + auto end_i = i + MAX_SLICE_SIZE; + auto end = end_i < server_message_ids.size() ? server_message_ids.begin() + end_i : server_message_ids.end(); + if (dialog_type != DialogType::Channel) { + td_->create_handler(mpas.get_promise()) + ->send(dialog_id, {server_message_ids.begin() + i, end}, revoke); + } else { + td_->create_handler(mpas.get_promise()) + ->send(dialog_id.get_channel_id(), {server_message_ids.begin() + i, end}); + } + } break; + } case DialogType::SecretChat: { vector random_ids; auto d = get_dialog_force(dialog_id, "delete_messages_on_server"); @@ -10667,9 +10661,7 @@ void MessagesManager::delete_messages_on_server(DialogId dialog_id, vectorsecret_chats_manager(), &SecretChatsManager::delete_messages, dialog_id.get_secret_chat_id(), - std::move(random_ids), std::move(promise)); - } else { - promise.set_value(Unit()); + std::move(random_ids), mpas.get_promise()); } break; } @@ -10677,6 +10669,7 @@ void MessagesManager::delete_messages_on_server(DialogId dialog_id, vector &message, bool is_permanently_deleted, vector &deleted_message_ids); + void erase_delete_messages_log_event(uint64 log_event_id); + void delete_sent_message_on_server(DialogId dialog_id, MessageId message_id); void delete_messages_on_server(DialogId dialog_id, vector message_ids, bool revoke, uint64 log_event_id,