From 418946b0e10e0b29e03aee18a3f85991ee59625f Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 26 Feb 2021 03:02:26 +0300 Subject: [PATCH] Add update 'chat_member'. --- telegram-bot-api/Client.cpp | 37 +++++++++++++++++++++++++++++++++++++ telegram-bot-api/Client.h | 8 ++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 9526796..bb72561 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -2266,6 +2266,29 @@ class Client::JsonChatMembers : public Jsonable { const Client *client_; }; +class Client::JsonChatMemberUpdated : public Jsonable { + public: + JsonChatMemberUpdated(const td_api::updateChatMember *update, const Client *client) + : update_(update), client_(client) { + } + void store(JsonValueScope *scope) const { + auto object = scope->enter_object(); + object("chat", JsonChat(update_->chat_id_, false, client_)); + object("actor", JsonUser(update_->actor_user_id_, client_)); + object("date", update_->date_); + auto chat_type = client_->get_chat_type(update_->chat_id_); + object("old_chat_member", JsonChatMember(update_->old_chat_member_.get(), chat_type, client_)); + object("new_chat_member", JsonChatMember(update_->new_chat_member_.get(), chat_type, client_)); + if (update_->invite_link_ != nullptr) { + object("invite_link", JsonChatInviteLink(update_->invite_link_.get(), client_)); + } + } + + private: + const td_api::updateChatMember *update_; + const Client *client_; +}; + class Client::JsonGameHighScore : public Jsonable { public: JsonGameHighScore(const td_api::gameHighScore *score, const Client *client) : score_(score), client_(client) { @@ -4313,6 +4336,9 @@ void Client::on_update(object_ptr result) { case td_api::updateNewCustomQuery::ID: add_new_custom_query(move_object_as(result)); break; + case td_api::updateChatMember::ID: + add_update_chat_member(move_object_as(result)); + break; default: // we are not interested in this updates break; @@ -8227,6 +8253,8 @@ Client::Slice Client::get_update_type_name(UpdateType update_type) { return Slice("poll"); case UpdateType::PollAnswer: return Slice("poll_answer"); + case UpdateType::ChatMember: + return Slice("chat_member"); default: UNREACHABLE(); return Slice(); @@ -8498,6 +8526,15 @@ void Client::add_new_custom_query(object_ptr &&que add_update(UpdateType::CustomQuery, JsonCustomJson(query->data_), timeout, 0); } +void Client::add_update_chat_member(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(5) << 33); + add_update(UpdateType::ChatMember, JsonChatMemberUpdated(update.get(), this), left_time, webhook_queue_id); + } +} + td::int32 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 7025b42..af028fb 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -136,6 +136,7 @@ class Client : public WebhookActor::Callback { class JsonChatPhotos; class JsonChatMember; class JsonChatMembers; + class JsonChatMemberUpdated; class JsonGameHighScore; class JsonAddress; class JsonOrderInfo; @@ -750,6 +751,8 @@ class Client : public WebhookActor::Callback { void add_new_custom_query(object_ptr &&query); + void add_update_chat_member(object_ptr &&update); + // append only before Size enum class UpdateType : int32 { Message, @@ -765,6 +768,7 @@ class Client : public WebhookActor::Callback { PreCheckoutQuery, Poll, PollAnswer, + ChatMember, Size }; @@ -792,8 +796,8 @@ class Client : public WebhookActor::Callback { bool have_message_access(int64 chat_id) const; - // by default all 13 update types up to PollAnswer are allowed - static constexpr td::uint32 DEFAULT_ALLOWED_UPDATE_TYPES = ((1 << 13) - 1); + // by default all 14 update types up to ChatMember are allowed + static constexpr td::uint32 DEFAULT_ALLOWED_UPDATE_TYPES = ((1 << 14) - 1); object_ptr authorization_state_; bool was_authorized_ = false;