Support multiple sponsored messages.

This commit is contained in:
levlam 2022-10-21 15:04:56 +03:00
parent e6d539097e
commit ec1310a0e7
6 changed files with 26 additions and 23 deletions

View File

@ -960,6 +960,9 @@ messageCalendar total_count:int32 days:vector<messageCalendarDay> = MessageCalen
//@content Content of the message. Currently, can be only of the type messageText
sponsoredMessage message_id:int53 is_recommended:Bool sponsor_chat_id:int53 sponsor_chat_info:chatInviteLinkInfo show_chat_photo:Bool link:InternalLinkType content:MessageContent = 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<sponsoredMessage> messages_between:int32 = SponsoredMessages;
//@description Describes a file added to file download list
//@file_id File identifier
@ -5074,8 +5077,8 @@ getChatScheduledMessages chat_id:int53 = Messages;
//@limit The maximum number of messages to be returned; must be positive and can't be greater than 100. For optimal performance, the number of returned messages is chosen by TDLib and can be smaller than the specified limit
getMessagePublicForwards chat_id:int53 message_id:int53 offset:string limit:int32 = FoundMessages;
//@description Returns sponsored message to be shown in a chat; for channel chats only. Returns a 404 error if there is no sponsored message in the chat @chat_id Identifier of the chat
getChatSponsoredMessage chat_id:int53 = SponsoredMessage;
//@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 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

View File

@ -23,7 +23,6 @@
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/logging.h"
#include "td/utils/Random.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/Status.h"
@ -114,9 +113,10 @@ struct SponsoredMessageManager::SponsoredMessage {
};
struct SponsoredMessageManager::DialogSponsoredMessages {
vector<Promise<td_api::object_ptr<td_api::sponsoredMessage>>> promises;
vector<Promise<td_api::object_ptr<td_api::sponsoredMessages>>> promises;
vector<SponsoredMessage> messages;
FlatHashMap<int64, string> message_random_ids;
int32 messages_between = 0;
};
SponsoredMessageManager::SponsoredMessageManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
@ -197,27 +197,26 @@ td_api::object_ptr<td_api::sponsoredMessage> SponsoredMessageManager::get_sponso
get_message_content_object(sponsored_message.content.get(), td_, dialog_id, 0, false, true, -1));
}
td_api::object_ptr<td_api::sponsoredMessage> SponsoredMessageManager::get_sponsored_message_object(
td_api::object_ptr<td_api::sponsoredMessages> SponsoredMessageManager::get_sponsored_messages_object(
DialogId dialog_id, const DialogSponsoredMessages &sponsored_messages) const {
if (sponsored_messages.messages.empty()) {
return nullptr;
}
auto pos = Random::fast(0, static_cast<int>(sponsored_messages.messages.size()) - 1);
return get_sponsored_message_object(dialog_id, sponsored_messages.messages[pos]);
auto messages = transform(sponsored_messages.messages, [this, dialog_id](const SponsoredMessage &message) {
return get_sponsored_message_object(dialog_id, message);
});
return td_api::make_object<td_api::sponsoredMessages>(std::move(messages), sponsored_messages.messages_between);
}
void SponsoredMessageManager::get_dialog_sponsored_message(
DialogId dialog_id, Promise<td_api::object_ptr<td_api::sponsoredMessage>> &&promise) {
void SponsoredMessageManager::get_dialog_sponsored_messages(
DialogId dialog_id, Promise<td_api::object_ptr<td_api::sponsoredMessages>> &&promise) {
if (!td_->messages_manager_->have_dialog_force(dialog_id, "get_dialog_sponsored_message")) {
return promise.set_error(Status::Error(400, "Chat not found"));
}
if (dialog_id.get_type() != DialogType::Channel) {
return promise.set_value(nullptr);
return promise.set_value(td_api::make_object<td_api::sponsoredMessages>());
}
auto &messages = dialog_sponsored_messages_[dialog_id];
if (messages != nullptr && messages->promises.empty()) {
return promise.set_value(get_sponsored_message_object(dialog_id, *messages));
return promise.set_value(get_sponsored_messages_object(dialog_id, *messages));
}
if (messages == nullptr) {
@ -328,8 +327,9 @@ void SponsoredMessageManager::on_get_dialog_sponsored_messages(
messages->messages.emplace_back(
local_id, sponsored_message->recommended_, sponsored_message->show_peer_photo_, sponsor_dialog_id,
server_message_id, std::move(sponsored_message->start_param_), std::move(invite_hash), std::move(content));
break;
}
messages->messages_between = sponsored_messages->posts_between_;
break;
}
case telegram_api::messages_sponsoredMessagesEmpty::ID:
break;
@ -338,7 +338,7 @@ void SponsoredMessageManager::on_get_dialog_sponsored_messages(
}
for (auto &promise : promises) {
promise.set_value(get_sponsored_message_object(dialog_id, *messages));
promise.set_value(get_sponsored_messages_object(dialog_id, *messages));
}
delete_cached_sponsored_messages_timeout_.set_timeout_in(dialog_id.get(), 300.0);
}

View File

@ -32,8 +32,8 @@ class SponsoredMessageManager final : public Actor {
SponsoredMessageManager &operator=(SponsoredMessageManager &&) = delete;
~SponsoredMessageManager() final;
void get_dialog_sponsored_message(DialogId dialog_id,
Promise<td_api::object_ptr<td_api::sponsoredMessage>> &&promise);
void get_dialog_sponsored_messages(DialogId dialog_id,
Promise<td_api::object_ptr<td_api::sponsoredMessages>> &&promise);
void view_sponsored_message(DialogId dialog_id, MessageId sponsored_message_id);
@ -51,7 +51,7 @@ class SponsoredMessageManager final : public Actor {
td_api::object_ptr<td_api::sponsoredMessage> get_sponsored_message_object(
DialogId dialog_id, const SponsoredMessage &sponsored_message) const;
td_api::object_ptr<td_api::sponsoredMessage> get_sponsored_message_object(
td_api::object_ptr<td_api::sponsoredMessages> get_sponsored_messages_object(
DialogId dialog_id, const DialogSponsoredMessages &sponsored_messages) const;
void on_get_dialog_sponsored_messages(

View File

@ -4639,10 +4639,10 @@ void Td::on_request(uint64 id, const td_api::getMessages &request) {
CREATE_REQUEST(GetMessagesRequest, request.chat_id_, request.message_ids_);
}
void Td::on_request(uint64 id, const td_api::getChatSponsoredMessage &request) {
void Td::on_request(uint64 id, const td_api::getChatSponsoredMessages &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
sponsored_message_manager_->get_dialog_sponsored_message(DialogId(request.chat_id_), std::move(promise));
sponsored_message_manager_->get_dialog_sponsored_messages(DialogId(request.chat_id_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getMessageThread &request) {

View File

@ -531,7 +531,7 @@ class Td final : public Actor {
void on_request(uint64 id, const td_api::getMessages &request);
void on_request(uint64 id, const td_api::getChatSponsoredMessage &request);
void on_request(uint64 id, const td_api::getChatSponsoredMessages &request);
void on_request(uint64 id, const td_api::getMessageLink &request);

View File

@ -2974,7 +2974,7 @@ class CliClient final : public Actor {
} else if (op == "gcspm") {
ChatId chat_id;
get_args(args, chat_id);
send_request(td_api::make_object<td_api::getChatSponsoredMessage>(chat_id));
send_request(td_api::make_object<td_api::getChatSponsoredMessages>(chat_id));
} else if (op == "gmlink") {
ChatId chat_id;
MessageId message_id;