Add and use BotCommands::update_all_bot_commands.

This commit is contained in:
levlam 2024-01-08 01:44:58 +03:00
parent b4ac2653cd
commit 90b8acff67
3 changed files with 30 additions and 40 deletions

View File

@ -138,6 +138,26 @@ td_api::object_ptr<td_api::botCommands> BotCommands::get_bot_commands_object(Td
td->contacts_manager_->get_user_id_object(bot_user_id_, "get_bot_commands_object"), std::move(commands));
}
bool BotCommands::update_all_bot_commands(vector<BotCommands> &all_bot_commands, BotCommands &&bot_commands) {
auto is_from_bot = [bot_user_id = bot_commands.bot_user_id_](const BotCommands &commands) {
return commands.bot_user_id_ == bot_user_id;
};
if (bot_commands.commands_.empty()) {
return td::remove_if(all_bot_commands, is_from_bot);
}
auto it = std::find_if(all_bot_commands.begin(), all_bot_commands.end(), is_from_bot);
if (it != all_bot_commands.end()) {
if (*it != bot_commands) {
*it = std::move(bot_commands);
return true;
}
return false;
}
all_bot_commands.push_back(std::move(bot_commands));
return true;
}
bool operator==(const BotCommands &lhs, const BotCommands &rhs) {
return lhs.bot_user_id_ == rhs.bot_user_id_ && lhs.commands_ == rhs.commands_;
}

View File

@ -61,10 +61,13 @@ class BotCommands {
public:
BotCommands() = default;
BotCommands(UserId bot_user_id, vector<telegram_api::object_ptr<telegram_api::botCommand>> &&bot_commands);
td_api::object_ptr<td_api::botCommands> get_bot_commands_object(Td *td) const;
static bool update_all_bot_commands(vector<BotCommands> &all_bot_commands, BotCommands &&bot_commands);
UserId get_bot_user_id() const {
return bot_user_id_;
}

View File

@ -7730,10 +7730,6 @@ void ContactsManager::on_update_bot_commands(DialogId dialog_id, UserId bot_user
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());
@ -7747,24 +7743,9 @@ void ContactsManager::on_update_bot_commands(DialogId dialog_id, UserId bot_user
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;
}
}
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;
@ -7772,24 +7753,10 @@ void ContactsManager::on_update_bot_commands(DialogId dialog_id, UserId bot_user
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) {
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;
}
}
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;