Load custom emoji from database,

This commit is contained in:
levlam 2022-07-25 23:04:53 +03:00
parent f20f015caa
commit e96a1995a2
3 changed files with 68 additions and 3 deletions

View File

@ -2510,6 +2510,7 @@ FileId StickersManager::on_get_sticker(unique_ptr<Sticker> new_sticker, bool rep
is_changed = true; is_changed = true;
} }
if (s->emoji_receive_date < new_sticker->emoji_receive_date) { 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; s->emoji_receive_date = new_sticker->emoji_receive_date;
is_changed = true; is_changed = true;
} }
@ -5138,8 +5139,49 @@ void StickersManager::get_all_animated_emojis(bool is_recursive,
promise.set_value(td_api::make_object<td_api::emojis>(std::move(emojis))); promise.set_value(td_api::make_object<td_api::emojis>(std::move(emojis)));
} }
void StickersManager::get_custom_emoji_stickers(vector<int64> &&document_ids, void StickersManager::load_custom_emoji_sticker_from_database(int64 custom_emoji_id, Promise<Unit> &&promise) {
auto &queries = custom_emoji_load_queries_[custom_emoji_id];
queries.push_back(std::move(promise));
if (queries.size() == 1) {
LOG(INFO) << "Trying to load custom emoji " << custom_emoji_id << " from database";
G()->td_db()->get_sqlite_pmc()->get(
get_custom_emoji_database_key(custom_emoji_id), PromiseCreator::lambda([custom_emoji_id](string value) {
send_closure(G()->stickers_manager(), &StickersManager::on_load_custom_emoji_from_database, custom_emoji_id,
std::move(value));
}));
}
}
void StickersManager::on_load_custom_emoji_from_database(int64 custom_emoji_id, string value) {
if (G()->close_flag()) {
return;
}
if (!value.empty()) {
LOG(INFO) << "Successfully loaded custom emoji " << custom_emoji_id << " of size " << value.size()
<< " from database";
CustomEmojiLogEvent log_event;
if (log_event_parse(log_event, value).is_error()) {
LOG(ERROR) << "Delete invalid custom emoji " << custom_emoji_id << " value from database";
G()->td_db()->get_sqlite_pmc()->erase(get_custom_emoji_database_key(custom_emoji_id), Auto());
}
} else {
LOG(INFO) << "Failed to load custom emoji " << custom_emoji_id << " from database";
}
auto it = custom_emoji_load_queries_.find(custom_emoji_id);
CHECK(it != custom_emoji_load_queries_.end());
CHECK(!it->second.empty());
auto promises = std::move(it->second);
custom_emoji_load_queries_.erase(it);
set_promises(promises);
}
void StickersManager::get_custom_emoji_stickers(vector<int64> &&document_ids, bool use_database,
Promise<td_api::object_ptr<td_api::stickers>> &&promise) { Promise<td_api::object_ptr<td_api::stickers>> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
vector<int64> unknown_document_ids; vector<int64> unknown_document_ids;
for (auto document_id : document_ids) { for (auto document_id : document_ids) {
if (custom_emoji_to_sticker_id_.count(document_id) == 0) { if (custom_emoji_to_sticker_id_.count(document_id) == 0) {
@ -5157,6 +5199,22 @@ void StickersManager::get_custom_emoji_stickers(vector<int64> &&document_ids,
return promise.set_value(td_api::make_object<td_api::stickers>(std::move(stickers))); return promise.set_value(td_api::make_object<td_api::stickers>(std::move(stickers)));
} }
if (use_database && G()->parameters().use_file_db) {
MultiPromiseActorSafe mpas{"LoadCustomEmojiMultiPromiseActor"};
mpas.add_promise(
PromiseCreator::lambda([actor_id = actor_id(this), document_ids, promise = std::move(promise)](Unit) mutable {
send_closure(actor_id, &StickersManager::get_custom_emoji_stickers, std::move(document_ids), false,
std::move(promise));
}));
auto lock = mpas.get_promise();
for (auto document_id : unknown_document_ids) {
load_custom_emoji_sticker_from_database(document_id, mpas.get_promise());
}
return lock.set_value(Unit());
}
auto query_promise = auto query_promise =
PromiseCreator::lambda([actor_id = actor_id(this), promise = std::move(promise)]( PromiseCreator::lambda([actor_id = actor_id(this), promise = std::move(promise)](
Result<vector<telegram_api::object_ptr<telegram_api::Document>>> r_documents) mutable { Result<vector<telegram_api::object_ptr<telegram_api::Document>>> r_documents) mutable {

View File

@ -101,7 +101,8 @@ class StickersManager final : public Actor {
void get_all_animated_emojis(bool is_recursive, Promise<td_api::object_ptr<td_api::emojis>> &&promise); void get_all_animated_emojis(bool is_recursive, Promise<td_api::object_ptr<td_api::emojis>> &&promise);
void get_custom_emoji_stickers(vector<int64> &&document_ids, Promise<td_api::object_ptr<td_api::stickers>> &&promise); void get_custom_emoji_stickers(vector<int64> &&document_ids, bool use_database,
Promise<td_api::object_ptr<td_api::stickers>> &&promise);
void get_premium_gift_option_sticker(int32 month_count, bool is_recursive, void get_premium_gift_option_sticker(int32 month_count, bool is_recursive,
Promise<td_api::object_ptr<td_api::sticker>> &&promise); Promise<td_api::object_ptr<td_api::sticker>> &&promise);
@ -541,6 +542,10 @@ class StickersManager final : public Actor {
string get_custom_emoji_database_key(int64 custom_emoji_id); string get_custom_emoji_database_key(int64 custom_emoji_id);
void load_custom_emoji_sticker_from_database(int64 custom_emoji_id, Promise<Unit> &&promise);
void on_load_custom_emoji_from_database(int64 custom_emoji_id, string value);
FileId on_get_sticker(unique_ptr<Sticker> new_sticker, bool replace); FileId on_get_sticker(unique_ptr<Sticker> new_sticker, bool replace);
StickerSet *get_sticker_set(StickerSetId sticker_set_id); StickerSet *get_sticker_set(StickerSetId sticker_set_id);
@ -919,6 +924,8 @@ class StickersManager final : public Actor {
FlatHashMap<uint32, StickerSetLoadRequest> sticker_set_load_requests_; FlatHashMap<uint32, StickerSetLoadRequest> sticker_set_load_requests_;
uint32 current_sticker_set_load_request_ = 0; uint32 current_sticker_set_load_request_ = 0;
FlatHashMap<int64, vector<Promise<Unit>>> custom_emoji_load_queries_;
FlatHashMap<int64, unique_ptr<PendingNewStickerSet>> pending_new_sticker_sets_; FlatHashMap<int64, unique_ptr<PendingNewStickerSet>> pending_new_sticker_sets_;
FlatHashMap<int64, unique_ptr<PendingAddStickerToSet>> pending_add_sticker_to_sets_; FlatHashMap<int64, unique_ptr<PendingAddStickerToSet>> pending_add_sticker_to_sets_;

View File

@ -7189,7 +7189,7 @@ void Td::on_request(uint64 id, td_api::getEmojiSuggestionsUrl &request) {
void Td::on_request(uint64 id, td_api::getCustomEmojiStickers &request) { void Td::on_request(uint64 id, td_api::getCustomEmojiStickers &request) {
CREATE_REQUEST_PROMISE(); CREATE_REQUEST_PROMISE();
stickers_manager_->get_custom_emoji_stickers(std::move(request.custom_emoji_ids_), std::move(promise)); stickers_manager_->get_custom_emoji_stickers(std::move(request.custom_emoji_ids_), true, std::move(promise));
} }
void Td::on_request(uint64 id, const td_api::getSavedAnimations &request) { void Td::on_request(uint64 id, const td_api::getSavedAnimations &request) {