Add updateReaction.

This commit is contained in:
levlam 2022-01-19 14:14:21 +03:00
parent 4e172fc5c8
commit 99586edf26
3 changed files with 146 additions and 0 deletions

View File

@ -2365,6 +2365,20 @@ phoneNumberAuthenticationSettings allow_flash_call:Bool allow_missed_call:Bool i
animations animations:vector<animation> = Animations; animations animations:vector<animation> = Animations;
//@description Contains stickers which must be used for reaction animation rendering
//@reaction Text representation of the reaction
//@title Reaction title
//@is_active True, if the reaction can be added to new messages and enabled in chats
//@static_icon Static icon for the reaction
//@appear_animation Appear animation for the reaction
//@select_animation Select animation for the reaction
//@activate_animation Activate animation for the reaction
//@effect_animation Effect animation for the reaction
//@around_animation Around animation for the reaction; may be null
//@center_animation Center animation for the reaction; may be null
reaction reaction:string title:string is_active:Bool static_icon:sticker appear_animation:sticker select_animation:sticker activate_animation:sticker effect_animation:sticker around_animation:sticker center_animation:sticker = Reaction;
//@class DiceStickers @description Contains animated stickers which must be used for dice animation rendering //@class DiceStickers @description Contains animated stickers which must be used for dice animation rendering
//@description A regular animated sticker @sticker The animated sticker with the dice animation //@description A regular animated sticker @sticker The animated sticker with the dice animation
@ -3932,6 +3946,9 @@ updateTermsOfService terms_of_service_id:string terms_of_service:termsOfService
//@description The list of users nearby has changed. The update is guaranteed to be sent only 60 seconds after a successful searchChatsNearby request @users_nearby The new list of users nearby //@description The list of users nearby has changed. The update is guaranteed to be sent only 60 seconds after a successful searchChatsNearby request @users_nearby The new list of users nearby
updateUsersNearby users_nearby:vector<chatNearby> = Update; updateUsersNearby users_nearby:vector<chatNearby> = Update;
//@description The list of supported reactions has changed @reactions The new list of supported reactions
updateReactions reactions:vector<reaction> = Update;
//@description The list of supported dice emojis has changed @emojis The new list of supported dice emojis //@description The list of supported dice emojis has changed @emojis The new list of supported dice emojis
updateDiceEmojis emojis:vector<string> = Update; updateDiceEmojis emojis:vector<string> = Update;

View File

