diff --git a/Telegram/SourceFiles/api/api_updates.cpp b/Telegram/SourceFiles/api/api_updates.cpp index 5d3f45fba..314f65298 100644 --- a/Telegram/SourceFiles/api/api_updates.cpp +++ b/Telegram/SourceFiles/api/api_updates.cpp @@ -1889,6 +1889,26 @@ void Updates::feedUpdate(const MTPUpdate &update) { } } break; + case mtpc_updateBotCommands: { + const auto &d = update.c_updateBotCommands(); + if (const auto peer = session().data().peerLoaded(peerFromMTP(d.vpeer()))) { + const auto botId = UserId(d.vbot_id().v); + if (const auto user = peer->asUser()) { + if (user->isBot() && user->id == peerFromUser(botId)) { + if (Data::UpdateBotCommands(user->botInfo->commands, d.vcommands())) { + session().data().botCommandsChanged(user); + } + } + } else if (const auto chat = peer->asChat()) { + chat->setBotCommands(botId, d.vcommands()); + } else if (const auto megagroup = peer->asMegagroup()) { + if (megagroup->mgInfo->updateBotCommands(botId, d.vcommands())) { + session().data().botCommandsChanged(megagroup); + } + } + } + } break; + case mtpc_updateServiceNotification: { const auto &d = update.c_updateServiceNotification(); const auto text = TextWithEntities { diff --git a/Telegram/SourceFiles/data/data_channel.cpp b/Telegram/SourceFiles/data/data_channel.cpp index e3d3eacec..935b351d7 100644 --- a/Telegram/SourceFiles/data/data_channel.cpp +++ b/Telegram/SourceFiles/data/data_channel.cpp @@ -50,6 +50,12 @@ bool MegagroupInfo::updateBotCommands(const MTPVector &data) { return Data::UpdateBotCommands(_botCommands, data); } +bool MegagroupInfo::updateBotCommands( + UserId botId, + const MTPVector &data) { + return Data::UpdateBotCommands(_botCommands, botId, data); +} + ChannelData::ChannelData(not_null owner, PeerId id) : PeerData(owner, id) , inputChannel( diff --git a/Telegram/SourceFiles/data/data_channel.h b/Telegram/SourceFiles/data/data_channel.h index abe9e0bec..34eef9cf4 100644 --- a/Telegram/SourceFiles/data/data_channel.h +++ b/Telegram/SourceFiles/data/data_channel.h @@ -82,6 +82,9 @@ public: void setLocation(const ChannelLocation &location); bool updateBotCommands(const MTPVector &data); + bool updateBotCommands( + UserId botId, + const MTPVector &data); [[nodiscard]] auto botCommands() const -> const base::flat_map> & { return _botCommands; diff --git a/Telegram/SourceFiles/data/data_chat.cpp b/Telegram/SourceFiles/data/data_chat.cpp index ff6518a50..50af68af3 100644 --- a/Telegram/SourceFiles/data/data_chat.cpp +++ b/Telegram/SourceFiles/data/data_chat.cpp @@ -250,6 +250,14 @@ void ChatData::setBotCommands(const MTPVector &data) { } } +void ChatData::setBotCommands( + UserId botId, + const MTPVector &data) { + if (Data::UpdateBotCommands(_botCommands, botId, data)) { + owner().botCommandsChanged(this); + } +} + namespace Data { void ApplyChatUpdate( diff --git a/Telegram/SourceFiles/data/data_chat.h b/Telegram/SourceFiles/data/data_chat.h index 814512d80..6583a66e3 100644 --- a/Telegram/SourceFiles/data/data_chat.h +++ b/Telegram/SourceFiles/data/data_chat.h @@ -156,6 +156,9 @@ public: [[nodiscard]] PeerId groupCallDefaultJoinAs() const; void setBotCommands(const MTPVector &data); + void setBotCommands( + UserId botId, + const MTPVector &data); [[nodiscard]] auto botCommands() const -> const base::flat_map> & { return _botCommands; diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index 25f2eb0f7..f00d5806a 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -120,6 +120,15 @@ bool UpdateBotCommands( return result; } +bool UpdateBotCommands( + base::flat_map> &commands, + UserId botId, + const MTPVector &data) { + return data.v.isEmpty() + ? commands.remove(botId) + : UpdateBotCommands(commands[botId], data); +} + bool UpdateBotCommands( base::flat_map> &commands, const MTPVector &data) { @@ -132,12 +141,7 @@ bool UpdateBotCommands( if (!filled.emplace(id).second) { LOG(("API Error: Two BotInfo for a single bot.")); return; - } - if (data.vcommands().v.isEmpty()) { - if (commands.remove(id)) { - result = true; - } - } else if (UpdateBotCommands(commands[id], data.vcommands())) { + } else if (UpdateBotCommands(commands, id, data.vcommands())) { result = true; } }); diff --git a/Telegram/SourceFiles/data/data_peer.h b/Telegram/SourceFiles/data/data_peer.h index db621dadf..570a9a8d6 100644 --- a/Telegram/SourceFiles/data/data_peer.h +++ b/Telegram/SourceFiles/data/data_peer.h @@ -168,6 +168,10 @@ struct UnavailableReason { bool UpdateBotCommands( std::vector &commands, const MTPVector &data); +bool UpdateBotCommands( + base::flat_map> &commands, + UserId botId, + const MTPVector &data); bool UpdateBotCommands( base::flat_map> &commands, const MTPVector &data);