Add AttachMenuManager::get_attach_menu_bot function.

This commit is contained in:
levlam 2022-03-29 13:47:24 +03:00
parent ccbedd39bf
commit 8736193034
2 changed files with 73 additions and 53 deletions

View File

@ -89,7 +89,8 @@ bool operator==(const AttachMenuManager::AttachMenuBot &lhs, const AttachMenuMan
lhs.default_icon_file_id_ == rhs.default_icon_file_id_ &&
lhs.ios_static_icon_file_id_ == rhs.ios_static_icon_file_id_ &&
lhs.ios_animated_icon_file_id_ == rhs.ios_animated_icon_file_id_ &&
lhs.android_icon_file_id_ == rhs.android_icon_file_id_ && lhs.macos_icon_file_id_ == rhs.macos_icon_file_id_;
lhs.android_icon_file_id_ == rhs.android_icon_file_id_ && lhs.macos_icon_file_id_ == rhs.macos_icon_file_id_ &&
lhs.is_added_ == rhs.is_added_;
}
bool operator!=(const AttachMenuManager::AttachMenuBot &lhs, const AttachMenuManager::AttachMenuBot &rhs) {
@ -107,6 +108,7 @@ void AttachMenuManager::AttachMenuBot::store(StorerT &storer) const {
STORE_FLAG(has_ios_animated_icon_file_id);
STORE_FLAG(has_android_icon_file_id);
STORE_FLAG(has_macos_icon_file_id);
STORE_FLAG(is_added_);
END_STORE_FLAGS();
td::store(user_id_, storer);
td::store(name_, storer);
@ -136,6 +138,7 @@ void AttachMenuManager::AttachMenuBot::parse(ParserT &parser) {
PARSE_FLAG(has_ios_animated_icon_file_id);
PARSE_FLAG(has_android_icon_file_id);
PARSE_FLAG(has_macos_icon_file_id);
PARSE_FLAG(is_added_);
END_PARSE_FLAGS();
td::parse(user_id_, parser);
td::parse(name_, parser);
@ -252,56 +255,22 @@ void AttachMenuManager::reload_attach_menu_bots() {
td_->create_handler<GetAttachMenuBotsQuery>(std::move(promise))->send(hash_);
}
void AttachMenuManager::on_reload_attach_menu_bots(
Result<telegram_api::object_ptr<telegram_api::AttachMenuBots>> &&result) {
if (!is_active()) {
return;
}
if (result.is_error()) {
set_timeout_in(Random::fast(60, 120));
return;
}
is_inited_ = true;
set_timeout_in(Random::fast(3600, 4800));
auto attach_menu_bots_ptr = result.move_as_ok();
auto constructor_id = attach_menu_bots_ptr->get_id();
if (constructor_id == telegram_api::attachMenuBotsNotModified::ID) {
return;
}
CHECK(constructor_id == telegram_api::attachMenuBots::ID);
auto attach_menu_bots = move_tl_object_as<telegram_api::attachMenuBots>(attach_menu_bots_ptr);
td_->contacts_manager_->on_get_users(std::move(attach_menu_bots->users_), "on_reload_attach_menu_bots");
auto new_hash = attach_menu_bots->hash_;
vector<AttachMenuBot> new_attach_menu_bots;
for (auto &bot : attach_menu_bots->bots_) {
Result<AttachMenuManager::AttachMenuBot> AttachMenuManager::get_attach_menu_bot(
tl_object_ptr<telegram_api::attachMenuBot> &&bot) const {
UserId user_id(bot->bot_id_);
if (!td_->contacts_manager_->have_user(user_id)) {
LOG(ERROR) << "Have no information about " << user_id;
new_hash = 0;
continue;
}
if (bot->inactive_) {
LOG(ERROR) << "Receive inactive attach menu bot " << user_id;
new_hash = 0;
continue;
return Status::Error(PSLICE() << "Have no information about " << user_id);
}
AttachMenuBot attach_menu_bot;
attach_menu_bot.is_added_ = !bot->inactive_;
attach_menu_bot.user_id_ = user_id;
attach_menu_bot.name_ = std::move(bot->short_name_);
for (auto &icon : bot->icons_) {
Slice name = icon->name_;
int32 document_id = icon->icon_->get_id();
if (document_id == telegram_api::documentEmpty::ID) {
LOG(ERROR) << "Have no icon for " << user_id << " with name " << name;
new_hash = 0;
continue;
return Status::Error(PSLICE() << "Have no icon for " << user_id << " with name " << name);
}
CHECK(document_id == telegram_api::document::ID);
@ -338,7 +307,55 @@ void AttachMenuManager::on_reload_attach_menu_bots(
UNREACHABLE();
}
}
new_attach_menu_bots.push_back(std::move(attach_menu_bot));
if (!attach_menu_bot.default_icon_file_id_.is_valid()) {
return Status::Error(PSLICE() << "Have no default icon for " << user_id);
}
return std::move(attach_menu_bot);
}
void AttachMenuManager::on_reload_attach_menu_bots(
Result<telegram_api::object_ptr<telegram_api::AttachMenuBots>> &&result) {
if (!is_active()) {
return;
}
if (result.is_error()) {
set_timeout_in(Random::fast(60, 120));
return;
}
is_inited_ = true;
set_timeout_in(Random::fast(3600, 4800));
auto attach_menu_bots_ptr = result.move_as_ok();
auto constructor_id = attach_menu_bots_ptr->get_id();
if (constructor_id == telegram_api::attachMenuBotsNotModified::ID) {
return;
}
CHECK(constructor_id == telegram_api::attachMenuBots::ID);
auto attach_menu_bots = move_tl_object_as<telegram_api::attachMenuBots>(attach_menu_bots_ptr);
td_->contacts_manager_->on_get_users(std::move(attach_menu_bots->users_), "on_reload_attach_menu_bots");
auto new_hash = attach_menu_bots->hash_;
vector<AttachMenuBot> new_attach_menu_bots;
for (auto &bot : attach_menu_bots->bots_) {
auto r_attach_menu_bot = get_attach_menu_bot(std::move(bot));
if (r_attach_menu_bot.is_error()) {
LOG(ERROR) << r_attach_menu_bot.error().message();
new_hash = 0;
continue;
}
if (!r_attach_menu_bot.ok().is_added_) {
LOG(ERROR) << "Receive non-added attach menu bot " << r_attach_menu_bot.ok().user_id_;
new_hash = 0;
continue;
}
new_attach_menu_bots.push_back(r_attach_menu_bot.move_as_ok());
}
bool need_update = new_attach_menu_bots != attach_menu_bots_;

View File

@ -43,6 +43,7 @@ class AttachMenuManager final : public Actor {
bool is_active() const;
struct AttachMenuBot {
bool is_added_ = false;
UserId user_id_;
string name_;
FileId default_icon_file_id_;
@ -64,6 +65,8 @@ class AttachMenuManager final : public Actor {
friend bool operator!=(const AttachMenuBot &lhs, const AttachMenuBot &rhs);
Result<AttachMenuBot> get_attach_menu_bot(tl_object_ptr<telegram_api::attachMenuBot> &&bot) const;
td_api::object_ptr<td_api::updateAttachMenuBots> get_update_attach_menu_bots_object() const;
void send_update_attach_menu_bots() const;