diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index aaf063a9d..85eefef00 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4212,6 +4212,9 @@ getMessagePublicForwards chat_id:int53 message_id:int53 offset:string limit:int3 //@description Returns sponsored messages to be shown in a chat; for channel chats only @chat_id Identifier of the chat getChatSponsoredMessages chat_id:int53 = SponsoredMessages; +//@description Informs TDLib that a sponsored message was viewed by the user @chat_id Chat identifier @message_id The identifier of the sponsored message being viewed +viewSponsoredMessage chat_id:int53 message_id:bytes = Ok; + //@description Removes an active notification from notification list. Needs to be called only if the notification is removed by the current user @notification_group_id Identifier of notification group to which the notification belongs @notification_id Identifier of removed notification removeNotification notification_group_id:int32 notification_id:int32 = Ok; diff --git a/td/telegram/SponsoredMessages.cpp b/td/telegram/SponsoredMessages.cpp index a934a5bde..858524435 100644 --- a/td/telegram/SponsoredMessages.cpp +++ b/td/telegram/SponsoredMessages.cpp @@ -7,11 +7,15 @@ #include "td/telegram/SponsoredMessages.h" #include "td/telegram/ContactsManager.h" +#include "td/telegram/Global.h" #include "td/telegram/MessageContent.h" #include "td/telegram/MessageEntity.h" #include "td/telegram/MessagesManager.h" +#include "td/telegram/net/NetQueryCreator.h" #include "td/telegram/Td.h" +//#include "td/utils/buffer.h" + namespace td { class GetSponsoredMessagesQuery final : public Td::ResultHandler { @@ -75,6 +79,39 @@ class GetSponsoredMessagesQuery final : public Td::ResultHandler { } }; +class ViewSponsoredMessageQuery final : public Td::ResultHandler { + Promise promise_; + ChannelId channel_id_; + + public: + explicit ViewSponsoredMessageQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(ChannelId channel_id, const string &message_id) { + channel_id_ = channel_id; + auto input_channel = td->contacts_manager_->get_input_channel(channel_id); + if (input_channel == nullptr) { + return promise_.set_error(Status::Error(3, "Chat info not found")); + } + send_query(G()->net_query_creator().create( + telegram_api::channels_viewSponsoredMessage(std::move(input_channel), BufferSlice(message_id)))); + } + + void on_result(uint64 id, BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(id, result_ptr.move_as_error()); + } + + promise_.set_value(Unit()); + } + + void on_error(uint64 id, Status status) final { + td->contacts_manager_->on_get_channel_error(channel_id_, status, "ViewSponsoredMessageQuery"); + promise_.set_error(std::move(status)); + } +}; + void get_dialog_sponsored_messages(Td *td, DialogId dialog_id, Promise> &&promise) { if (!td->messages_manager_->have_dialog_force(dialog_id, "get_sponsored_messages")) { @@ -88,4 +125,17 @@ void get_dialog_sponsored_messages(Td *td, DialogId dialog_id, td->create_handler(std::move(promise))->send(dialog_id.get_channel_id()); } +void view_sponsored_message(Td *td, DialogId dialog_id, const string &message_id, Promise &&promise) { + if (!td->messages_manager_->have_dialog_force(dialog_id, "view_sponsored_message")) { + return promise.set_error(Status::Error(400, "Chat not found")); + } + if (dialog_id.get_type() != DialogType::Channel || + td->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) != ContactsManager::ChannelType::Broadcast || + message_id.empty()) { + return promise.set_error(Status::Error(400, "Message not found")); + } + + td->create_handler(std::move(promise))->send(dialog_id.get_channel_id(), message_id); +} + } // namespace td diff --git a/td/telegram/SponsoredMessages.h b/td/telegram/SponsoredMessages.h index 5566285f1..94455d60b 100644 --- a/td/telegram/SponsoredMessages.h +++ b/td/telegram/SponsoredMessages.h @@ -20,4 +20,6 @@ class Td; void get_dialog_sponsored_messages(Td *td, DialogId dialog_id, Promise> &&promise); +void view_sponsored_message(Td *td, DialogId dialog_id, const string &message_id, Promise &&promise); + } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index f31706ba2..ab15aba8d 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5072,6 +5072,12 @@ void Td::on_request(uint64 id, const td_api::getChatSponsoredMessages &request) get_dialog_sponsored_messages(this, DialogId(request.chat_id_), std::move(promise)); } +void Td::on_request(uint64 id, const td_api::viewSponsoredMessage &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + view_sponsored_message(this, DialogId(request.chat_id_), request.message_id_, std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::getMessageThread &request) { CHECK_IS_USER(); CREATE_REQUEST(GetMessageThreadRequest, request.chat_id_, request.message_id_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 3d30ca80c..f8a0332a9 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -514,6 +514,8 @@ class Td final : public NetQueryCallback { void on_request(uint64 id, const td_api::getChatSponsoredMessages &request); + void on_request(uint64 id, const td_api::viewSponsoredMessage &request); + void on_request(uint64 id, const td_api::getMessageLink &request); void on_request(uint64 id, const td_api::getMessageEmbeddingCode &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 9404ef1c2..f52081d5d 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2588,6 +2588,11 @@ class CliClient final : public Actor { send_request(td_api::make_object(as_chat_id(chat_id), as_message_ids(message_ids))); } else if (op == "gsm") { send_request(td_api::make_object(as_chat_id(args))); + } else if (op == "vsm") { + string chat_id; + string message_id; + get_args(args, chat_id, message_id); + send_request(td_api::make_object(as_chat_id(chat_id), message_id)); } else if (op == "gmlink") { string chat_id; string message_id;