Save and load message effects.

This commit is contained in:
levlam 2024-05-09 17:37:07 +03:00
parent 5d349d86e1
commit 1b200bb820
6 changed files with 155 additions and 43 deletions

View File

@ -1409,9 +1409,6 @@ messageEffectTypePremiumSticker sticker:sticker = MessageEffectType;
//@type Type of the effect //@type Type of the effect
messageEffect id:int64 static_icon:sticker emoji:string is_premium:Bool type:MessageEffectType = MessageEffect; messageEffect id:int64 static_icon:sticker emoji:string is_premium:Bool type:MessageEffectType = MessageEffect;
//@description Contains a list of message effects @effects List of available message effects. Emoji reaction effects are guaranteed to be returned before sticker effects
messageEffects effects:vector<messageEffect> = MessageEffects;
//@class MessageSendingState @description Contains information about the sending state of the message //@class MessageSendingState @description Contains information about the sending state of the message
@ -8406,9 +8403,6 @@ getSavedMessagesTags saved_messages_topic_id:int53 = SavedMessagesTags;
//@description Changes label of a Saved Messages tag; for Telegram Premium users only @tag The tag which label will be changed @label New label for the tag; 0-12 characters //@description Changes label of a Saved Messages tag; for Telegram Premium users only @tag The tag which label will be changed @label New label for the tag; 0-12 characters
setSavedMessagesTagLabel tag:ReactionType label:string = Ok; setSavedMessagesTagLabel tag:ReactionType label:string = Ok;
//@description Returns effects that can be added to a message sent in a private chat
getMessageEffects = MessageEffects;
//@description Returns information about a message effect. Returns a 404 error if the effect is not found @effect_id Unique identifier of the effect //@description Returns information about a message effect. Returns a 404 error if the effect is not found @effect_id Unique identifier of the effect
getMessageEffect effect_id:int64 = MessageEffect; getMessageEffect effect_id:int64 = MessageEffect;

View File

