From f9dcb217d0415c716e13bc858c3ee9d949149c2d Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 12 Aug 2024 15:05:21 +0300 Subject: [PATCH] Add td_api::togglePaidMessageReactionIsAnonymous. --- td/generate/scheme/td_api.tl | 8 ++++- td/telegram/MessageReaction.cpp | 52 +++++++++++++++++++++++++++++++++ td/telegram/MessageReaction.h | 3 ++ td/telegram/MessagesManager.cpp | 20 +++++++++++++ td/telegram/MessagesManager.h | 3 ++ td/telegram/Td.cpp | 7 +++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 7 +++++ 8 files changed, 101 insertions(+), 1 deletion(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 6e40a7967..06b6aed40 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -9077,7 +9077,7 @@ removeMessageReaction chat_id:int53 message_id:int53 reaction_type:ReactionType //@chat_id Identifier of the chat to which the message belongs //@message_id Identifier of the message //@star_count Number of Telegram Stars to be used for the reaction; 1-getOption("paid_reaction_star_count_max") -//@is_anonymous Pass true to make all paid reactions of the user anonymous; pass false to make the user's profile visible among top reactors +//@is_anonymous Pass true to make paid reaction of the user on the message anonymous; pass false to make the user's profile visible among top reactors addPaidMessageReaction chat_id:int53 message_id:int53 star_count:int53 is_anonymous:Bool = Ok; //@description Removes all pending paid reactions on a message. Can be called within 5 seconds after the last addPaidMessageReaction call @@ -9085,6 +9085,12 @@ addPaidMessageReaction chat_id:int53 message_id:int53 star_count:int53 is_anonym //@message_id Identifier of the message removePendingPaidMessageReactions chat_id:int53 message_id:int53 = Ok; +//@description Changes whether the paid message reaction of the user to a message is anonymous. The message must have paid reaction added by the user +//@chat_id Identifier of the chat to which the message belongs +//@message_id Identifier of the message +//@is_anonymous Pass true to make paid reaction of the user on the message anonymous; pass false to make the user's profile visible among top reactors +togglePaidMessageReactionIsAnonymous chat_id:int53 message_id:int53 is_anonymous:Bool = Ok; + //@description Sets reactions on a message; for bots only //@chat_id Identifier of the chat to which the message belongs //@message_id Identifier of the message diff --git a/td/telegram/MessageReaction.cpp b/td/telegram/MessageReaction.cpp index 49db09307..a4f36482c 100644 --- a/td/telegram/MessageReaction.cpp +++ b/td/telegram/MessageReaction.cpp @@ -201,6 +201,43 @@ class SendPaidReactionQuery final : public Td::ResultHandler { } }; +class TogglePaidReactionPrivacyQuery final : public Td::ResultHandler { + Promise promise_; + DialogId dialog_id_; + + public: + explicit TogglePaidReactionPrivacyQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(MessageFullId message_full_id, bool is_anonymous) { + dialog_id_ = message_full_id.get_dialog_id(); + + auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id_, AccessRights::Read); + if (input_peer == nullptr) { + return on_error(Status::Error(400, "Can't access the chat")); + } + + send_query(G()->net_query_creator().create( + telegram_api::messages_togglePaidReactionPrivacy( + std::move(input_peer), message_full_id.get_message_id().get_server_message_id().get(), is_anonymous), + {{dialog_id_}, {message_full_id}})); + } + + 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()); + } + + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "TogglePaidReactionPrivacyQuery"); + promise_.set_error(std::move(status)); + } +}; + class GetMessageReactionsListQuery final : public Td::ResultHandler { Promise> promise_; DialogId dialog_id_; @@ -1050,6 +1087,21 @@ void MessageReactions::send_paid_message_reaction(Td *td, MessageFullId message_ ->send(message_full_id, star_count, is_anonymous, random_id); } +bool MessageReactions::toggle_paid_message_reaction_is_anonymous(Td *td, MessageFullId message_full_id, + bool is_anonymous, Promise &&promise) { + if (pending_paid_reactions_ != 0) { + pending_is_anonymous_ = is_anonymous; + } + for (auto &top_reactor : top_reactors_) { + if (top_reactor.is_me()) { + top_reactor.add_count(0, is_anonymous); + td->create_handler(std::move(promise))->send(message_full_id, is_anonymous); + return true; + } + } + return pending_paid_reactions_ != 0; +} + StringBuilder &operator<<(StringBuilder &string_builder, const MessageReactions &reactions) { if (reactions.are_tags_) { return string_builder << "MessageTags{" << reactions.reactions_ << '}'; diff --git a/td/telegram/MessageReaction.h b/td/telegram/MessageReaction.h index 4e4f57d63..22ec28121 100644 --- a/td/telegram/MessageReaction.h +++ b/td/telegram/MessageReaction.h @@ -215,6 +215,9 @@ struct MessageReactions { void send_paid_message_reaction(Td *td, MessageFullId message_full_id, int64 random_id, Promise &&promise); + bool toggle_paid_message_reaction_is_anonymous(Td *td, MessageFullId message_full_id, bool is_anonymous, + Promise &&promise); + template void store(StorerT &storer) const; diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 101e1023b..3c764f15e 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -22851,6 +22851,26 @@ void MessagesManager::remove_paid_message_reactions(MessageFullId message_full_i promise.set_value(Unit()); } +void MessagesManager::toggle_paid_message_reaction_is_anonymous(MessageFullId message_full_id, bool is_anonymous, + Promise &&promise) { + auto dialog_id = message_full_id.get_dialog_id(); + Dialog *d = get_dialog_force(dialog_id, "toggle_paid_message_reaction_is_anonymous"); + if (d == nullptr) { + return promise.set_error(Status::Error(400, "Chat not found")); + } + Message *m = get_message_force(d, message_full_id.get_message_id(), "toggle_paid_message_reaction_is_anonymous"); + if (m == nullptr) { + return promise.set_error(Status::Error(400, "Message not found")); + } + if (m->reactions == nullptr) { + return promise.set_error(Status::Error(400, "Message has no paid reactions")); + } + if (m->reactions->toggle_paid_message_reaction_is_anonymous(td_, message_full_id, is_anonymous, std::move(promise))) { + send_update_message_interaction_info(d->dialog_id, m); + on_message_changed(d, m, true, "toggle_paid_message_reaction_is_anonymous"); + } +} + void MessagesManager::set_message_reactions(Dialog *d, Message *m, bool is_big, bool add_to_recent, Promise &&promise) { CHECK(m->reactions != nullptr); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index ef3048d18..35bf13bc4 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -801,6 +801,9 @@ class MessagesManager final : public Actor { void remove_paid_message_reactions(MessageFullId message_full_id, Promise &&promise); + void toggle_paid_message_reaction_is_anonymous(MessageFullId message_full_id, bool is_anonymous, + Promise &&promise); + td_api::object_ptr get_dialog_event_log_message_object( DialogId dialog_id, tl_object_ptr &&message, DialogId &sender_dialog_id); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 40abf9611..bb0f2aaa1 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4984,6 +4984,13 @@ void Td::on_request(uint64 id, const td_api::removePendingPaidMessageReactions & std::move(promise)); } +void Td::on_request(uint64 id, const td_api::togglePaidMessageReactionIsAnonymous &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + messages_manager_->toggle_paid_message_reaction_is_anonymous( + {DialogId(request.chat_id_), MessageId(request.message_id_)}, request.is_anonymous_, std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::removeMessageReaction &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 1a0366f79..460c4e16f 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -814,6 +814,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::removePendingPaidMessageReactions &request); + void on_request(uint64 id, const td_api::togglePaidMessageReactionIsAnonymous &request); + void on_request(uint64 id, const td_api::removeMessageReaction &request); void on_request(uint64 id, const td_api::setMessageReactions &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index d97f6c221..29ecd70e1 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2979,6 +2979,13 @@ class CliClient final : public Actor { MessageId message_id; get_args(args, chat_id, message_id); send_request(td_api::make_object(chat_id, message_id)); + } else if (op == "tpmria") { + ChatId chat_id; + MessageId message_id; + bool is_anonymous; + get_args(args, chat_id, message_id, is_anonymous); + send_request( + td_api::make_object(chat_id, message_id, is_anonymous)); } else if (op == "gmars") { ChatId chat_id; MessageId message_id;