From 121e2d8a188ea53d754c8d1b6eaeb64bc9bee012 Mon Sep 17 00:00:00 2001 From: David Guillen Fandos Date: Sat, 3 Feb 2024 11:23:59 +0100 Subject: [PATCH] Rebase: Add "chat_boost" and "removed_chat_boost" updates. --- telegram-bot-api/Client.cpp | 122 ++++++++++++++++++++++++++++++++++++ telegram-bot-api/Client.h | 10 +++ 2 files changed, 132 insertions(+) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 2c699d4..3b2ea05 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -3066,6 +3066,97 @@ class Client::JsonChatJoinRequest final : public td::Jsonable { const Client *client_; }; +class Client::JsonChatBoostSource final : public td::Jsonable { + public: + JsonChatBoostSource(const td_api::ChatBoostSource *boost_source, const Client *client) + : boost_source_(boost_source), client_(client) { + } + void store(td::JsonValueScope *scope) const { + auto object = scope->enter_object(); + CHECK(boost_source_ != nullptr); + switch (boost_source_->get_id()) { + case td_api::chatBoostSourcePremium::ID: { + const auto *source = static_cast(boost_source_); + object("source", "premium"); + object("user", JsonUser(source->user_id_, client_)); + break; + } + case td_api::chatBoostSourceGiftCode::ID: { + const auto *source = static_cast(boost_source_); + object("source", "gift_code"); + object("user", JsonUser(source->user_id_, client_)); + break; + } + case td_api::chatBoostSourceGiveaway::ID: { + const auto *source = static_cast(boost_source_); + object("source", "giveaway"); + object("giveaway_message_id", as_client_message_id_unchecked(source->giveaway_message_id_)); + if (source->user_id_ != 0) { + object("user", JsonUser(source->user_id_, client_)); + } else if (source->is_unclaimed_) { + object("is_unclaimed", td::JsonTrue()); + } + break; + } + default: + UNREACHABLE(); + } + } + + private: + const td_api::ChatBoostSource *boost_source_; + const Client *client_; +}; + +class Client::JsonChatBoost final : public td::Jsonable { + public: + JsonChatBoost(const td_api::chatBoost *boost, const Client *client) : boost_(boost), client_(client) { + } + void store(td::JsonValueScope *scope) const { + auto object = scope->enter_object(); + object("boost_id", boost_->id_); + object("add_date", boost_->start_date_); + object("expiration_date", boost_->expiration_date_); + object("source", JsonChatBoostSource(boost_->source_.get(), client_)); + } + + private: + const td_api::chatBoost *boost_; + const Client *client_; +}; + +class Client::JsonChatBoostUpdated final : public td::Jsonable { + public: + JsonChatBoostUpdated(const td_api::updateChatBoost *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_, false, client_)); + object("boost", JsonChatBoost(update_->boost_.get(), client_)); + } + + private: + const td_api::updateChatBoost *update_; + const Client *client_; +}; + +class Client::JsonChatBoostRemoved final : public td::Jsonable { + public: + JsonChatBoostRemoved(const td_api::updateChatBoost *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_, false, client_)); + object("boost_id", update_->boost_->id_); + object("remove_date", update_->boost_->start_date_); + object("source", JsonChatBoostSource(update_->boost_->source_.get(), client_)); + } + + private: + const td_api::updateChatBoost *update_; + const Client *client_; +}; + class Client::JsonGameHighScore final : public td::Jsonable { public: JsonGameHighScore(const td_api::gameHighScore *score, const Client *client) : score_(score), client_(client) { @@ -6162,6 +6253,9 @@ void Client::on_update(object_ptr result) { case td_api::updateNewChatJoinRequest::ID: add_update_chat_join_request(move_object_as(result)); break; + case td_api::updateChatBoost::ID: + add_update_chat_boost(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) { @@ -12150,6 +12244,10 @@ td::Slice Client::get_update_type_name(UpdateType update_type) { return td::Slice("chat_member"); case UpdateType::ChatJoinRequest: return td::Slice("chat_join_request"); + case UpdateType::ChatBoostUpdated: + return td::Slice("chat_boost"); + case UpdateType::ChatBoostRemoved: + return td::Slice("removed_chat_boost"); default: UNREACHABLE(); return td::Slice(); @@ -12458,6 +12556,22 @@ void Client::add_update_chat_join_request(object_ptr &&update) { + CHECK(update != nullptr); + auto left_time = update->boost_->start_date_ + 86400 - get_unix_time(); + if (left_time > 0) { + auto webhook_queue_id = update->chat_id_ + (static_cast(7) << 33); + if (update->boost_->expiration_date_ == 0) { + add_update(UpdateType::ChatBoostRemoved, JsonChatBoostRemoved(update.get(), this), left_time, webhook_queue_id); + } else { + add_update(UpdateType::ChatBoostUpdated, JsonChatBoostUpdated(update.get(), this), left_time, webhook_queue_id); + } + } else { + LOG(DEBUG) << "Skip updateChatBoost with date " << update->boost_->start_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_) { @@ -13287,6 +13401,14 @@ td::int32 Client::as_scheduled_message_id(int64 message_id) { return -static_cast((message_id >> 3) & ((1 << 18) - 1)); } +td::int32 Client::as_client_message_id_unchecked(int64 message_id) { + auto result = static_cast(message_id >> 20); + if (as_tdlib_message_id(result) != message_id) { + return 0; + } + return result; +} + td::int64 Client::get_supergroup_chat_id(int64 supergroup_id) { return static_cast(-1000000000000ll) - supergroup_id; } diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index b01e66e..9e4ad14 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -159,6 +159,10 @@ class Client final : public WebhookActor::Callback { class JsonChatMembers; class JsonChatMemberUpdated; class JsonChatJoinRequest; + class JsonChatBoostSource; + class JsonChatBoost; + class JsonChatBoostUpdated; + class JsonChatBoostRemoved; class JsonForumTopicCreated; class JsonForumTopicEdited; class JsonForumTopicInfo; @@ -1042,6 +1046,8 @@ class Client final : public WebhookActor::Callback { static int32 as_scheduled_message_id(int64 message_id); + static int32 as_client_message_id_unchecked(int64 message_id); + static int64 get_supergroup_chat_id(int64 supergroup_id); static int64 get_basic_group_chat_id(int64 basic_group_id); @@ -1075,6 +1081,8 @@ class Client final : public WebhookActor::Callback { void add_update_chat_join_request(object_ptr &&update); + void add_update_chat_boost(object_ptr &&update); + // append only before Size enum class UpdateType : int32 { Message, @@ -1093,6 +1101,8 @@ class Client final : public WebhookActor::Callback { MyChatMember, ChatMember, ChatJoinRequest, + ChatBoostUpdated, + ChatBoostRemoved, Size };