@ -255,8 +255,8 @@ class GetMessageAvailableEffectsQuery final : public Td::ResultHandler {
: promise_(std::move(promise)) { : promise_(std::move(promise)) {
} }
void send() { void send(int32 hash) {
send_query(G()->net_query_creator().create(telegram_api::messages_getAvailableEffects(0))); send_query(G()->net_query_creator().create(telegram_api::messages_getAvailableEffects(hash)));
} }
void on_result(BufferSlice packet) final { void on_result(BufferSlice packet) final {
@ -777,7 +777,8 @@ void ReactionManager::load_reactions() {
return reload_reactions(); return reload_reactions();
} }
auto new_reactions = reactions_; Reactions new_reactions;
new_reactions.are_being_reloaded_ = reactions_.are_being_reloaded_;
auto status = log_event_parse(new_reactions, reactions); auto status = log_event_parse(new_reactions, reactions);
if (status.is_error()) { if (status.is_error()) {
LOG(ERROR) << "Can't load available reactions: " << status; LOG(ERROR) << "Can't load available reactions: " << status;
@ -1223,35 +1224,80 @@ td_api::object_ptr<td_api::messageEffect> ReactionManager::get_message_effect_ob
return nullptr; return nullptr;
} }
td_api::object_ptr<td_api::messageEffects> ReactionManager::get_message_effects_object() const {
auto effects =
transform(message_effects_.effects_, [&](const Effect &effect) { return get_message_effect_object(effect); });
return td_api::make_object<td_api::messageEffects>(std::move(effects));
}
td_api::object_ptr<td_api::updateAvailableMessageEffects> ReactionManager::get_update_available_message_effects_object() td_api::object_ptr<td_api::updateAvailableMessageEffects> ReactionManager::get_update_available_message_effects_object()
const { const {
return td_api::make_object<td_api::updateAvailableMessageEffects>(vector<int64>(active_message_effects_)); return td_api::make_object<td_api::updateAvailableMessageEffects>(vector<int64>(active_message_effects_));
} }
void ReactionManager::reload_message_effects() { void ReactionManager::reload_message_effects() {
if (G()->close_flag() || message_effects_.are_being_reloaded_) {
return;
}
CHECK(!td_->auth_manager_->is_bot());
message_effects_.are_being_reloaded_ = true;
load_message_effects(); // must be after are_being_reloaded_ is set to true to avoid recursion
auto promise = PromiseCreator::lambda(
[actor_id = actor_id(this)](
Result<telegram_api::object_ptr<telegram_api::messages_AvailableEffects>> r_effects) mutable {
send_closure(actor_id, &ReactionManager::on_get_message_effects, std::move(r_effects));
});
td_->create_handler<GetMessageAvailableEffectsQuery>(std::move(promise))->send(message_effects_.hash_);
} }
void ReactionManager::get_message_effects(Promise<td_api::object_ptr<td_api::messageEffects>> &&promise) { void ReactionManager::load_message_effects() {
auto query_promise = PromiseCreator::lambda( if (are_message_effects_loaded_from_database_) {
[actor_id = actor_id(this), promise = std::move(promise)]( return;
Result<telegram_api::object_ptr<telegram_api::messages_AvailableEffects>> r_effects) mutable { }
send_closure(actor_id, &ReactionManager::on_get_message_effects, std::move(r_effects), std::move(promise)); are_message_effects_loaded_from_database_ = true;
});
td_->create_handler<GetMessageAvailableEffectsQuery>(std::move(query_promise))->send(); LOG(INFO) << "Loading message effects";
string message_effects = G()->td_db()->get_binlog_pmc()->get("message_effects");
if (message_effects.empty()) {
return reload_message_effects();
}
Effects new_message_effects;
new_message_effects.are_being_reloaded_ = message_effects_.are_being_reloaded_;
auto status = log_event_parse(new_message_effects, message_effects);
if (status.is_error()) {
LOG(ERROR) << "Can't load message effects: " << status;
return reload_message_effects();
}
for (auto &effect : new_message_effects.effects_) {
if (!effect.is_valid()) {
LOG(ERROR) << "Loaded invalid message effect";
return reload_message_effects();
}
}
message_effects_ = std::move(new_message_effects);
LOG(INFO) << "Successfully loaded " << message_effects_.effects_.size() << " message effects";
update_active_message_effects();
}
void ReactionManager::save_message_effects() {
LOG(INFO) << "Save " << message_effects_.effects_.size() << " message effects";
are_message_effects_loaded_from_database_ = true;
G()->td_db()->get_binlog_pmc()->set("message_effects", log_event_store(message_effects_).as_slice().str());
} }
void ReactionManager::on_get_message_effects( void ReactionManager::on_get_message_effects(
Result<telegram_api::object_ptr<telegram_api::messages_AvailableEffects>> r_effects, Result<telegram_api::object_ptr<telegram_api::messages_AvailableEffects>> r_effects) {
Promise<td_api::object_ptr<td_api::messageEffects>> promise) { CHECK(message_effects_.are_being_reloaded_);
message_effects_.are_being_reloaded_ = false;
auto get_message_effect_queries = std::move(pending_get_message_effect_queries_);
pending_get_message_effect_queries_.clear();
SCOPE_EXIT {
for (auto &query : get_message_effect_queries) {
query.second.set_value(get_message_effect_object(query.first));
}
};
G()->ignore_result_if_closing(r_effects); G()->ignore_result_if_closing(r_effects);
if (r_effects.is_error()) { if (r_effects.is_error()) {
return promise.set_error(r_effects.move_as_error()); return;
} }
auto message_effects = r_effects.move_as_ok(); auto message_effects = r_effects.move_as_ok();
@ -1334,13 +1380,14 @@ void ReactionManager::on_get_message_effects(
message_effects_.effects_ = std::move(new_effects); message_effects_.effects_ = std::move(new_effects);
message_effects_.hash_ = effects->hash_; message_effects_.hash_ = effects->hash_;
save_message_effects();
update_active_message_effects(); update_active_message_effects();
break; break;
} }
default: default:
UNREACHABLE(); UNREACHABLE();
} }
promise.set_value(get_message_effects_object());
} }
void ReactionManager::save_active_message_effects() { void ReactionManager::save_active_message_effects() {
@ -1385,13 +1432,11 @@ void ReactionManager::update_active_message_effects() {
void ReactionManager::get_message_effect(int64 effect_id, void ReactionManager::get_message_effect(int64 effect_id,
Promise<td_api::object_ptr<td_api::messageEffect>> &&promise) { Promise<td_api::object_ptr<td_api::messageEffect>> &&promise) {
/*
load_message_effects(); load_message_effects();
if (effects_.effects_.empty() && effects_.are_being_reloaded_) { if (message_effects_.effects_.empty() && message_effects_.are_being_reloaded_) {
pending_get_message_effect_queries_.emplace_back(effect_id, std::move(promise)); pending_get_message_effect_queries_.emplace_back(effect_id, std::move(promise));
return; return;
} }
*/
promise.set_value(get_message_effect_object(effect_id)); promise.set_value(get_message_effect_object(effect_id));
} }

View File

@ -83,8 +83,6 @@ class ReactionManager final : public Actor {
void reload_message_effects(); void reload_message_effects();
void get_message_effects(Promise<td_api::object_ptr<td_api::messageEffects>> &&promise);
void get_message_effect(int64 effect_id, Promise<td_api::object_ptr<td_api::messageEffect>> &&promise); void get_message_effect(int64 effect_id, Promise<td_api::object_ptr<td_api::messageEffect>> &&promise);
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const; void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
@ -210,11 +208,24 @@ class ReactionManager final : public Actor {
bool is_sticker() const { bool is_sticker() const {
return effect_animation_id_ == FileId(); return effect_animation_id_ == FileId();
} }
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
}; };
struct Effects { struct Effects {
int32 hash_ = 0; int32 hash_ = 0;
bool are_being_reloaded_ = false;
vector<Effect> effects_; vector<Effect> effects_;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
}; };
td_api::object_ptr<td_api::emojiReaction> get_emoji_reaction_object(const string &emoji) const; td_api::object_ptr<td_api::emojiReaction> get_emoji_reaction_object(const string &emoji) const;
@ -265,12 +276,13 @@ class ReactionManager final : public Actor {
td_api::object_ptr<td_api::messageEffect> get_message_effect_object(int64 effect_id) const; td_api::object_ptr<td_api::messageEffect> get_message_effect_object(int64 effect_id) const;
td_api::object_ptr<td_api::messageEffects> get_message_effects_object() const;
td_api::object_ptr<td_api::updateAvailableMessageEffects> get_update_available_message_effects_object() const; td_api::object_ptr<td_api::updateAvailableMessageEffects> get_update_available_message_effects_object() const;
void on_get_message_effects(Result<telegram_api::object_ptr<telegram_api::messages_AvailableEffects>> r_effects, void load_message_effects();
Promise<td_api::object_ptr<td_api::messageEffects>> promise);
void save_message_effects();
void on_get_message_effects(Result<telegram_api::object_ptr<telegram_api::messages_AvailableEffects>> r_effects);
void save_active_message_effects(); void save_active_message_effects();
@ -284,6 +296,7 @@ class ReactionManager final : public Actor {
bool is_inited_ = false; bool is_inited_ = false;
bool are_reactions_loaded_from_database_ = false; bool are_reactions_loaded_from_database_ = false;
bool are_all_tags_loaded_from_database_ = false; bool are_all_tags_loaded_from_database_ = false;
bool are_message_effects_loaded_from_database_ = false;
vector<std::pair<string, Promise<td_api::object_ptr<td_api::emojiReaction>>>> pending_get_emoji_reaction_queries_; vector<std::pair<string, Promise<td_api::object_ptr<td_api::emojiReaction>>>> pending_get_emoji_reaction_queries_;
@ -302,6 +315,8 @@ class ReactionManager final : public Actor {
Effects message_effects_; Effects message_effects_;
vector<int64> active_message_effects_; vector<int64> active_message_effects_;
vector<std::pair<int64, Promise<td_api::object_ptr<td_api::messageEffect>>>> pending_get_message_effect_queries_;
}; };
} // namespace td } // namespace td

View File

@ -119,4 +119,70 @@ void ReactionManager::ReactionList::parse(ParserT &parser) {
} }
} }
template <class StorerT>
void ReactionManager::Effect::store(StorerT &storer) const {
StickersManager *stickers_manager = storer.context()->td().get_actor_unsafe()->stickers_manager_.get();
bool has_static_icon = static_icon_id_.is_valid();
bool has_effect_animation = effect_animation_id_.is_valid();
BEGIN_STORE_FLAGS();
STORE_FLAG(is_premium_);
STORE_FLAG(has_static_icon);
STORE_FLAG(has_effect_animation);
END_STORE_FLAGS();
td::store(id_, storer);
td::store(emoji_, storer);
if (has_static_icon) {
stickers_manager->store_sticker(static_icon_id_, false, storer, "Effect");
}
stickers_manager->store_sticker(effect_sticker_id_, false, storer, "Effect");
if (has_effect_animation) {
stickers_manager->store_sticker(effect_animation_id_, false, storer, "Effect");
}
}
template <class ParserT>
void ReactionManager::Effect::parse(ParserT &parser) {
StickersManager *stickers_manager = parser.context()->td().get_actor_unsafe()->stickers_manager_.get();
bool has_static_icon;
bool has_effect_animation;
BEGIN_PARSE_FLAGS();
PARSE_FLAG(is_premium_);
PARSE_FLAG(has_static_icon);
PARSE_FLAG(has_effect_animation);
END_PARSE_FLAGS();
td::parse(id_, parser);
td::parse(emoji_, parser);
if (has_static_icon) {
static_icon_id_ = stickers_manager->parse_sticker(false, parser);
}
effect_sticker_id_ = stickers_manager->parse_sticker(false, parser);
if (has_effect_animation) {
effect_animation_id_ = stickers_manager->parse_sticker(false, parser);
}
}
template <class StorerT>
void ReactionManager::Effects::store(StorerT &storer) const {
bool has_effects = !effects_.empty();
BEGIN_STORE_FLAGS();
STORE_FLAG(has_effects);
END_STORE_FLAGS();
if (has_effects) {
td::store(effects_, storer);
td::store(hash_, storer);
}
}
template <class ParserT>
void ReactionManager::Effects::parse(ParserT &parser) {
bool has_effects;
BEGIN_PARSE_FLAGS();
PARSE_FLAG(has_effects);
END_PARSE_FLAGS();
if (has_effects) {
td::parse(effects_, parser);
td::parse(hash_, parser);
}
}
} // namespace td } // namespace td

View File

@ -5502,12 +5502,6 @@ void Td::on_request(uint64 id, td_api::setSavedMessagesTagLabel &request) {
std::move(promise)); std::move(promise));
} }
void Td::on_request(uint64 id, const td_api::getMessageEffects &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
reaction_manager_->get_message_effects(std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getMessageEffect &request) { void Td::on_request(uint64 id, const td_api::getMessageEffect &request) {
CHECK_IS_USER(); CHECK_IS_USER();
CREATE_REQUEST_PROMISE(); CREATE_REQUEST_PROMISE();

View File

@ -831,8 +831,6 @@ class Td final : public Actor {
void on_request(uint64 id, td_api::setSavedMessagesTagLabel &request); void on_request(uint64 id, td_api::setSavedMessagesTagLabel &request);
void on_request(uint64 id, const td_api::getMessageEffects &request);
void on_request(uint64 id, const td_api::getMessageEffect &request); void on_request(uint64 id, const td_api::getMessageEffect &request);
void on_request(uint64 id, td_api::getMessagePublicForwards &request); void on_request(uint64 id, td_api::getMessagePublicForwards &request);