2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

Moved out BotCommand struct to separated file.

This commit is contained in:
23rd
2022-05-14 23:50:02 +03:00
committed by John Preston
parent 8347318c90
commit 5f8608ed90
16 changed files with 160 additions and 105 deletions

View File

@@ -88,88 +88,6 @@ PeerId FakePeerIdForJustName(const QString &name) {
return peerFromUser(kShift + std::abs(base));
}
bool UpdateBotCommands(
std::vector<BotCommand> &commands,
const MTPVector<MTPBotCommand> *data) {
if (!data) {
const auto changed = !commands.empty();
commands.clear();
return changed;
}
const auto &v = data->v;
commands.reserve(v.size());
auto result = false;
auto index = 0;
for (const auto &command : v) {
command.match([&](const MTPDbotCommand &data) {
const auto command = qs(data.vcommand());
const auto description = qs(data.vdescription());
if (commands.size() <= index) {
commands.push_back({
.command = command,
.description = description,
});
result = true;
} else {
auto &entry = commands[index];
if (entry.command != command
|| entry.description != description) {
entry.command = command;
entry.description = description;
result = true;
}
}
++index;
});
}
if (index < commands.size()) {
result = true;
}
commands.resize(index);
return result;
}
bool UpdateBotCommands(
base::flat_map<UserId, std::vector<BotCommand>> &commands,
UserId botId,
const MTPVector<MTPBotCommand> *data) {
return (!data || data->v.isEmpty())
? commands.remove(botId)
: UpdateBotCommands(commands[botId], data);
}
bool UpdateBotCommands(
base::flat_map<UserId, std::vector<BotCommand>> &commands,
const MTPVector<MTPBotInfo> &data) {
auto result = false;
auto filled = base::flat_set<UserId>();
filled.reserve(data.v.size());
for (const auto &item : data.v) {
item.match([&](const MTPDbotInfo &data) {
if (!data.vuser_id()) {
LOG(("API Error: BotInfo without UserId for commands map."));
return;
}
const auto id = UserId(*data.vuser_id());
if (!filled.emplace(id).second) {
LOG(("API Error: Two BotInfo for a single bot."));
return;
} else if (UpdateBotCommands(commands, id, data.vcommands())) {
result = true;
}
});
}
for (auto i = begin(commands); i != end(commands);) {
if (filled.contains(i->first)) {
++i;
} else {
i = commands.erase(i);
result = true;
}
}
return result;
}
bool ApplyBotMenuButton(
not_null<BotInfo*> info,
const MTPBotMenuButton *button) {