From 50bb07bc465c9016662ac5d84d992958bba3fb4c Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 20 Dec 2023 17:45:26 +0300 Subject: [PATCH] Add "message_reaction_count" update. --- telegram-bot-api/Client.cpp | 51 +++++++++++++++++++++++++++++++++++++ telegram-bot-api/Client.h | 14 +++++++--- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 1a3a82e..e3c49e6 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -680,6 +680,20 @@ class Client::JsonReactionType final : public td::Jsonable { const td_api::ReactionType *reaction_type_; }; +class Client::JsonReactionCount final : public td::Jsonable { + public: + explicit JsonReactionCount(const td_api::messageReaction *message_reaction) : message_reaction_(message_reaction) { + } + void store(td::JsonValueScope *scope) const { + auto object = scope->enter_object(); + object("type", JsonReactionType(message_reaction_->type_.get())); + object("total_count", message_reaction_->total_count_); + } + + private: + const td_api::messageReaction *message_reaction_; +}; + class Client::JsonChatPermissions final : public td::Jsonable { public: explicit JsonChatPermissions(const td_api::chatPermissions *chat_permissions) : chat_permissions_(chat_permissions) { @@ -3606,6 +3620,25 @@ class Client::JsonMessageReactionUpdated final : public td::Jsonable { const Client *client_; }; +class Client::JsonMessageReactionCountUpdated final : public td::Jsonable { + public: + JsonMessageReactionCountUpdated(const td_api::updateMessageReactions *update, const Client *client) + : update_(update), client_(client) { + } + void store(td::JsonValueScope *scope) const { + auto object = scope->enter_object(); + object("chat", JsonChat(update_->chat_id_, client_)); + object("message_id", as_client_message_id(update_->message_id_)); + object("date", update_->date_); + object("reactions", + td::json_array(update_->reactions_, [](const auto &reaction) { return JsonReactionCount(reaction.get()); })); + } + + private: + const td_api::updateMessageReactions *update_; + const Client *client_; +}; + class Client::JsonUpdateTypes final : public td::Jsonable { public: explicit JsonUpdateTypes(td::uint32 update_types) : update_types_(update_types) { @@ -6875,6 +6908,9 @@ void Client::on_update(object_ptr result) { case td_api::updateMessageReaction::ID: add_update_message_reaction(move_object_as(result)); break; + case td_api::updateMessageReactions::ID: + add_update_message_reaction_count(move_object_as(result)); + break; case td_api::updateConnectionState::ID: { auto update = move_object_as(result); if (update->state_->get_id() == td_api::connectionStateReady::ID) { @@ -13114,6 +13150,8 @@ td::Slice Client::get_update_type_name(UpdateType update_type) { return td::Slice("removed_chat_boost"); case UpdateType::MessageReaction: return td::Slice("message_reaction"); + case UpdateType::MessageReactionCount: + return td::Slice("message_reaction_count"); default: UNREACHABLE(); return td::Slice(); @@ -13451,6 +13489,19 @@ void Client::add_update_message_reaction(object_ptr &&update) { + CHECK(update != nullptr); + auto left_time = update->date_ + 86400 - get_unix_time(); + if (left_time > 0) { + auto webhook_queue_id = update->chat_id_ + (static_cast(9) << 33); + add_update(UpdateType::MessageReactionCount, JsonMessageReactionCountUpdated(update.get(), this), left_time, + webhook_queue_id); + } else { + LOG(DEBUG) << "Skip updateMessageReactions with date " << update->date_ << ", because current date is " + << get_unix_time(); + } +} + td::int64 Client::choose_added_member_id(const td_api::messageChatAddMembers *message_add_members) const { CHECK(message_add_members != nullptr); for (auto &member_user_id : message_add_members->member_user_ids_) { diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index ab1f33e..6bcfa76 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -105,6 +105,7 @@ class Client final : public WebhookActor::Callback { class JsonUser; class JsonUsers; class JsonReactionType; + class JsonReactionCount; class JsonChatPermissions; class JsonChatPhotoInfo; class JsonChatLocation; @@ -174,6 +175,7 @@ class Client final : public WebhookActor::Callback { class JsonForumTopicInfo; class JsonGameHighScore; class JsonMessageReactionUpdated; + class JsonMessageReactionCountUpdated; class JsonAddress; class JsonOrderInfo; class JsonSuccessfulPaymentBot; @@ -1129,6 +1131,8 @@ class Client final : public WebhookActor::Callback { void add_update_message_reaction(object_ptr &&update); + void add_update_message_reaction_count(object_ptr &&update); + // append only before Size enum class UpdateType : int32 { Message, @@ -1150,6 +1154,7 @@ class Client final : public WebhookActor::Callback { ChatBoostUpdated, ChatBoostRemoved, MessageReaction, + MessageReactionCount, Size }; @@ -1179,10 +1184,11 @@ class Client final : public WebhookActor::Callback { bool have_message_access(int64 chat_id) const; - // by default ChatMember and MessageReaction updates are disabled - static constexpr td::uint32 DEFAULT_ALLOWED_UPDATE_TYPES = (1 << static_cast(UpdateType::Size)) - 1 - - (1 << static_cast(UpdateType::ChatMember)) - - (1 << static_cast(UpdateType::MessageReaction)); + // by default ChatMember, MessageReaction, and MessageReactionCount updates are disabled + static constexpr td::uint32 DEFAULT_ALLOWED_UPDATE_TYPES = + (1 << static_cast(UpdateType::Size)) - 1 - (1 << static_cast(UpdateType::ChatMember)) - + (1 << static_cast(UpdateType::MessageReaction)) - + (1 << static_cast(UpdateType::MessageReactionCount)); object_ptr authorization_state_; bool was_authorized_ = false;