Handle updateBotCommands.

This commit is contained in:
John Preston 2021-07-05 20:59:06 +03:00
parent 8f5ac0420e
commit 8fc7ba7ac1
7 changed files with 54 additions and 6 deletions

View file

@ -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 {

View file

@ -50,6 +50,12 @@ bool MegagroupInfo::updateBotCommands(const MTPVector<MTPBotInfo> &data) {
return Data::UpdateBotCommands(_botCommands, data);
}
bool MegagroupInfo::updateBotCommands(
UserId botId,
const MTPVector<MTPBotCommand> &data) {
return Data::UpdateBotCommands(_botCommands, botId, data);
}
ChannelData::ChannelData(not_null<Data::Session*> owner, PeerId id)
: PeerData(owner, id)
, inputChannel(

View file

@ -82,6 +82,9 @@ public:
void setLocation(const ChannelLocation &location);
bool updateBotCommands(const MTPVector<MTPBotInfo> &data);
bool updateBotCommands(
UserId botId,
const MTPVector<MTPBotCommand> &data);
[[nodiscard]] auto botCommands() const
-> const base::flat_map<UserId, std::vector<BotCommand>> & {
return _botCommands;

View file

@ -250,6 +250,14 @@ void ChatData::setBotCommands(const MTPVector<MTPBotInfo> &data) {
}
}
void ChatData::setBotCommands(
UserId botId,
const MTPVector<MTPBotCommand> &data) {
if (Data::UpdateBotCommands(_botCommands, botId, data)) {
owner().botCommandsChanged(this);
}
}
namespace Data {
void ApplyChatUpdate(

View file

@ -156,6 +156,9 @@ public:
[[nodiscard]] PeerId groupCallDefaultJoinAs() const;
void setBotCommands(const MTPVector<MTPBotInfo> &data);
void setBotCommands(
UserId botId,
const MTPVector<MTPBotCommand> &data);
[[nodiscard]] auto botCommands() const
-> const base::flat_map<UserId, std::vector<BotCommand>> & {
return _botCommands;

View file

@ -120,6 +120,15 @@ bool UpdateBotCommands(
return result;
}
bool UpdateBotCommands(
base::flat_map<UserId, std::vector<BotCommand>> &commands,
UserId botId,
const MTPVector<MTPBotCommand> &data) {
return data.v.isEmpty()
? commands.remove(botId)
: UpdateBotCommands(commands[botId], data);
}
bool UpdateBotCommands(
base::flat_map<UserId, std::vector<BotCommand>> &commands,
const MTPVector<MTPBotInfo> &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;
}
});

View file

@ -168,6 +168,10 @@ struct UnavailableReason {
bool UpdateBotCommands(
std::vector<BotCommand> &commands,
const MTPVector<MTPBotCommand> &data);
bool UpdateBotCommands(
base::flat_map<UserId, std::vector<BotCommand>> &commands,
UserId botId,
const MTPVector<MTPBotCommand> &data);
bool UpdateBotCommands(
base::flat_map<UserId, std::vector<BotCommand>> &commands,
const MTPVector<MTPBotInfo> &data);