From dbf1253075433d314a8780f0e74304997dff9afa Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 20 Jan 2021 14:49:18 +0300 Subject: [PATCH] Replace deleteSupergroup with universal deleteChat method. --- td/generate/scheme/td_api.tl | 6 +++--- td/generate/scheme/td_api.tlo | Bin 194356 -> 194340 bytes td/telegram/ContactsManager.cpp | 22 ++++++++++++++++++++++ td/telegram/ContactsManager.h | 8 +++++--- td/telegram/MessagesManager.cpp | 20 +++++++++++--------- td/telegram/MessagesManager.h | 7 +++++-- td/telegram/SecretChatActor.cpp | 5 +++-- td/telegram/SecretChatActor.h | 2 +- td/telegram/SecretChatsManager.cpp | 6 +++--- td/telegram/Td.cpp | 22 ++++++++++++++++------ td/telegram/Td.h | 4 ++-- td/telegram/cli.cpp | 4 ++-- test/secret.cpp | 4 ++-- 13 files changed, 75 insertions(+), 35 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 4029464f6..466cb779e 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -3821,6 +3821,9 @@ getMessageThreadHistory chat_id:int53 message_id:int53 from_message_id:int53 off //@chat_id Chat identifier @remove_from_chat_list Pass true if the chat should be removed from the chat list @revoke Pass true to try to delete chat history for all users deleteChatHistory chat_id:int53 remove_from_chat_list:Bool revoke:Bool = Ok; +//@description Deletes a chat along with all messages in the corresponding chat for all chat members; requires owner privileges. For group chats this will release the username and remove all members. Chats with more than 1000 members can't be deleted using this method @chat_id Chat identifier +deleteChat chat_id:int53 = Ok; + //@description Searches for messages with given words in the chat. Returns the results in reverse chronological order, i.e. in order of decreasing message_id. Cannot be used in secret chats with a non-empty query //-(searchSecretMessages should be used instead), or without an enabled message database. For optimal performance the number of returned messages is chosen by the library //@chat_id Identifier of the chat in which to search messages @@ -4695,9 +4698,6 @@ reportSupergroupSpam supergroup_id:int32 user_id:int32 message_ids:vector //@filter The type of users to return. By default, supergroupMembersFilterRecent @offset Number of users to skip @limit The maximum number of users be returned; up to 200 getSupergroupMembers supergroup_id:int32 filter:SupergroupMembersFilter offset:int32 limit:int32 = ChatMembers; -//@description Deletes a supergroup or channel along with all messages in the corresponding chat. This will release the supergroup or channel username and remove all members; requires owner privileges in the supergroup or channel. Chats with more than 1000 members can't be deleted using this method @supergroup_id Identifier of the supergroup or channel -deleteSupergroup supergroup_id:int32 = Ok; - //@description Closes a secret chat, effectively transferring its state to secretChatStateClosed @secret_chat_id Secret chat identifier closeSecretChat secret_chat_id:int32 = Ok; diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 017b3e174c3622c2ba18399ff18eb86f671716ef..25dce982dad80b63db6af35ac4d2d64b69858bbc 100644 GIT binary patch delta 47 zcmdn;j(f>F?uHh|Ele?<0&x#cedS6?%}Fgubvj=;CWRFM D;K33Q delta 62 zcmV-E0KxyH?hCZ;3xI?Hv;tg7x3Eb9R0S2Ip3I04WMyn+bY)X@aAk65a&L8Tmk}@m U6PMgF0W6o$ECLX>1Wy7Ws2wjCR{#J2 diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 7f3fc2055..259a00287 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -34,6 +34,7 @@ #include "td/telegram/Photo.hpp" #include "td/telegram/RestrictionReason.h" #include "td/telegram/SecretChatActor.h" +#include "td/telegram/SecretChatsManager.h" #include "td/telegram/ServerMessageId.h" #include "td/telegram/StickerSetId.hpp" #include "td/telegram/StickersManager.h" @@ -6724,6 +6725,27 @@ void ContactsManager::delete_channel(ChannelId channel_id, Promise &&promi td_->create_handler(std::move(promise))->send(channel_id); } +void ContactsManager::delete_dialog(DialogId dialog_id, Promise &&promise) { + if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + return promise.set_error(Status::Error(3, "Chat not found")); + } + + switch (dialog_id.get_type()) { + case DialogType::User: + return td_->messages_manager_->delete_dialog_history(dialog_id, true, true, std::move(promise)); + case DialogType::Chat: + return delete_chat(dialog_id.get_chat_id(), std::move(promise)); + case DialogType::Channel: + return delete_channel(dialog_id.get_channel_id(), std::move(promise)); + case DialogType::SecretChat: + send_closure(td_->secret_chats_manager_, &SecretChatsManager::cancel_chat, dialog_id.get_secret_chat_id(), true, + std::move(promise)); + return; + default: + UNREACHABLE(); + } +} + void ContactsManager::add_chat_participant(ChatId chat_id, UserId user_id, int32 forward_limit, Promise &&promise) { const Chat *c = get_chat(chat_id); diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 77894dfd8..ad9b9f135 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -359,7 +359,7 @@ class ContactsManager : public Actor { void report_channel_spam(ChannelId channel_id, UserId user_id, const vector &message_ids, Promise &&promise); - void delete_channel(ChannelId channel_id, Promise &&promise); + void delete_dialog(DialogId dialog_id, Promise &&promise); void get_channel_statistics(DialogId dialog_id, bool is_dark, Promise> &&promise); @@ -379,8 +379,6 @@ class ContactsManager : public Actor { void add_channel_participants(ChannelId channel_id, const vector &user_ids, Promise &&promise); - void delete_chat(ChatId chat_id, Promise &&promise); - void change_chat_participant_status(ChatId chat_id, UserId user_id, DialogParticipantStatus status, Promise &&promise); @@ -1132,6 +1130,10 @@ class ContactsManager : public Actor { tl_object_ptr input_check_password, Promise &&promise); + void delete_chat(ChatId chat_id, Promise &&promise); + + void delete_channel(ChannelId channel_id, Promise &&promise); + void get_channel_statistics_dc_id(DialogId dialog_id, bool for_full_statistics, Promise &&promise); void get_channel_statistics_dc_id_impl(ChannelId channel_id, bool for_full_statistics, Promise &&promise); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 8762d5fa5..77f289987 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -12255,9 +12255,9 @@ void MessagesManager::finish_delete_secret_messages(DialogId dialog_id, std::vec delete_dialog_messages_from_updates(dialog_id, to_delete_message_ids, false); } -void MessagesManager::delete_secret_chat_history(SecretChatId secret_chat_id, MessageId last_message_id, - Promise<> promise) { - LOG(DEBUG) << "On delete history in " << secret_chat_id << " up to " << last_message_id; +void MessagesManager::delete_secret_chat_history(SecretChatId secret_chat_id, bool remove_from_dialog_list, + MessageId last_message_id, Promise<> promise) { + LOG(DEBUG) << "Delete history in " << secret_chat_id << " up to " << last_message_id; CHECK(secret_chat_id.is_valid()); CHECK(!last_message_id.is_scheduled()); @@ -12273,19 +12273,20 @@ void MessagesManager::delete_secret_chat_history(SecretChatId secret_chat_id, Me pending_secret_message->type = PendingSecretMessage::Type::DeleteHistory; pending_secret_message->dialog_id = dialog_id; pending_secret_message->last_message_id = last_message_id; + pending_secret_message->remove_from_dialog_list = remove_from_dialog_list; add_secret_message(std::move(pending_secret_message)); } -void MessagesManager::finish_delete_secret_chat_history(DialogId dialog_id, MessageId last_message_id, - Promise<> promise) { +void MessagesManager::finish_delete_secret_chat_history(DialogId dialog_id, bool remove_from_dialog_list, + MessageId last_message_id, Promise<> promise) { LOG(DEBUG) << "Delete history in " << dialog_id << " up to " << last_message_id; - promise.set_value(Unit()); // TODO: set after event is saved Dialog *d = get_dialog(dialog_id); CHECK(d != nullptr); // TODO: probably last_message_id is not needed - delete_all_dialog_messages(d, false, true); + delete_all_dialog_messages(d, remove_from_dialog_list, true); + promise.set_value(Unit()); // TODO: set after event is saved } void MessagesManager::read_secret_chat_outbox(SecretChatId secret_chat_id, int32 up_to_date, int32 read_date) { @@ -12560,8 +12561,9 @@ void MessagesManager::finish_add_secret_message(unique_ptr std::move(pending_secret_message->success_promise)); } if (pending_secret_message->type == PendingSecretMessage::Type::DeleteHistory) { - return finish_delete_secret_chat_history(pending_secret_message->dialog_id, pending_secret_message->last_message_id, - std::move(pending_secret_message->success_promise)); + return finish_delete_secret_chat_history( + pending_secret_message->dialog_id, pending_secret_message->remove_from_dialog_list, + pending_secret_message->last_message_id, std::move(pending_secret_message->success_promise)); } auto d = get_dialog(pending_secret_message->message_info.dialog_id); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index ff79e01bb..18dcaf4b4 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -234,7 +234,8 @@ class MessagesManager : public Actor { void delete_secret_messages(SecretChatId secret_chat_id, std::vector random_ids, Promise<> promise); - void delete_secret_chat_history(SecretChatId secret_chat_id, MessageId last_message_id, Promise<> promise); + void delete_secret_chat_history(SecretChatId secret_chat_id, bool remove_from_dialog_list, MessageId last_message_id, + Promise<> promise); void read_secret_chat_outbox(SecretChatId secret_chat_id, int32 up_to_date, int32 read_date); @@ -1574,6 +1575,7 @@ class MessagesManager : public Actor { DialogId dialog_id; vector random_ids; MessageId last_message_id; + bool remove_from_dialog_list = false; Promise<> success_promise; }; @@ -1709,7 +1711,8 @@ class MessagesManager : public Actor { void finish_delete_secret_messages(DialogId dialog_id, std::vector random_ids, Promise<> promise); - void finish_delete_secret_chat_history(DialogId dialog_id, MessageId last_message_id, Promise<> promise); + void finish_delete_secret_chat_history(DialogId dialog_id, bool remove_from_dialog_list, MessageId last_message_id, + Promise<> promise); MessageInfo parse_telegram_api_message(tl_object_ptr message_ptr, bool is_scheduled, const char *source) const; diff --git a/td/telegram/SecretChatActor.cpp b/td/telegram/SecretChatActor.cpp index 3e1b32ab1..571472767 100644 --- a/td/telegram/SecretChatActor.cpp +++ b/td/telegram/SecretChatActor.cpp @@ -770,7 +770,7 @@ void SecretChatActor::do_close_chat_impl(bool delete_history, bool is_already_di auto lock = mpas.get_promise(); if (delete_history) { - context_->on_flush_history(MessageId::max(), mpas.get_promise()); + context_->on_flush_history(true, MessageId::max(), mpas.get_promise()); } send_update_secret_chat(); @@ -1349,7 +1349,8 @@ Status SecretChatActor::do_inbound_message_decrypted(unique_ptron_flush_history(MessageId(ServerMessageId(message->message_id)), std::move(save_message_finish)); + context_->on_flush_history(false, MessageId(ServerMessageId(message->message_id)), + std::move(save_message_finish)); break; case secret_api::decryptedMessageActionReadMessages::ID: { const auto &random_ids = diff --git a/td/telegram/SecretChatActor.h b/td/telegram/SecretChatActor.h index f2ff16a62..682aa7044 100644 --- a/td/telegram/SecretChatActor.h +++ b/td/telegram/SecretChatActor.h @@ -96,7 +96,7 @@ class SecretChatActor : public NetQueryCallback { tl_object_ptr file, tl_object_ptr message, Promise<> promise) = 0; virtual void on_delete_messages(std::vector random_id, Promise<> promise) = 0; - virtual void on_flush_history(MessageId message_id, Promise<> promise) = 0; + virtual void on_flush_history(bool remove_from_dialog_list, MessageId message_id, Promise<> promise) = 0; virtual void on_read_message(int64 random_id, Promise<> promise) = 0; virtual void on_screenshot_taken(UserId user_id, MessageId message_id, int32 date, int64 random_id, Promise<> promise) = 0; diff --git a/td/telegram/SecretChatsManager.cpp b/td/telegram/SecretChatsManager.cpp index 6176b3586..db6d0a28e 100644 --- a/td/telegram/SecretChatsManager.cpp +++ b/td/telegram/SecretChatsManager.cpp @@ -373,9 +373,9 @@ unique_ptr SecretChatsManager::make_secret_chat_contex send_closure(G()->messages_manager(), &MessagesManager::delete_secret_messages, secret_chat_id_, std::move(random_ids), std::move(promise)); } - void on_flush_history(MessageId message_id, Promise<> promise) override { - send_closure(G()->messages_manager(), &MessagesManager::delete_secret_chat_history, secret_chat_id_, message_id, - std::move(promise)); + void on_flush_history(bool remove_from_dialog_list, MessageId message_id, Promise<> promise) override { + send_closure(G()->messages_manager(), &MessagesManager::delete_secret_chat_history, secret_chat_id_, + remove_from_dialog_list, message_id, std::move(promise)); } void on_read_message(int64 random_id, Promise<> promise) override { send_closure(G()->messages_manager(), &MessagesManager::open_secret_message, secret_chat_id_, random_id, diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index ab419916e..24dfc126c 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5472,6 +5472,22 @@ void Td::on_request(uint64 id, const td_api::deleteChatHistory &request) { std::move(promise)); } +void Td::on_request(uint64 id, const td_api::deleteChat &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + DialogId dialog_id(request.chat_id_); + auto query_promise = [actor_id = messages_manager_actor_.get(), dialog_id, + promise = std::move(promise)](Result &&result) mutable { + if (result.is_error()) { + promise.set_error(result.move_as_error()); + } else { + send_closure(actor_id, &MessagesManager::delete_dialog, dialog_id); + promise.set_value(Unit()); + } + }; + contacts_manager_->delete_dialog(dialog_id, std::move(query_promise)); +} + void Td::on_request(uint64 id, const td_api::getMessageThreadHistory &request) { CHECK_IS_USER(); CREATE_REQUEST(GetMessageThreadHistoryRequest, request.chat_id_, request.message_id_, request.from_message_id_, @@ -6699,12 +6715,6 @@ void Td::on_request(uint64 id, td_api::getSupergroupMembers &request) { request.offset_, request.limit_, -1, false, std::move(query_promise)); } -void Td::on_request(uint64 id, const td_api::deleteSupergroup &request) { - CHECK_IS_USER(); - CREATE_OK_REQUEST_PROMISE(); - contacts_manager_->delete_channel(ChannelId(request.supergroup_id_), std::move(promise)); -} - void Td::on_request(uint64 id, td_api::closeSecretChat &request) { CREATE_OK_REQUEST_PROMISE(); send_closure(secret_chats_manager_, &SecretChatsManager::cancel_chat, SecretChatId(request.secret_chat_id_), false, diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 7e690f5b6..7efb1f85c 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -578,6 +578,8 @@ class Td final : public NetQueryCallback { void on_request(uint64 id, const td_api::deleteChatHistory &request); + void on_request(uint64 id, const td_api::deleteChat &request); + void on_request(uint64 id, const td_api::getMessageThreadHistory &request); void on_request(uint64 id, td_api::searchChatMessages &request); @@ -884,8 +886,6 @@ class Td final : public NetQueryCallback { void on_request(uint64 id, td_api::getSupergroupMembers &request); - void on_request(uint64 id, const td_api::deleteSupergroup &request); - void on_request(uint64 id, td_api::closeSecretChat &request); void on_request(uint64 id, td_api::getStickers &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index b04bc96bb..55900c391 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3417,8 +3417,8 @@ class CliClient final : public Actor { td_api::make_object(as_location("40.0", "60.0"), "address"))); } else if (op == "UpgradeBasicGroupChatToSupergroupChat") { send_request(td_api::make_object(as_chat_id(args))); - } else if (op == "DeleteSupergroup") { - send_request(td_api::make_object(as_supergroup_id(args))); + } else if (op == "DeleteChat") { + send_request(td_api::make_object(as_chat_id(args))); } else if (op == "gcpc") { send_request(td_api::make_object()); } else if (op == "gcpcl") { diff --git a/test/secret.cpp b/test/secret.cpp index 6353b2151..b9d91108b 100644 --- a/test/secret.cpp +++ b/test/secret.cpp @@ -545,7 +545,7 @@ class FakeSecretChatContext : public SecretChatActor::Context { void on_send_message_ok(int64 random_id, MessageId message_id, int32 date, tl_object_ptr file, Promise<>) override; void on_delete_messages(std::vector random_id, Promise<>) override; - void on_flush_history(MessageId, Promise<>) override; + void on_flush_history(bool, MessageId, Promise<>) override; void on_read_message(int64, Promise<>) override; void on_screenshot_taken(UserId user_id, MessageId message_id, int32 date, int64 random_id, @@ -992,7 +992,7 @@ void FakeSecretChatContext::on_send_message_ok(int64 random_id, MessageId messag void FakeSecretChatContext::on_delete_messages(std::vector random_id, Promise<> promise) { promise.set_value(Unit()); } -void FakeSecretChatContext::on_flush_history(MessageId, Promise<> promise) { +void FakeSecretChatContext::on_flush_history(bool, MessageId, Promise<> promise) { promise.set_error(Status::Error("Unsupported")); } void FakeSecretChatContext::on_read_message(int64, Promise<> promise) {