diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index de87ce229..9043a5159 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -7968,13 +7968,18 @@ getChatBoostLink chat_id:int53 = ChatBoostLink; //@description Returns information about a link to boost a chat. Can be called for any internal link of the type internalLinkTypeChatBoost @url The link to boost a chat getChatBoostLinkInfo url:string = ChatBoostLinkInfo; -//@description Returns list of boosts applied to a chat. The user must be an administrator in the channel chat to get the list of boosts +//@description Returns list of boosts applied to a chat; requires administrator rights in the channel chat //@chat_id Identifier of the chat //@only_gift_codes Pass true to receive only boosts received from gift codes and giveaways created by the chat //@offset Offset of the first entry to return as received from the previous request; use empty string to get the first chunk of results //@limit The maximum number of boosts to be returned; up to 100. For optimal performance, the number of returned boosts can be smaller than the specified limit getChatBoosts chat_id:int53 only_gift_codes:Bool offset:string limit:int32 = FoundChatBoosts; +//@description Returns list of boosts applied to a chat by a given user; requires administrator rights in the channel chat; for bots only +//@chat_id Identifier of the chat +//@user_id Identifier of the user +getUserChatBoosts chat_id:int53 user_id:int53 = FoundChatBoosts; + //@description Returns information about a bot that can be added to attachment or side menu @bot_user_id Bot's user identifier getAttachmentMenuBot bot_user_id:int53 = AttachmentMenuBot; diff --git a/td/telegram/BoostManager.cpp b/td/telegram/BoostManager.cpp index 4b1fdf673..ace77ea64 100644 --- a/td/telegram/BoostManager.cpp +++ b/td/telegram/BoostManager.cpp @@ -286,6 +286,54 @@ class GetBoostsListQuery final : public Td::ResultHandler { } }; +class GetUserBoostsQuery final : public Td::ResultHandler { + Promise> promise_; + DialogId dialog_id_; + + public: + explicit GetUserBoostsQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(DialogId dialog_id, UserId user_id) { + dialog_id_ = dialog_id; + auto input_peer = td_->messages_manager_->get_input_peer(dialog_id_, AccessRights::Read); + CHECK(input_peer != nullptr); + auto r_input_user = td_->contacts_manager_->get_input_user(user_id); + CHECK(r_input_user.is_ok()); + send_query(G()->net_query_creator().create( + telegram_api::premium_getUserBoosts(std::move(input_peer), r_input_user.move_as_ok()))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + auto result = result_ptr.move_as_ok(); + LOG(DEBUG) << "Receive result for GetUserBoostsQuery: " << to_string(result); + td_->contacts_manager_->on_get_users(std::move(result->users_), "GetUserBoostsQuery"); + + auto total_count = result->count_; + vector> boosts; + for (auto &boost : result->boosts_) { + auto chat_boost_object = get_chat_boost_object(td_, boost); + if (chat_boost_object == nullptr || chat_boost_object->expiration_date_ <= G()->unix_time()) { + continue; + } + boosts.push_back(std::move(chat_boost_object)); + } + promise_.set_value( + td_api::make_object(total_count, std::move(boosts), result->next_offset_)); + } + + void on_error(Status status) final { + td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "GetUserBoostsQuery"); + promise_.set_error(std::move(status)); + } +}; + BoostManager::BoostManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { } @@ -376,7 +424,7 @@ td_api::object_ptr BoostManager::get_chat_boost_link_ void BoostManager::get_dialog_boosts(DialogId dialog_id, bool only_gift_codes, const string &offset, int32 limit, Promise> &&promise) { - if (!td_->messages_manager_->have_dialog_force(dialog_id, "get_dialog_boost_status")) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "get_dialog_boosts")) { return promise.set_error(Status::Error(400, "Chat not found")); } if (!td_->messages_manager_->have_input_peer(dialog_id, AccessRights::Read)) { @@ -389,6 +437,21 @@ void BoostManager::get_dialog_boosts(DialogId dialog_id, bool only_gift_codes, c td_->create_handler(std::move(promise))->send(dialog_id, only_gift_codes, offset, limit); } +void BoostManager::get_user_dialog_boosts(DialogId dialog_id, UserId user_id, + Promise> &&promise) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "get_user_dialog_boosts")) { + return promise.set_error(Status::Error(400, "Chat not found")); + } + if (!td_->messages_manager_->have_input_peer(dialog_id, AccessRights::Read)) { + return promise.set_error(Status::Error(400, "Can't access the chat")); + } + if (!user_id.is_valid()) { + return promise.set_error(Status::Error(400, "User not found")); + } + + td_->create_handler(std::move(promise))->send(dialog_id, user_id); +} + void BoostManager::on_update_dialog_boost(DialogId dialog_id, telegram_api::object_ptr &&boost) { if (!td_->auth_manager_->is_bot()) { LOG(ERROR) << "Receive updateBotChatBoost by a non-bot"; diff --git a/td/telegram/BoostManager.h b/td/telegram/BoostManager.h index 3ffe83c15..8d84a6368 100644 --- a/td/telegram/BoostManager.h +++ b/td/telegram/BoostManager.h @@ -43,6 +43,9 @@ class BoostManager final : public Actor { void get_dialog_boosts(DialogId dialog_id, bool only_gift_codes, const string &offset, int32 limit, Promise> &&promise); + void get_user_dialog_boosts(DialogId dialog_id, UserId user_id, + Promise> &&promise); + void on_update_dialog_boost(DialogId dialog_id, telegram_api::object_ptr &&boost); private: diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 928b51f1b..468f6a8d1 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6674,6 +6674,12 @@ void Td::on_request(uint64 id, td_api::getChatBoosts &request) { request.limit_, std::move(promise)); } +void Td::on_request(uint64 id, const td_api::getUserChatBoosts &request) { + CHECK_IS_BOT(); + CREATE_REQUEST_PROMISE(); + boost_manager_->get_user_dialog_boosts(DialogId(request.chat_id_), UserId(request.user_id_), std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::getAttachmentMenuBot &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index d27db10fd..ddc688183 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1060,6 +1060,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::getChatBoosts &request); + void on_request(uint64 id, const td_api::getUserChatBoosts &request); + void on_request(uint64 id, const td_api::getAttachmentMenuBot &request); void on_request(uint64 id, const td_api::toggleBotIsAddedToAttachmentMenu &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 6e858171e..2c09dafd4 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -4365,6 +4365,11 @@ class CliClient final : public Actor { string limit; get_args(args, chat_id, only_gift_codes, offset, limit); send_request(td_api::make_object(chat_id, only_gift_codes, offset, as_limit(limit))); + } else if (op == "gucb") { + ChatId chat_id; + UserId user_id; + get_args(args, chat_id, user_id); + send_request(td_api::make_object(chat_id, user_id)); } else if (op == "gamb") { UserId user_id; get_args(args, user_id);