From 00eed8928c3cc62db6ef37c007008cd444cf8847 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 13 Dec 2020 15:18:33 +0300 Subject: [PATCH] Move voice chat creation to GroupCallManager. --- td/generate/scheme/td_api.tl | 2 +- td/telegram/ContactsManager.cpp | 63 ------------------------------- td/telegram/ContactsManager.h | 5 --- td/telegram/GroupCallManager.cpp | 65 +++++++++++++++++++++++++++++++- td/telegram/GroupCallManager.h | 4 +- td/telegram/Td.cpp | 2 +- 6 files changed, 68 insertions(+), 73 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index b40abed15..5c33a2611 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4351,7 +4351,7 @@ sendCallRating call_id:int32 rating:int32 comment:string problems:vectorcreate_handler(std::move(promise))->send(channel_id, slow_mode_delay); } -void ContactsManager::create_channel_voice_chat(DialogId dialog_id, Promise &&promise) { - if (!dialog_id.is_valid()) { - return promise.set_error(Status::Error(400, "Invalid chat identifier specified")); - } - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { - return promise.set_error(Status::Error(400, "Chat not found")); - } - - if (dialog_id.get_type() != DialogType::Channel) { - return promise.set_error(Status::Error(400, "Chat is not a supergroup")); - } - - auto channel_id = dialog_id.get_channel_id(); - const Channel *c = get_channel(channel_id); - if (c == nullptr) { - return promise.set_error(Status::Error(400, "Chat info not found")); - } - if (!c->is_megagroup) { - return promise.set_error(Status::Error(400, "Chat is not a supergroup")); - } - if (!get_channel_permissions(c).can_manage_calls()) { - return promise.set_error(Status::Error(400, "Not enough rights in the supergroup")); - } - - auto new_promise = PromiseCreator::lambda( - [actor_id = actor_id(this), channel_id, promise = std::move(promise)](Result result) mutable { - if (result.is_error()) { - promise.set_error(result.move_as_error()); - } else { - send_closure(actor_id, &ContactsManager::on_create_channel_group_call, channel_id, result.move_as_ok(), - std::move(promise)); - } - }); - send_closure(G()->group_call_manager(), &GroupCallManager::create_voice_chat, dialog_id, std::move(new_promise)); -} - -void ContactsManager::on_create_channel_group_call(ChannelId channel_id, InputGroupCallId input_group_call_id, - Promise &&promise) { - if (G()->close_flag()) { - return promise.set_error(Status::Error(500, "Request aborted")); - } - if (!input_group_call_id.is_valid()) { - return promise.set_error(Status::Error(500, "Receive invalid group call identifier")); - } - - Channel *c = get_channel(channel_id); - CHECK(c != nullptr); - if (!c->has_active_group_call || !c->is_group_call_empty) { - c->has_active_group_call = true; - c->is_group_call_empty = true; - c->is_changed = true; - update_channel(c, channel_id); - } - - auto channel_full = get_channel_full_force(channel_id, "on_create_channel_group_call"); - if (channel_full != nullptr && channel_full->active_group_call_id != input_group_call_id) { - channel_full->active_group_call_id = input_group_call_id; - channel_full->is_changed = true; - update_channel_full(channel_full, channel_id); - } - promise.set_value(td_->group_call_manager_->get_group_call_id(input_group_call_id, DialogId(channel_id))); -} - void ContactsManager::get_channel_statistics_dc_id(DialogId dialog_id, bool for_full_statistics, Promise &&promise) { if (!dialog_id.is_valid()) { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 233c0436c..d8772d38b 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -367,8 +367,6 @@ class ContactsManager : public Actor { void set_channel_slow_mode_delay(DialogId dialog_id, int32 slow_mode_delay, Promise &&promise); - void create_channel_voice_chat(DialogId dialog_id, Promise &&promise); - void report_channel_spam(ChannelId channel_id, UserId user_id, const vector &message_ids, Promise &&promise); @@ -1322,9 +1320,6 @@ class ContactsManager : public Actor { void update_bot_info(BotInfo *bot_info, UserId user_id, bool send_update, bool from_database); - void on_create_channel_group_call(ChannelId channel_id, InputGroupCallId input_group_call_id, - Promise &&promise); - bool is_chat_full_outdated(const ChatFull *chat_full, const Chat *c, ChatId chat_id); bool is_user_contact(const User *u, UserId user_id) const; diff --git a/td/telegram/GroupCallManager.cpp b/td/telegram/GroupCallManager.cpp index e8e09dcfd..42e3e3edc 100644 --- a/td/telegram/GroupCallManager.cpp +++ b/td/telegram/GroupCallManager.cpp @@ -590,8 +590,69 @@ GroupCallManager::GroupCall *GroupCallManager::get_group_call(InputGroupCallId i } } -void GroupCallManager::create_voice_chat(DialogId dialog_id, Promise &&promise) { - td_->create_handler(std::move(promise))->send(dialog_id); +void GroupCallManager::create_voice_chat(DialogId dialog_id, Promise &&promise) { + if (!dialog_id.is_valid()) { + return promise.set_error(Status::Error(400, "Invalid chat identifier specified")); + } + if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + return promise.set_error(Status::Error(400, "Chat not found")); + } + + switch (dialog_id.get_type()) { + case DialogType::Channel: { + auto channel_id = dialog_id.get_channel_id(); + switch (td_->contacts_manager_->get_channel_type(channel_id)) { + case ChannelType::Unknown: + return promise.set_error(Status::Error(400, "Chat info not found")); + case ChannelType::Megagroup: + // OK + break; + case ChannelType::Broadcast: + return promise.set_error(Status::Error(400, "Chat is not a group")); + default: + UNREACHABLE(); + break; + } + if (!td_->contacts_manager_->get_channel_permissions(channel_id).can_manage_calls()) { + return promise.set_error(Status::Error(400, "Not enough rights in the chat")); + } + break; + } + case DialogType::Chat: + case DialogType::User: + case DialogType::SecretChat: + return promise.set_error(Status::Error(400, "Chat can't have a voice chat")); + default: + UNREACHABLE(); + } + + auto query_promise = PromiseCreator::lambda( + [actor_id = actor_id(this), 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, &GroupCallManager::on_voice_chat_created, dialog_id, result.move_as_ok(), + std::move(promise)); + } + }); + td_->create_handler(std::move(query_promise))->send(dialog_id); +} + +void GroupCallManager::on_voice_chat_created(DialogId dialog_id, InputGroupCallId input_group_call_id, + Promise &&promise) { + if (G()->close_flag()) { + return promise.set_error(Status::Error(500, "Request aborted")); + } + if (!input_group_call_id.is_valid()) { + return promise.set_error(Status::Error(500, "Receive invalid group call identifier")); + } + + td_->contacts_manager_->on_update_channel_group_call(dialog_id.get_channel_id(), true, true); + + // TODO + // td_->messages_manager_->on_update_chat_group_call(dialog_id, input_group_call_id); + + promise.set_value(get_group_call_id(input_group_call_id, dialog_id)); } void GroupCallManager::get_group_call(GroupCallId group_call_id, diff --git a/td/telegram/GroupCallManager.h b/td/telegram/GroupCallManager.h index 7f20531f1..b82a5f9da 100644 --- a/td/telegram/GroupCallManager.h +++ b/td/telegram/GroupCallManager.h @@ -37,7 +37,7 @@ class GroupCallManager : public Actor { GroupCallId get_group_call_id(InputGroupCallId input_group_call_id, DialogId dialog_id); - void create_voice_chat(DialogId dialog_id, Promise &&promise); + void create_voice_chat(DialogId dialog_id, Promise &&promise); void get_group_call(GroupCallId group_call_id, Promise> &&promise); @@ -109,6 +109,8 @@ class GroupCallManager : public Actor { const GroupCall *get_group_call(InputGroupCallId input_group_call_id) const; GroupCall *get_group_call(InputGroupCallId input_group_call_id); + void on_voice_chat_created(DialogId dialog_id, InputGroupCallId input_group_call_id, Promise &&promise); + void reload_group_call(InputGroupCallId input_group_call_id, Promise> &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index b2bc6baba..c81aa106e 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6037,7 +6037,7 @@ void Td::on_request(uint64 id, const td_api::createVoiceChat &request) { promise.set_value(td_api::make_object(result.ok().get())); } }); - contacts_manager_->create_channel_voice_chat(DialogId(request.chat_id_), std::move(query_promise)); + group_call_manager_->create_voice_chat(DialogId(request.chat_id_), std::move(query_promise)); } void Td::on_request(uint64 id, const td_api::getGroupCall &request) {