From 2969f570b8a26413d991b887b89e2cd3bdc1878a Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 2 Jun 2023 14:42:28 +0300 Subject: [PATCH] Add class td_api::messageSponsor. --- td/generate/scheme/td_api.tl | 26 ++++++++++---- td/telegram/Photo.h | 1 + td/telegram/SponsoredMessageManager.cpp | 47 +++++++++++++++++++------ td/telegram/SponsoredMessageManager.h | 3 ++ 4 files changed, 60 insertions(+), 17 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 50278302f..7d97c1340 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -1223,17 +1223,31 @@ messageSourceScreenshot = MessageSource; messageSourceOther = MessageSource; +//@class MessageSponsorType @description Describes type of a message sponsor + +//@description The sponsor is a bot @bot_user_id User identifier of the bot @link An internal link to be opened when the sponsored message is clicked +messageSponsorTypeBot bot_user_id:int53 link:InternalLinkType = MessageSponsorType; + +//@description The sponsor is a public channel chat @chat_id Sponsor chat identifier @link An internal link to be opened when the sponsored message is clicked; may be null if the sponsor chat needs to be opened instead +messageSponsorTypePublicChannel chat_id:int53 link:InternalLinkType = MessageSponsorType; + +//@description The sponsor is a private channel chat @title Title of the chat @invite_link Invite link for the channel +messageSponsorTypePrivateChannel title:string invite_link:string = MessageSponsorType; + + +//@description Information about the sponsor of a message +//@type Type of the sponsor +//@photo Photo of the sponsor; may be null if must not be shown +//@info Additional optional information about the sponsor to be shown along with the message +messageSponsor type:MessageSponsorType photo:chatPhotoInfo info:string = MessageSponsor; + //@description Describes a sponsored message //@message_id Message identifier; unique for the chat to which the sponsored message belongs among both ordinary and sponsored messages //@is_recommended True, if the message needs to be labeled as "recommended" instead of "sponsored" -//@sponsor_chat_id Sponsor chat identifier; 0 if the sponsor chat is accessible through an invite link -//@sponsor_chat_info Information about the sponsor chat; may be null unless sponsor_chat_id == 0 -//@show_chat_photo True, if the sponsor's chat photo must be shown -//@link An internal link to be opened when the sponsored message is clicked; may be null if the sponsor chat needs to be opened instead //@content Content of the message. Currently, can be only of the type messageText -//@sponsor_info If non-empty, information about the sponsor to be shown along with the message +//@sponsor Information about the sponsor of the message //@additional_info If non-empty, additional information about the sponsored message to be shown along with the message -sponsoredMessage message_id:int53 is_recommended:Bool sponsor_chat_id:int53 sponsor_chat_info:chatInviteLinkInfo show_chat_photo:Bool link:InternalLinkType content:MessageContent sponsor_info:string additional_info:string = SponsoredMessage; +sponsoredMessage message_id:int53 is_recommended:Bool content:MessageContent sponsor:messageSponsor additional_info:string = SponsoredMessage; //@description Contains a list of sponsored messages @messages List of sponsored messages @messages_between The minimum number of messages between shown sponsored messages, or 0 if only one sponsored message must be shown after all ordinary messages sponsoredMessages messages:vector messages_between:int32 = SponsoredMessages; diff --git a/td/telegram/Photo.h b/td/telegram/Photo.h index f3c2470bb..5b3df191b 100644 --- a/td/telegram/Photo.h +++ b/td/telegram/Photo.h @@ -84,6 +84,7 @@ StringBuilder &operator<<(StringBuilder &string_builder, const ProfilePhoto &pro DialogPhoto get_dialog_photo(FileManager *file_manager, DialogId dialog_id, int64 dialog_access_hash, tl_object_ptr &&chat_photo_ptr); + tl_object_ptr get_chat_photo_info_object(FileManager *file_manager, const DialogPhoto *dialog_photo); diff --git a/td/telegram/SponsoredMessageManager.cpp b/td/telegram/SponsoredMessageManager.cpp index d278cedd0..212eccfaa 100644 --- a/td/telegram/SponsoredMessageManager.cpp +++ b/td/telegram/SponsoredMessageManager.cpp @@ -15,6 +15,7 @@ #include "td/telegram/MessagesManager.h" #include "td/telegram/net/NetQueryCreator.h" #include "td/telegram/OptionManager.h" +#include "td/telegram/Photo.h" #include "td/telegram/ServerMessageId.h" #include "td/telegram/Td.h" #include "td/telegram/telegram_api.h" @@ -195,10 +196,10 @@ void SponsoredMessageManager::delete_cached_sponsored_messages(DialogId dialog_i } } -td_api::object_ptr SponsoredMessageManager::get_sponsored_message_object( - DialogId dialog_id, const SponsoredMessage &sponsored_message) const { - td_api::object_ptr chat_invite_link_info; - td_api::object_ptr link; +td_api::object_ptr SponsoredMessageManager::get_message_sponsor_object( + const SponsoredMessage &sponsored_message) const { + td_api::object_ptr type; + td_api::object_ptr photo; switch (sponsored_message.sponsor_dialog_id.get_type()) { case DialogType::User: { auto user_id = sponsored_message.sponsor_dialog_id.get_user_id(); @@ -211,7 +212,13 @@ td_api::object_ptr SponsoredMessageManager::get_sponso LOG(ERROR) << "Sponsor " << user_id << " has no username"; return nullptr; } - link = td_api::make_object(bot_username, sponsored_message.start_param, false); + type = td_api::make_object( + td_->contacts_manager_->get_user_id_object(user_id, "messageSponsorTypeBot"), + td_api::make_object(bot_username, sponsored_message.start_param, false)); + if (sponsored_message.show_dialog_photo) { + photo = get_chat_photo_info_object(td_->file_manager_.get(), + td_->contacts_manager_->get_user_dialog_photo(user_id)); + } break; } case DialogType::Channel: { @@ -220,17 +227,25 @@ td_api::object_ptr SponsoredMessageManager::get_sponso LOG(ERROR) << "Sponsor " << channel_id << " is not a channel"; return nullptr; } + td_api::object_ptr link; if (sponsored_message.server_message_id.is_valid()) { link = td_api::make_object( PSTRING() << LinkManager::get_t_me_url() << "c/" << channel_id.get() << '/' << sponsored_message.server_message_id.get()); } + type = td_api::make_object( + td_->messages_manager_->get_chat_id_object(sponsored_message.sponsor_dialog_id, "sponsoredMessage"), + std::move(link)); + if (sponsored_message.show_dialog_photo) { + photo = get_chat_photo_info_object(td_->file_manager_.get(), + td_->contacts_manager_->get_channel_dialog_photo(channel_id)); + } break; } case DialogType::None: { CHECK(!sponsored_message.invite_hash.empty()); auto invite_link = LinkManager::get_dialog_invite_link(sponsored_message.invite_hash, false); - chat_invite_link_info = td_->contacts_manager_->get_chat_invite_link_info_object(invite_link); + auto chat_invite_link_info = td_->contacts_manager_->get_chat_invite_link_info_object(invite_link); if (chat_invite_link_info == nullptr) { LOG(ERROR) << "Failed to get invite link info for " << invite_link; return nullptr; @@ -240,18 +255,28 @@ td_api::object_ptr SponsoredMessageManager::get_sponso LOG(ERROR) << "Receive sponsor chat of a wrong type " << to_string(chat_invite_link_info->type_); return nullptr; } - link = td_api::make_object( - LinkManager::get_dialog_invite_link(sponsored_message.invite_hash, true)); + type = td_api::make_object(chat_invite_link_info->title_, invite_link); + if (sponsored_message.show_dialog_photo) { + photo = std::move(chat_invite_link_info->photo_); + } + break; } default: break; } + return td_api::make_object(std::move(type), std::move(photo), sponsored_message.sponsor_info); +} + +td_api::object_ptr SponsoredMessageManager::get_sponsored_message_object( + DialogId dialog_id, const SponsoredMessage &sponsored_message) const { + auto sponsor = get_message_sponsor_object(sponsored_message); + if (sponsor == nullptr) { + return nullptr; + } return td_api::make_object( sponsored_message.local_id, sponsored_message.is_recommended, - td_->messages_manager_->get_chat_id_object(sponsored_message.sponsor_dialog_id, "sponsoredMessage"), - std::move(chat_invite_link_info), sponsored_message.show_dialog_photo, std::move(link), get_message_content_object(sponsored_message.content.get(), td_, dialog_id, 0, false, true, -1), - sponsored_message.sponsor_info, sponsored_message.additional_info); + std::move(sponsor), sponsored_message.additional_info); } td_api::object_ptr SponsoredMessageManager::get_sponsored_messages_object( diff --git a/td/telegram/SponsoredMessageManager.h b/td/telegram/SponsoredMessageManager.h index cdc2488e4..34fdc5f53 100644 --- a/td/telegram/SponsoredMessageManager.h +++ b/td/telegram/SponsoredMessageManager.h @@ -51,6 +51,9 @@ class SponsoredMessageManager final : public Actor { void delete_cached_sponsored_messages(DialogId dialog_id); + td_api::object_ptr get_message_sponsor_object( + const SponsoredMessage &sponsored_message) const; + td_api::object_ptr get_sponsored_message_object( DialogId dialog_id, const SponsoredMessage &sponsored_message) const;