Simplify td_api::emojiCategory usage.

This commit is contained in:
levlam 2023-02-09 22:47:44 +03:00
parent 8e74b99e13
commit b895900e75
5 changed files with 99 additions and 24 deletions

View File

@ -2975,9 +2975,9 @@ trendingStickerSets total_count:int32 sets:vector<stickerSetInfo> is_premium:Boo
//@description Contains a list of similar emoji to search for in getStickers and searchStickers
//@name Name of the category
//@icon_custom_emoji_id Unique identifier of the custom emoji, which represents icon of the category
//@icon Custom emoji sticker, which represents icon of the category
//@emojis List of emojis in the category
emojiCategory name:string icon_custom_emoji_id:int64 emojis:vector<string> = EmojiCategory;
emojiCategory name:string icon:sticker emojis:vector<string> = EmojiCategory;
//@description Represents a list of emoji categories @categories List of categories
emojiCategories categories:vector<emojiCategory> = EmojiCategories;

View File

@ -5,6 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/EmojiGroup.h"
#include "td/telegram/StickersManager.h"
#include "td/utils/algorithm.h"
#include "td/utils/Time.h"
@ -17,8 +18,10 @@ EmojiGroup::EmojiGroup(telegram_api::object_ptr<telegram_api::emojiGroup> &&emoj
, emojis_(std::move(emoji_group->emoticons_)) {
}
td_api::object_ptr<td_api::emojiCategory> EmojiGroup::get_emoji_category_object() const {
return td_api::make_object<td_api::emojiCategory>(title_, icon_custom_emoji_id_.get(), vector<string>(emojis_));
td_api::object_ptr<td_api::emojiCategory> EmojiGroup::get_emoji_category_object(
StickersManager *stickers_manager) const {
return td_api::make_object<td_api::emojiCategory>(
title_, stickers_manager->get_custom_emoji_sticker_object(icon_custom_emoji_id_), vector<string>(emojis_));
}
EmojiGroupList::EmojiGroupList(string used_language_codes, int32 hash,
@ -32,9 +35,19 @@ EmojiGroupList::EmojiGroupList(string used_language_codes, int32 hash,
, next_reload_time_(Time::now() + 3600) {
}
td_api::object_ptr<td_api::emojiCategories> EmojiGroupList::get_emoji_categories_object() const {
return td_api::make_object<td_api::emojiCategories>(
transform(emoji_groups_, [](const EmojiGroup &emoji_group) { return emoji_group.get_emoji_category_object(); }));
td_api::object_ptr<td_api::emojiCategories> EmojiGroupList::get_emoji_categories_object(
StickersManager *stickers_manager) const {
auto emoji_categories = transform(emoji_groups_, [stickers_manager](const EmojiGroup &emoji_group) {
return emoji_group.get_emoji_category_object(stickers_manager);
});
td::remove_if(emoji_categories, [](const td_api::object_ptr<td_api::emojiCategory> &emoji_category) {
if (emoji_category->icon_ == nullptr) {
LOG(ERROR) << "Failed to get icon for emoji category " << emoji_category->name_;
return true;
}
return false;
});
return td_api::make_object<td_api::emojiCategories>(std::move(emoji_categories));
}
bool EmojiGroupList::is_expired() const {
@ -45,4 +58,8 @@ void EmojiGroupList::update_next_reload_time() {
next_reload_time_ = Time::now() + 3600;
}
vector<CustomEmojiId> EmojiGroupList::get_icon_custom_emoji_ids() const {
return transform(emoji_groups_, [](const EmojiGroup &emoji_group) { return emoji_group.get_icon_custom_emoji_id(); });
}
} // namespace td

View File

@ -14,6 +14,8 @@
namespace td {
class StickersManager;
class EmojiGroup {
string title_;
CustomEmojiId icon_custom_emoji_id_;
@ -24,7 +26,11 @@ class EmojiGroup {
explicit EmojiGroup(telegram_api::object_ptr<telegram_api::emojiGroup> &&emoji_group);
td_api::object_ptr<td_api::emojiCategory> get_emoji_category_object() const;
td_api::object_ptr<td_api::emojiCategory> get_emoji_category_object(StickersManager *stickers_manager) const;
CustomEmojiId get_icon_custom_emoji_id() const {
return icon_custom_emoji_id_;
}
template <class StorerT>
void store(StorerT &storer) const;
@ -45,7 +51,7 @@ class EmojiGroupList {
EmojiGroupList(string used_language_codes, int32 hash,
vector<telegram_api::object_ptr<telegram_api::emojiGroup>> &&emoji_groups);
td_api::object_ptr<td_api::emojiCategories> get_emoji_categories_object() const;
td_api::object_ptr<td_api::emojiCategories> get_emoji_categories_object(StickersManager *stickers_manager) const;
const string &get_used_language_codes() const {
return used_language_codes_;
@ -59,6 +65,8 @@ class EmojiGroupList {
void update_next_reload_time();
vector<CustomEmojiId> get_icon_custom_emoji_ids() const;
template <class StorerT>
void store(StorerT &storer) const;

View File

@ -2995,7 +2995,7 @@ FileId StickersManager::on_get_sticker(unique_ptr<Sticker> new_sticker, bool rep
if (s->emoji_receive_date_ < new_sticker->emoji_receive_date_) {
LOG(DEBUG) << "Update custom emoji file " << file_id << " receive date";
s->emoji_receive_date_ = new_sticker->emoji_receive_date_;
is_changed = true;
s->is_from_database_ = false;
}
if (is_changed) {
@ -6464,6 +6464,28 @@ void StickersManager::on_load_custom_emoji_from_database(CustomEmojiId custom_em
set_promises(promises);
}
td_api::object_ptr<td_api::sticker> StickersManager::get_custom_emoji_sticker_object(CustomEmojiId custom_emoji_id) {
auto file_id = custom_emoji_to_sticker_id_.get(custom_emoji_id);
if (!file_id.is_valid()) {
return nullptr;
}
auto s = get_sticker(file_id);
CHECK(s != nullptr);
CHECK(s->type_ == StickerType::CustomEmoji);
if (s->emoji_receive_date_ < G()->unix_time() - 86400 && !s->is_being_reloaded_) {
s->is_being_reloaded_ = true;
LOG(INFO) << "Reload " << custom_emoji_id;
auto promise = PromiseCreator::lambda(
[actor_id =
actor_id(this)](Result<vector<telegram_api::object_ptr<telegram_api::Document>>> r_documents) mutable {
send_closure(actor_id, &StickersManager::on_get_custom_emoji_documents, std::move(r_documents),
vector<CustomEmojiId>(), Promise<td_api::object_ptr<td_api::stickers>>());
});
td_->create_handler<GetCustomEmojiDocumentsQuery>(std::move(promise))->send({custom_emoji_id});
}
return get_sticker_object(file_id);
}
td_api::object_ptr<td_api::stickers> StickersManager::get_custom_emoji_stickers_object(
const vector<CustomEmojiId> &custom_emoji_ids) {
vector<td_api::object_ptr<td_api::sticker>> stickers;
@ -10168,7 +10190,7 @@ void StickersManager::get_emoji_groups(EmojiGroupType group_type,
auto used_language_codes = get_used_language_codes_string();
LOG(INFO) << "Have language codes " << used_language_codes;
if (emoji_group_list_[type].get_used_language_codes() == used_language_codes) {
promise.set_value(emoji_group_list_[type].get_emoji_categories_object());
promise.set_value(emoji_group_list_[type].get_emoji_categories_object(this));
if (!emoji_group_list_[type].is_expired()) {
return;
}
@ -10206,22 +10228,38 @@ void StickersManager::on_load_emoji_groups_from_database(EmojiGroupType group_ty
LOG(INFO) << "Successfully loaded emoji groups of type " << group_type << " from database";
auto type = static_cast<int32>(group_type);
auto status = log_event_parse(emoji_group_list_[type], value);
EmojiGroupList group_list;
auto status = log_event_parse(group_list, value);
if (status.is_error()) {
LOG(ERROR) << "Can't load emoji groups: " << status;
emoji_group_list_[type] = {};
return reload_emoji_groups(group_type, std::move(used_language_codes));
}
if (emoji_group_list_[type].get_used_language_codes() != used_language_codes) {
if (group_list.get_used_language_codes() != used_language_codes) {
return reload_emoji_groups(group_type, std::move(used_language_codes));
}
auto custom_emoji_ids = group_list.get_icon_custom_emoji_ids();
get_custom_emoji_stickers_unlimited(
std::move(custom_emoji_ids),
PromiseCreator::lambda([actor_id = actor_id(this), group_type, group_list = std::move(group_list)](
Result<td_api::object_ptr<td_api::stickers>> &&result) {
send_closure(actor_id, &StickersManager::on_load_emoji_group_icons, group_type, std::move(group_list));
}));
}
void StickersManager::on_load_emoji_group_icons(EmojiGroupType group_type, EmojiGroupList group_list) {
if (G()->close_flag()) {
return on_get_emoji_groups(group_type, group_list.get_used_language_codes(), Global::request_aborted_error());
}
auto type = static_cast<int32>(group_type);
emoji_group_list_[type] = std::move(group_list);
auto promises = std::move(emoji_group_load_queries_[type]);
reset_to_empty(emoji_group_load_queries_[type]);
for (auto &promise : promises) {
promise.set_value(emoji_group_list_[type].get_emoji_categories_object());
promise.set_value(emoji_group_list_[type].get_emoji_categories_object(this));
}
}
@ -10267,22 +10305,30 @@ void StickersManager::on_get_emoji_groups(
break;
case telegram_api::messages_emojiGroups::ID: {
auto groups = telegram_api::move_object_as<telegram_api::messages_emojiGroups>(emoji_groups);
emoji_group_list_[type] = EmojiGroupList(used_language_codes, groups->hash_, std::move(groups->groups_));
break;
EmojiGroupList group_list = EmojiGroupList(used_language_codes, groups->hash_, std::move(groups->groups_));
if (!used_language_codes.empty() && G()->parameters().use_file_db /* have SQLite PMC */) {
G()->td_db()->get_sqlite_pmc()->set(get_emoji_groups_database_key(group_type),
log_event_store(group_list).as_slice().str(), Auto());
}
auto custom_emoji_ids = group_list.get_icon_custom_emoji_ids();
get_custom_emoji_stickers_unlimited(
std::move(custom_emoji_ids),
PromiseCreator::lambda([actor_id = actor_id(this), group_type, group_list = std::move(group_list)](
Result<td_api::object_ptr<td_api::stickers>> &&result) {
send_closure(actor_id, &StickersManager::on_load_emoji_group_icons, group_type, std::move(group_list));
}));
return;
}
default:
UNREACHABLE();
}
if (!used_language_codes.empty() && G()->parameters().use_file_db /* have SQLite PMC */) {
G()->td_db()->get_sqlite_pmc()->set(get_emoji_groups_database_key(group_type),
log_event_store(emoji_group_list_[type]).as_slice().str(), Auto());
}
auto promises = std::move(emoji_group_load_queries_[type]);
reset_to_empty(emoji_group_load_queries_[type]);
for (auto &promise : promises) {
promise.set_value(emoji_group_list_[type].get_emoji_categories_object());
promise.set_value(emoji_group_list_[type].get_emoji_categories_object(this));
}
}

View File

@ -81,6 +81,8 @@ class StickersManager final : public Actor {
tl_object_ptr<td_api::stickers> get_stickers_object(const vector<FileId> &sticker_ids) const;
td_api::object_ptr<td_api::sticker> get_custom_emoji_sticker_object(CustomEmojiId custom_emoji_id);
tl_object_ptr<td_api::DiceStickers> get_dice_stickers_object(const string &emoji, int32 value) const;
int32 get_dice_success_animation_frame_number(const string &emoji, int32 value) const;
@ -1030,6 +1032,8 @@ class StickersManager final : public Actor {
void on_load_emoji_groups_from_database(EmojiGroupType group_type, string used_language_codes, string value);
void on_load_emoji_group_icons(EmojiGroupType group_type, EmojiGroupList group_list);
void reload_emoji_groups(EmojiGroupType group_type, string used_language_codes);
void on_get_emoji_groups(EmojiGroupType group_type, string used_language_codes,