@ -69,6 +69,29 @@
namespace td { namespace td {
class GetAvailableReactionsQuery final : public Td::ResultHandler {
public:
void send(int32 hash) {
send_query(G()->net_query_creator().create(telegram_api::messages_getAvailableReactions(hash)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_getAvailableReactions>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for GetAvailableReactionsQuery: " << to_string(ptr);
td_->stickers_manager_->on_get_available_reactions(std::move(ptr));
}
void on_error(Status status) final {
LOG(INFO) << "Receive error for GetAvailableReactionsQuery: " << status;
td_->stickers_manager_->on_get_available_reactions(nullptr);
}
};
class GetAllStickersQuery final : public Td::ResultHandler { class GetAllStickersQuery final : public Td::ResultHandler {
bool is_masks_; bool is_masks_;
@ -1313,6 +1336,15 @@ void StickersManager::init() {
G()->shared_config().set_option_empty("animated_emoji_sticker_set_name"); // legacy G()->shared_config().set_option_empty("animated_emoji_sticker_set_name"); // legacy
} }
void StickersManager::reload_reactions() {
CHECK(!td_->auth_manager_->is_bot());
if (reactions_.are_being_reloaded_ || G()->close_flag()) {
return;
}
reactions_.are_being_reloaded_ = true;
td_->create_handler<GetAvailableReactionsQuery>()->send(reactions_.hash_);
}
StickersManager::SpecialStickerSet &StickersManager::add_special_sticker_set(const SpecialStickerSetType &type) { StickersManager::SpecialStickerSet &StickersManager::add_special_sticker_set(const SpecialStickerSetType &type) {
auto &result = special_sticker_sets_[type]; auto &result = special_sticker_sets_[type];
if (result.type_.is_empty()) { if (result.type_.is_empty()) {
@ -2159,6 +2191,9 @@ bool StickersManager::has_webp_thumbnail(const vector<tl_object_ptr<telegram_api
std::pair<int64, FileId> StickersManager::on_get_sticker_document(tl_object_ptr<telegram_api::Document> &&document_ptr, std::pair<int64, FileId> StickersManager::on_get_sticker_document(tl_object_ptr<telegram_api::Document> &&document_ptr,
StickerFormat expected_format) { StickerFormat expected_format) {
if (document_ptr == nullptr) {
return {};
}
int32 document_constructor_id = document_ptr->get_id(); int32 document_constructor_id = document_ptr->get_id();
if (document_constructor_id == telegram_api::documentEmpty::ID) { if (document_constructor_id == telegram_api::documentEmpty::ID) {
LOG(ERROR) << "Empty sticker document received"; LOG(ERROR) << "Empty sticker document received";
@ -3103,6 +3138,68 @@ void StickersManager::on_get_special_sticker_set(const SpecialStickerSetType &ty
on_load_special_sticker_set(type, Status::OK()); on_load_special_sticker_set(type, Status::OK());
} }
td_api::object_ptr<td_api::updateReactions> StickersManager::get_update_reactions_object() const {
auto reactions = transform(reactions_.reactions_, [this](const Reaction &reaction) {
return td_api::make_object<td_api::reaction>(
reaction.reaction_, reaction.title_, reaction.is_active_, get_sticker_object(reaction.static_icon_),
get_sticker_object(reaction.appear_animation_), get_sticker_object(reaction.select_animation_),
get_sticker_object(reaction.activate_animation_), get_sticker_object(reaction.effect_animation_),
get_sticker_object(reaction.around_animation_), get_sticker_object(reaction.center_animation_));
});
return td_api::make_object<td_api::updateReactions>(std::move(reactions));
}
void StickersManager::on_get_available_reactions(
tl_object_ptr<telegram_api::messages_AvailableReactions> &&available_reactions_ptr) {
CHECK(reactions_.are_being_reloaded_);
reactions_.are_being_reloaded_ = false;
if (available_reactions_ptr == nullptr) {
// failed to get available reactions
return;
}
int32 constructor_id = available_reactions_ptr->get_id();
if (constructor_id == telegram_api::messages_availableReactionsNotModified::ID) {
LOG(INFO) << "Available reactions are not modified";
return;
}
CHECK(constructor_id == telegram_api::messages_availableReactions::ID);
auto available_reactions = move_tl_object_as<telegram_api::messages_availableReactions>(available_reactions_ptr);
vector<Reaction> new_reactions;
for (auto &available_reaction : available_reactions->reactions_) {
Reaction reaction;
reaction.is_active_ = !available_reaction->inactive_;
reaction.reaction_ = std::move(available_reaction->reaction_);
reaction.title_ = std::move(available_reaction->title_);
reaction.static_icon_ =
on_get_sticker_document(std::move(available_reaction->static_icon_), StickerFormat::Webp).second;
reaction.appear_animation_ =
on_get_sticker_document(std::move(available_reaction->appear_animation_), StickerFormat::Tgs).second;
reaction.select_animation_ =
on_get_sticker_document(std::move(available_reaction->select_animation_), StickerFormat::Tgs).second;
reaction.activate_animation_ =
on_get_sticker_document(std::move(available_reaction->activate_animation_), StickerFormat::Tgs).second;
reaction.effect_animation_ =
on_get_sticker_document(std::move(available_reaction->effect_animation_), StickerFormat::Tgs).second;
reaction.around_animation_ =
on_get_sticker_document(std::move(available_reaction->around_animation_), StickerFormat::Tgs).second;
reaction.center_animation_ =
on_get_sticker_document(std::move(available_reaction->center_icon_), StickerFormat::Tgs).second;
if (!reaction.static_icon_.is_valid() || !reaction.appear_animation_.is_valid() ||
!reaction.select_animation_.is_valid() || !reaction.activate_animation_.is_valid() ||
!reaction.effect_animation_.is_valid()) {
LOG(ERROR) << "Receive invalid reaction " << reaction.reaction_;
continue;
}
new_reactions.push_back(std::move(reaction));
}
reactions_.reactions_ = std::move(new_reactions);
reactions_.hash_ = available_reactions->hash_;
send_closure(G()->td(), &Td::send_update, get_update_reactions_object());
}
void StickersManager::on_get_installed_sticker_sets(bool is_masks, void StickersManager::on_get_installed_sticker_sets(bool is_masks,
tl_object_ptr<telegram_api::messages_AllStickers> &&stickers_ptr) { tl_object_ptr<telegram_api::messages_AllStickers> &&stickers_ptr) {
next_installed_sticker_sets_load_time_[is_masks] = Time::now_cached() + Random::fast(30 * 60, 50 * 60); next_installed_sticker_sets_load_time_[is_masks] = Time::now_cached() + Random::fast(30 * 60, 50 * 60);
@ -7436,6 +7533,8 @@ void StickersManager::after_get_difference() {
return; return;
} }
if (td_->is_online()) { if (td_->is_online()) {
reload_reactions();
get_installed_sticker_sets(false, Auto()); get_installed_sticker_sets(false, Auto());
get_installed_sticker_sets(true, Auto()); get_installed_sticker_sets(true, Auto());
get_featured_sticker_sets(0, 1000, Auto()); get_featured_sticker_sets(0, 1000, Auto());
@ -7455,6 +7554,9 @@ void StickersManager::get_current_state(vector<td_api::object_ptr<td_api::Update
return; return;
} }
if (!reactions_.reactions_.empty()) {
updates.push_back(get_update_reactions_object());
}
for (int is_masks = 0; is_masks < 2; is_masks++) { for (int is_masks = 0; is_masks < 2; is_masks++) {
if (are_installed_sticker_sets_loaded_[is_masks]) { if (are_installed_sticker_sets_loaded_[is_masks]) {
updates.push_back(get_update_installed_sticker_sets_object(is_masks)); updates.push_back(get_update_installed_sticker_sets_object(is_masks));

View File

@ -131,6 +131,8 @@ class StickersManager final : public Actor {
void view_featured_sticker_sets(const vector<StickerSetId> &sticker_set_ids); void view_featured_sticker_sets(const vector<StickerSetId> &sticker_set_ids);
void on_get_available_reactions(tl_object_ptr<telegram_api::messages_AvailableReactions> &&available_reactions_ptr);
void on_get_installed_sticker_sets(bool is_masks, tl_object_ptr<telegram_api::messages_AllStickers> &&stickers_ptr); void on_get_installed_sticker_sets(bool is_masks, tl_object_ptr<telegram_api::messages_AllStickers> &&stickers_ptr);
void on_get_installed_sticker_sets_failed(bool is_masks, Status error); void on_get_installed_sticker_sets_failed(bool is_masks, Status error);
@ -438,6 +440,25 @@ class StickersManager final : public Actor {
bool is_being_reloaded_ = false; bool is_being_reloaded_ = false;
}; };
struct Reaction {
string reaction_;
string title_;
bool is_active_ = false;
FileId static_icon_;
FileId appear_animation_;
FileId select_animation_;
FileId activate_animation_;
FileId effect_animation_;
FileId around_animation_;
FileId center_animation_;
};
struct Reactions {
int32 hash_ = 0;
bool are_being_reloaded_ = false;
vector<Reaction> reactions_;
};
class StickerListLogEvent; class StickerListLogEvent;
class StickerSetListLogEvent; class StickerSetListLogEvent;
@ -657,6 +678,10 @@ class StickersManager final : public Actor {
void tear_down() final; void tear_down() final;
void reload_reactions();
td_api::object_ptr<td_api::updateReactions> get_update_reactions_object() const;
SpecialStickerSet &add_special_sticker_set(const SpecialStickerSetType &type); SpecialStickerSet &add_special_sticker_set(const SpecialStickerSetType &type);
static void init_special_sticker_set(SpecialStickerSet &sticker_set, int64 sticker_set_id, int64 access_hash, static void init_special_sticker_set(SpecialStickerSet &sticker_set, int64 sticker_set_id, int64 access_hash,
@ -826,6 +851,8 @@ class StickersManager final : public Actor {
std::unordered_map<FileId, std::pair<UserId, Promise<Unit>>, FileIdHash> being_uploaded_files_; std::unordered_map<FileId, std::pair<UserId, Promise<Unit>>, FileIdHash> being_uploaded_files_;
Reactions reactions_;
std::unordered_map<string, vector<string>> emoji_language_codes_; std::unordered_map<string, vector<string>> emoji_language_codes_;
std::unordered_map<string, int32> emoji_language_code_versions_; std::unordered_map<string, int32> emoji_language_code_versions_;
std::unordered_map<string, double> emoji_language_code_last_difference_times_; std::unordered_map<string, double> emoji_language_code_last_difference_times_;