From f210906dcaf36d8416088205f67ff7126079261e Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 28 Jun 2021 18:32:29 +0300 Subject: [PATCH] Support updateBotCommands. --- td/telegram/ContactsManager.cpp | 93 +++++++++++++++++++++++++++++++-- td/telegram/ContactsManager.h | 6 ++- td/telegram/UpdatesManager.cpp | 2 + 3 files changed, 96 insertions(+), 5 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 990e5369c..def3b3b76 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -6052,6 +6052,90 @@ void ContactsManager::update_is_location_visible() { G()->shared_config().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; + } + + auto is_from_bot = [bot_user_id](const BotCommands &commands) { + return commands.get_bot_user_id() == bot_user_id; + }; + + 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); + } + break; + } + case DialogType::Chat: { + ChatId chat_id(dialog_id.get_chat_id()); + auto chat_full = get_chat_full(chat_id); + if (chat_full != nullptr) { + if (bot_commands.empty()) { + if (td::remove_if(chat_full->bot_commands, is_from_bot)) { + chat_full->is_changed = true; + } + } else { + BotCommands commands(bot_user_id, std::move(bot_commands)); + auto it = std::find_if(chat_full->bot_commands.begin(), chat_full->bot_commands.end(), is_from_bot); + if (it != chat_full->bot_commands.end()) { + if (*it != commands) { + *it = std::move(commands); + chat_full->is_changed = true; + } + } else { + chat_full->bot_commands.push_back(std::move(commands)); + chat_full->is_changed = true; + } + } + update_chat_full(chat_full, chat_id); + } + break; + } + case DialogType::Channel: { + ChannelId channel_id(dialog_id.get_channel_id()); + auto channel_full = get_channel_full(channel_id, "on_update_bot_commands"); + if (channel_full != nullptr) { + if (bot_commands.empty()) { + if (td::remove_if(channel_full->bot_commands, is_from_bot)) { + channel_full->is_changed = true; + } + } else { + BotCommands commands(bot_user_id, std::move(bot_commands)); + auto it = std::find_if(channel_full->bot_commands.begin(), channel_full->bot_commands.end(), is_from_bot); + if (it != channel_full->bot_commands.end()) { + if (*it != commands) { + *it = std::move(commands); + channel_full->is_changed = true; + } + } else { + channel_full->bot_commands.push_back(std::move(commands)); + channel_full->is_changed = true; + } + } + update_channel_full(channel_full, channel_id); + } + break; + } + case DialogType::SecretChat: + default: + LOG(ERROR) << "Receive updateBotCommands in " << dialog_id; + break; + } +} + FileId ContactsManager::get_profile_photo_file_id(int64 photo_id) const { auto it = my_photo_file_id_.find(photo_id); if (it == my_photo_file_id_.end()) { @@ -10069,9 +10153,7 @@ void ContactsManager::on_get_user_full(tl_object_ptr &&u if (user->bot_info_ != nullptr && !td_->auth_manager_->is_bot()) { description = std::move(user->bot_info_->description_); - auto commands = transform(std::move(user->bot_info_->commands_), - [](auto &&bot_command) { return BotCommand(std::move(bot_command)); }); - on_update_user_full_commands(user_full, user_id, std::move(commands)); + on_update_user_full_commands(user_full, user_id, std::move(user->bot_info_->commands_)); } if (user_full->description != description) { user_full->description = std::move(description); @@ -11000,8 +11082,11 @@ void ContactsManager::on_update_user_full_common_chat_count(UserFull *user_full, } } -void ContactsManager::on_update_user_full_commands(UserFull *user_full, UserId user_id, vector &&commands) { +void ContactsManager::on_update_user_full_commands(UserFull *user_full, UserId user_id, + vector> &&bot_commands) { CHECK(user_full != nullptr); + auto commands = + transform(std::move(bot_commands), [](auto &&bot_command) { return BotCommand(std::move(bot_command)); }); if (user_full->commands != commands) { user_full->commands = std::move(commands); user_full->is_changed = true; diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 24bedbb56..69e8eb179 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -206,6 +206,9 @@ class ContactsManager : public Actor { 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_dialog_administrators(DialogId dialog_id, vector &&administrators, bool have_access, bool from_database); @@ -1178,7 +1181,8 @@ class ContactsManager : public Actor { void on_update_user_full_is_blocked(UserFull *user_full, UserId user_id, bool is_blocked); void on_update_user_full_common_chat_count(UserFull *user_full, UserId user_id, int32 common_chat_count); - void on_update_user_full_commands(UserFull *user_full, UserId user_id, vector &&commands); + void on_update_user_full_commands(UserFull *user_full, UserId user_id, + vector> &&bot_commands); void on_update_user_full_need_phone_number_privacy_exception(UserFull *user_full, UserId user_id, bool need_phone_number_privacy_exception); diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 6c702a5ed..c8b1796b1 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -2614,6 +2614,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_)); promise.set_value(Unit()); }