From 919a1d83295f889df39d732b8a19b8a2c302d63f Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 9 Mar 2024 23:35:12 +0300 Subject: [PATCH] Move on_update_bot_commands to DialogManager. --- td/telegram/ContactsManager.cpp | 79 +++++++++++---------------------- td/telegram/ContactsManager.h | 7 +-- td/telegram/DialogManager.cpp | 34 ++++++++++++++ td/telegram/DialogManager.h | 3 ++ td/telegram/UpdatesManager.cpp | 4 +- 5 files changed, 70 insertions(+), 57 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index e17a989cb..64d22c79a 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -6461,61 +6461,10 @@ void ContactsManager::update_is_location_visible() { td_->option_manager_->set_option_boolean("is_location_visible", expire_date != 0); } -void ContactsManager::on_update_bot_commands(DialogId dialog_id, UserId bot_user_id, - vector> &&bot_commands) { - if (!bot_user_id.is_valid()) { - LOG(ERROR) << "Receive updateBotCOmmands about invalid " << bot_user_id; - return; - } - if (!have_user(bot_user_id) || !is_user_bot(bot_user_id)) { - return; - } - if (td_->auth_manager_->is_bot()) { - return; - } - - switch (dialog_id.get_type()) { - case DialogType::User: { - UserId user_id(dialog_id.get_user_id()); - auto user_full = get_user_full(user_id); - if (user_full != nullptr) { - on_update_user_full_commands(user_full, user_id, std::move(bot_commands)); - update_user_full(user_full, user_id, "on_update_bot_commands"); - } - break; - } - case DialogType::Chat: { - ChatId chat_id(dialog_id.get_chat_id()); - auto chat_full = get_chat_full(chat_id); - if (chat_full != nullptr && BotCommands::update_all_bot_commands( - chat_full->bot_commands, BotCommands(bot_user_id, std::move(bot_commands)))) { - chat_full->is_changed = true; - update_chat_full(chat_full, chat_id, "on_update_bot_commands"); - } - break; - } - case DialogType::Channel: { - ChannelId channel_id(dialog_id.get_channel_id()); - auto channel_full = get_channel_full(channel_id, true, "on_update_bot_commands"); - if (channel_full != nullptr && - BotCommands::update_all_bot_commands(channel_full->bot_commands, - BotCommands(bot_user_id, std::move(bot_commands)))) { - channel_full->is_changed = true; - update_channel_full(channel_full, channel_id, "on_update_bot_commands"); - } - break; - } - case DialogType::SecretChat: - default: - LOG(ERROR) << "Receive updateBotCommands in " << dialog_id; - break; - } -} - void ContactsManager::on_update_bot_menu_button(UserId bot_user_id, tl_object_ptr &&bot_menu_button) { if (!bot_user_id.is_valid()) { - LOG(ERROR) << "Receive updateBotCOmmands about invalid " << bot_user_id; + LOG(ERROR) << "Receive updateBotMenuButton about invalid " << bot_user_id; return; } if (!have_user_force(bot_user_id, "on_update_bot_menu_button") || !is_user_bot(bot_user_id)) { @@ -12772,6 +12721,15 @@ void ContactsManager::on_update_user_full_greeting_message(UserFull *user_full, } } +void ContactsManager::on_update_user_commands( + UserId user_id, vector> &&bot_commands) { + UserFull *user_full = get_user_full_force(user_id, "on_update_user_commands"); + if (user_full != nullptr) { + on_update_user_full_commands(user_full, user_id, std::move(bot_commands)); + update_user_full(user_full, user_id, "on_update_user_commands"); + } +} + void ContactsManager::on_update_user_full_commands(UserFull *user_full, UserId user_id, vector> &&bot_commands) { CHECK(user_full != nullptr); @@ -14404,6 +14362,14 @@ void ContactsManager::on_update_channel_photo(Channel *c, ChannelId channel_id, true); } +void ContactsManager::on_update_chat_bot_commands(ChatId chat_id, BotCommands &&bot_commands) { + auto chat_full = get_chat_full_force(chat_id, "on_update_chat_bot_commands"); + if (chat_full != nullptr && BotCommands::update_all_bot_commands(chat_full->bot_commands, std::move(bot_commands))) { + chat_full->is_changed = true; + update_chat_full(chat_full, chat_id, "on_update_chat_bot_commands"); + } +} + void ContactsManager::on_update_channel_photo(Channel *c, ChannelId channel_id, DialogPhoto &&photo, bool invalidate_photo_cache) { if (td_->auth_manager_->is_bot()) { @@ -16343,6 +16309,15 @@ void ContactsManager::on_update_channel_administrator_count(ChannelId channel_id } } +void ContactsManager::on_update_channel_bot_commands(ChannelId channel_id, BotCommands &&bot_commands) { + auto channel_full = get_channel_full_force(channel_id, true, "on_update_channel_bot_commands"); + if (channel_full != nullptr && + BotCommands::update_all_bot_commands(channel_full->bot_commands, std::move(bot_commands))) { + channel_full->is_changed = true; + update_channel_full(channel_full, channel_id, "on_update_channel_bot_commands"); + } +} + void ContactsManager::on_get_chat_empty(telegram_api::chatEmpty &chat, const char *source) { ChatId chat_id(chat.id_); if (!chat_id.is_valid()) { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 3707c5870..8d842040b 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -258,6 +258,8 @@ class ContactsManager final : public Actor { void on_update_user_greeting_message(UserId user_id, BusinessGreetingMessage &&greeting_message); void on_update_user_need_phone_number_privacy_exception(UserId user_id, bool need_phone_number_privacy_exception); void on_update_user_wallpaper_overridden(UserId user_id, bool wallpaper_overridden); + void on_update_user_commands(UserId user_id, + vector> &&bot_commands); void on_set_profile_photo(UserId user_id, tl_object_ptr &&photo, bool is_fallback, int64 old_photo_id, Promise &&promise); @@ -273,6 +275,7 @@ class ContactsManager final : public Actor { void on_update_chat_delete_user(ChatId chat_id, UserId user_id, int32 version); void on_update_chat_default_permissions(ChatId chat_id, RestrictedRights default_permissions, int32 version); void on_update_chat_pinned_message(ChatId chat_id, MessageId pinned_message_id, int32 version); + void on_update_chat_bot_commands(ChatId chat_id, BotCommands &&bot_commands); void on_update_channel_participant_count(ChannelId channel_id, int32 participant_count); void on_update_channel_editable_username(ChannelId channel_id, string &&username); @@ -297,12 +300,10 @@ class ContactsManager final : public Actor { void on_update_channel_has_pinned_stories(ChannelId channel_id, bool has_pinned_stories); void on_update_channel_default_permissions(ChannelId channel_id, RestrictedRights default_permissions); void on_update_channel_administrator_count(ChannelId channel_id, int32 administrator_count); + void on_update_channel_bot_commands(ChannelId channel_id, BotCommands &&bot_commands); int32 on_update_peer_located(vector> &&peers, bool from_update); - void on_update_bot_commands(DialogId dialog_id, UserId bot_user_id, - vector> &&bot_commands); - void on_update_bot_menu_button(UserId bot_user_id, tl_object_ptr &&bot_menu_button); void speculative_add_channel_participants(ChannelId channel_id, const vector &added_user_ids, diff --git a/td/telegram/DialogManager.cpp b/td/telegram/DialogManager.cpp index ee2152d18..91478c184 100644 --- a/td/telegram/DialogManager.cpp +++ b/td/telegram/DialogManager.cpp @@ -1790,6 +1790,40 @@ bool DialogManager::is_dialog_removed_from_dialog_list(DialogId dialog_id) const return false; } +void DialogManager::on_update_dialog_bot_commands( + DialogId dialog_id, UserId bot_user_id, vector> &&bot_commands) { + if (!bot_user_id.is_valid()) { + LOG(ERROR) << "Receive updateBotCommands about invalid " << bot_user_id; + return; + } + if (!td_->contacts_manager_->have_user_force(bot_user_id, "on_update_dialog_bot_commands") || + !td_->contacts_manager_->is_user_bot(bot_user_id)) { + return; + } + if (td_->auth_manager_->is_bot()) { + return; + } + + switch (dialog_id.get_type()) { + case DialogType::User: + if (DialogId(bot_user_id) != dialog_id) { + LOG(ERROR) << "Receive commands of " << bot_user_id << " in " << dialog_id; + return; + } + return td_->contacts_manager_->on_update_user_commands(bot_user_id, std::move(bot_commands)); + case DialogType::Chat: + return td_->contacts_manager_->on_update_chat_bot_commands(dialog_id.get_chat_id(), + BotCommands(bot_user_id, std::move(bot_commands))); + case DialogType::Channel: + return td_->contacts_manager_->on_update_channel_bot_commands(dialog_id.get_channel_id(), + BotCommands(bot_user_id, std::move(bot_commands))); + case DialogType::SecretChat: + default: + LOG(ERROR) << "Receive updateBotCommands in " << dialog_id; + break; + } +} + void DialogManager::on_dialog_usernames_updated(DialogId dialog_id, const Usernames &old_usernames, const Usernames &new_usernames) { LOG(INFO) << "Update usernames in " << dialog_id << " from " << old_usernames << " to " << new_usernames; diff --git a/td/telegram/DialogManager.h b/td/telegram/DialogManager.h index 83b7f9a49..bbf8ec14e 100644 --- a/td/telegram/DialogManager.h +++ b/td/telegram/DialogManager.h @@ -179,6 +179,9 @@ class DialogManager final : public Actor { void upload_dialog_photo(DialogId dialog_id, FileId file_id, bool is_animation, double main_frame_timestamp, bool is_reupload, Promise &&promise, vector bad_parts = {}); + void on_update_dialog_bot_commands(DialogId dialog_id, UserId bot_user_id, + vector> &&bot_commands); + void on_dialog_usernames_updated(DialogId dialog_id, const Usernames &old_usernames, const Usernames &new_usernames); void on_dialog_usernames_received(DialogId dialog_id, const Usernames &usernames, bool from_database); diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 0e5a12bd2..72f4ab380 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -3981,8 +3981,8 @@ void UpdatesManager::on_update(tl_object_ptr up } void UpdatesManager::on_update(tl_object_ptr update, Promise &&promise) { - td_->contacts_manager_->on_update_bot_commands(DialogId(update->peer_), UserId(update->bot_id_), - std::move(update->commands_)); + td_->dialog_manager_->on_update_dialog_bot_commands(DialogId(update->peer_), UserId(update->bot_id_), + std::move(update->commands_)); promise.set_value(Unit()); }