From 1bc8e90c10d7d1c1d425856132119aed0483dfaa Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 10 Jan 2024 15:20:16 +0300 Subject: [PATCH] Move create_new_secret_chat to ContactsManager. --- td/telegram/ContactsManager.cpp | 28 +++++++++++++++++++++++ td/telegram/ContactsManager.h | 4 ++++ td/telegram/MessagesManager.cpp | 11 ---------- td/telegram/MessagesManager.h | 2 -- td/telegram/Td.cpp | 39 +++------------------------------ 5 files changed, 35 insertions(+), 49 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 5194ae40b..dd409d16c 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -50,6 +50,7 @@ #include "td/telegram/PremiumGiftOption.hpp" #include "td/telegram/ReactionManager.h" #include "td/telegram/SecretChatLayer.h" +#include "td/telegram/SecretChatsManager.h" #include "td/telegram/ServerMessageId.h" #include "td/telegram/StickerPhotoSize.h" #include "td/telegram/StickersManager.h" @@ -16488,6 +16489,33 @@ void ContactsManager::send_get_channel_full_query(ChannelFull *channel_full, Cha get_chat_full_queries_.add_query(DialogId(channel_id).get(), std::move(send_query), std::move(promise)); } +void ContactsManager::create_new_secret_chat(UserId user_id, Promise> &&promise) { + TRY_RESULT_PROMISE(promise, input_user, get_input_user(user_id)); + if (input_user->get_id() != telegram_api::inputUser::ID) { + return promise.set_error(Status::Error(400, "Can't create secret chat with the user")); + } + auto user = static_cast(input_user.get()); + + send_closure( + G()->secret_chats_manager(), &SecretChatsManager::create_chat, UserId(user->user_id_), user->access_hash_, + PromiseCreator::lambda([actor_id = actor_id(this), + promise = std::move(promise)](Result r_secret_chat_id) mutable { + if (r_secret_chat_id.is_error()) { + return promise.set_error(r_secret_chat_id.move_as_error()); + } + send_closure(actor_id, &ContactsManager::on_create_new_secret_chat, r_secret_chat_id.ok(), std::move(promise)); + })); +} + +void ContactsManager::on_create_new_secret_chat(SecretChatId secret_chat_id, + Promise> &&promise) { + TRY_STATUS_PROMISE(promise, G()->close_status()); + CHECK(secret_chat_id.is_valid()); + DialogId dialog_id(secret_chat_id); + td_->dialog_manager_->force_create_dialog(dialog_id, "on_create_new_secret_chat"); + promise.set_value(td_->messages_manager_->get_chat_object(dialog_id)); +} + bool ContactsManager::have_secret_chat(SecretChatId secret_chat_id) const { return secret_chats_.count(secret_chat_id) > 0; } diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 286229d38..faec05798 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -606,6 +606,8 @@ class ContactsManager final : public Actor { bool is_channel_public(ChannelId channel_id) const; + void create_new_secret_chat(UserId user_id, Promise> &&promise); + bool have_secret_chat(SecretChatId secret_chat_id) const; bool have_secret_chat_force(SecretChatId secret_chat_id, const char *source); bool get_secret_chat(SecretChatId secret_chat_id, bool force, Promise &&promise); @@ -1346,6 +1348,8 @@ class ContactsManager final : public Actor { void send_get_channel_full_query(ChannelFull *channel_full, ChannelId channel_id, Promise &&promise, const char *source); + void on_create_new_secret_chat(SecretChatId secret_chat_id, Promise> &&promise); + const SecretChat *get_secret_chat(SecretChatId secret_chat_id) const; SecretChat *get_secret_chat(SecretChatId secret_chat_id); SecretChat *get_secret_chat_force(SecretChatId secret_chat_id, const char *source); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index b74841788..ff244f340 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -18202,17 +18202,6 @@ DialogId MessagesManager::create_new_channel_chat(const string &title, bool is_f return DialogId(); } -void MessagesManager::create_new_secret_chat(UserId user_id, Promise &&promise) { - TRY_RESULT_PROMISE(promise, input_user, td_->contacts_manager_->get_input_user(user_id)); - if (input_user->get_id() != telegram_api::inputUser::ID) { - return promise.set_error(Status::Error(400, "Can't create secret chat with the user")); - } - auto user = static_cast(input_user.get()); - - send_closure(G()->secret_chats_manager(), &SecretChatsManager::create_chat, UserId(user->user_id_), - user->access_hash_, std::move(promise)); -} - DialogId MessagesManager::migrate_dialog_to_megagroup(DialogId dialog_id, Promise &&promise) { LOG(INFO) << "Trying to convert " << dialog_id << " to supergroup"; diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 57afb2e3d..8fe8a5d53 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -641,8 +641,6 @@ class MessagesManager final : public Actor { const DialogLocation &location, bool for_import, MessageTtl message_ttl, int64 &random_id, Promise &&promise); - void create_new_secret_chat(UserId user_id, Promise &&promise); - DialogId migrate_dialog_to_megagroup(DialogId dialog_id, Promise &&promise); bool is_dialog_opened(DialogId dialog_id) const; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 263429a30..238369966 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -1680,41 +1680,6 @@ class CreateNewGroupChatRequest final : public RequestActor<> { } }; -class CreateNewSecretChatRequest final : public RequestActor { - UserId user_id_; - SecretChatId secret_chat_id_; - - void do_run(Promise &&promise) final { - if (get_tries() < 2) { - promise.set_value(std::move(secret_chat_id_)); - return; - } - td_->messages_manager_->create_new_secret_chat(user_id_, std::move(promise)); - } - - void do_set_result(SecretChatId &&result) final { - secret_chat_id_ = result; - LOG(INFO) << "New " << secret_chat_id_ << " created"; - } - - void do_send_result() final { - CHECK(secret_chat_id_.is_valid()); - // SecretChatActor will send this update by itself - // But since the update may still be on its way, we will update essential fields here. - td_->contacts_manager_->on_update_secret_chat( - secret_chat_id_, 0 /* no access_hash */, user_id_, SecretChatState::Unknown, true /* it is outbound chat */, - -1 /* unknown TTL */, 0 /* unknown creation date */, "" /* no key_hash */, 0, FolderId()); - DialogId dialog_id(secret_chat_id_); - td_->dialog_manager_->force_create_dialog(dialog_id, "create new secret chat", true); - send_result(td_->messages_manager_->get_chat_object(dialog_id)); - } - - public: - CreateNewSecretChatRequest(ActorShared td, uint64 request_id, int64 user_id) - : RequestActor(std::move(td), request_id), user_id_(user_id) { - } -}; - class CreateNewSupergroupChatRequest final : public RequestActor<> { string title_; bool is_forum_; @@ -6066,7 +6031,9 @@ void Td::on_request(uint64 id, td_api::createNewSupergroupChat &request) { } void Td::on_request(uint64 id, const td_api::createNewSecretChat &request) { - CREATE_REQUEST(CreateNewSecretChatRequest, request.user_id_); + CHECK_IS_USER(); + CREATE_REQUEST_PROMISE(); + contacts_manager_->create_new_secret_chat(UserId(request.user_id_), std::move(promise)); } void Td::on_request(uint64 id, const td_api::createCall &request) {