tdlight/td/telegram/SponsoredMessageManager.cpp

312 lines
13 KiB
C++
Raw Normal View History

2021-08-24 22:44:47 +02:00
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2021-09-07 14:49:34 +02:00
#include "td/telegram/SponsoredMessageManager.h"
2021-08-24 22:44:47 +02:00
2021-09-01 19:31:39 +02:00
#include "td/telegram/ChannelId.h"
#include "td/telegram/ConfigShared.h"
2021-08-24 22:44:47 +02:00
#include "td/telegram/ContactsManager.h"
2021-08-25 21:34:18 +02:00
#include "td/telegram/Global.h"
2021-08-24 22:44:47 +02:00
#include "td/telegram/MessageContent.h"
#include "td/telegram/MessageEntity.h"
#include "td/telegram/MessagesManager.h"
2021-08-25 21:34:18 +02:00
#include "td/telegram/net/NetQueryCreator.h"
#include "td/telegram/ServerMessageId.h"
2021-08-24 22:44:47 +02:00
#include "td/telegram/Td.h"
2021-09-01 19:31:39 +02:00
#include "td/telegram/telegram_api.h"
2021-08-24 22:44:47 +02:00
2021-09-07 16:21:18 +02:00
#include "td/utils/algorithm.h"
2021-09-01 19:31:39 +02:00
#include "td/utils/buffer.h"
#include "td/utils/logging.h"
2021-11-04 10:46:08 +01:00
#include "td/utils/SliceBuilder.h"
2021-09-01 19:31:39 +02:00
#include "td/utils/Status.h"
2021-08-25 21:34:18 +02:00
2021-09-10 10:44:50 +02:00
#include <limits>
2021-08-24 22:44:47 +02:00
namespace td {
class GetSponsoredMessagesQuery final : public Td::ResultHandler {
2021-09-07 16:21:18 +02:00
Promise<telegram_api::object_ptr<telegram_api::messages_sponsoredMessages>> promise_;
2021-08-24 22:44:47 +02:00
ChannelId channel_id_;
public:
2021-09-07 16:21:18 +02:00
explicit GetSponsoredMessagesQuery(
Promise<telegram_api::object_ptr<telegram_api::messages_sponsoredMessages>> &&promise)
2021-08-24 22:44:47 +02:00
: promise_(std::move(promise)) {
}
void send(ChannelId channel_id) {
channel_id_ = channel_id;
auto input_channel = td_->contacts_manager_->get_input_channel(channel_id);
2021-08-24 22:44:47 +02:00
if (input_channel == nullptr) {
return promise_.set_error(Status::Error(400, "Chat info not found"));
2021-08-24 22:44:47 +02:00
}
send_query(G()->net_query_creator().create(telegram_api::channels_getSponsoredMessages(std::move(input_channel))));
}
void on_result(BufferSlice packet) final {
2021-08-24 22:44:47 +02:00
auto result_ptr = fetch_result<telegram_api::channels_getSponsoredMessages>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
2021-08-24 22:44:47 +02:00
}
2021-09-07 16:21:18 +02:00
promise_.set_value(result_ptr.move_as_ok());
2021-08-24 22:44:47 +02:00
}
void on_error(Status status) final {
td_->contacts_manager_->on_get_channel_error(channel_id_, status, "GetSponsoredMessagesQuery");
2021-08-24 22:44:47 +02:00
promise_.set_error(std::move(status));
}
};
2021-08-25 21:34:18 +02:00
class ViewSponsoredMessageQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
ChannelId channel_id_;
public:
explicit ViewSponsoredMessageQuery(Promise<Unit> &&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);
2021-08-25 21:34:18 +02:00
if (input_channel == nullptr) {
return promise_.set_error(Status::Error(400, "Chat info not found"));
2021-08-25 21:34:18 +02:00
}
send_query(G()->net_query_creator().create(
telegram_api::channels_viewSponsoredMessage(std::move(input_channel), BufferSlice(message_id))));
}
void on_result(BufferSlice packet) final {
2021-08-25 21:34:18 +02:00
auto result_ptr = fetch_result<telegram_api::channels_viewSponsoredMessage>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
2021-08-25 21:34:18 +02:00
}
promise_.set_value(Unit());
}
void on_error(Status status) final {
td_->contacts_manager_->on_get_channel_error(channel_id_, status, "ViewSponsoredMessageQuery");
2021-08-25 21:34:18 +02:00
promise_.set_error(std::move(status));
}
};
2021-09-07 16:21:18 +02:00
struct SponsoredMessageManager::SponsoredMessage {
2021-09-10 10:44:50 +02:00
int32 local_id = 0;
2021-09-07 16:21:18 +02:00
DialogId sponsor_dialog_id;
ServerMessageId server_message_id;
2021-09-07 16:21:18 +02:00
string start_param;
unique_ptr<MessageContent> content;
SponsoredMessage() = default;
SponsoredMessage(int32 local_id, DialogId sponsor_dialog_id, ServerMessageId server_message_id, string start_param,
unique_ptr<MessageContent> content)
2021-09-10 10:44:50 +02:00
: local_id(local_id)
2021-09-07 16:21:18 +02:00
, sponsor_dialog_id(sponsor_dialog_id)
, server_message_id(server_message_id)
2021-09-07 16:21:18 +02:00
, start_param(std::move(start_param))
, content(std::move(content)) {
}
};
struct SponsoredMessageManager::DialogSponsoredMessages {
vector<Promise<td_api::object_ptr<td_api::sponsoredMessages>>> promises;
vector<SponsoredMessage> messages;
2021-09-10 10:44:50 +02:00
std::unordered_map<int32, string> message_random_ids;
2021-09-07 16:21:18 +02:00
};
2021-09-07 14:49:34 +02:00
SponsoredMessageManager::SponsoredMessageManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
2021-09-07 19:27:04 +02:00
delete_cached_sponsored_messages_timeout_.set_callback(on_delete_cached_sponsored_messages_timeout_callback);
delete_cached_sponsored_messages_timeout_.set_callback_data(static_cast<void *>(this));
2021-09-07 14:49:34 +02:00
}
SponsoredMessageManager::~SponsoredMessageManager() = default;
void SponsoredMessageManager::tear_down() {
parent_.reset();
}
2021-09-07 19:27:04 +02:00
void SponsoredMessageManager::on_delete_cached_sponsored_messages_timeout_callback(void *sponsored_message_manager_ptr,
int64 dialog_id_int) {
if (G()->close_flag()) {
return;
}
auto sponsored_message_manager = static_cast<SponsoredMessageManager *>(sponsored_message_manager_ptr);
send_closure_later(sponsored_message_manager->actor_id(sponsored_message_manager),
&SponsoredMessageManager::delete_cached_sponsored_messages, DialogId(dialog_id_int));
}
void SponsoredMessageManager::delete_cached_sponsored_messages(DialogId dialog_id) {
if (G()->close_flag()) {
return;
}
auto it = dialog_sponsored_messages_.find(dialog_id);
CHECK(it != dialog_sponsored_messages_.end());
CHECK(it->second->promises.empty());
dialog_sponsored_messages_.erase(it);
}
2021-09-07 16:21:18 +02:00
td_api::object_ptr<td_api::sponsoredMessage> SponsoredMessageManager::get_sponsored_message_object(
DialogId dialog_id, const SponsoredMessage &sponsored_message) const {
2021-10-08 14:00:13 +02:00
td_api::object_ptr<td_api::InternalLinkType> link;
switch (sponsored_message.sponsor_dialog_id.get_type()) {
case DialogType::User: {
auto user_id = sponsored_message.sponsor_dialog_id.get_user_id();
if (!td_->contacts_manager_->is_user_bot(user_id)) {
break;
}
auto bot_username = td_->contacts_manager_->get_user_username(user_id);
if (bot_username.empty()) {
break;
}
link = td_api::make_object<td_api::internalLinkTypeBotStart>(bot_username, sponsored_message.start_param);
break;
}
2021-11-27 11:36:17 +01:00
case DialogType::Channel:
if (sponsored_message.server_message_id.is_valid()) {
auto channel_id = sponsored_message.sponsor_dialog_id.get_channel_id();
auto t_me = G()->shared_config().get_option_string("t_me_url", "https://t.me/");
link = td_api::make_object<td_api::internalLinkTypeMessage>(
PSTRING() << t_me << "c/" << channel_id.get() << '/' << sponsored_message.server_message_id.get());
}
break;
2021-10-08 14:00:13 +02:00
default:
break;
}
2021-09-07 16:21:18 +02:00
return td_api::make_object<td_api::sponsoredMessage>(
2021-10-08 14:00:13 +02:00
sponsored_message.local_id, sponsored_message.sponsor_dialog_id.get(), std::move(link),
2021-09-07 16:21:18 +02:00
get_message_content_object(sponsored_message.content.get(), td_, dialog_id, 0, false, true, -1));
}
td_api::object_ptr<td_api::sponsoredMessages> SponsoredMessageManager::get_sponsored_messages_object(
DialogId dialog_id, const DialogSponsoredMessages &sponsored_messages) const {
return td_api::make_object<td_api::sponsoredMessages>(
transform(sponsored_messages.messages, [this, dialog_id](const SponsoredMessage &sponsored_message) {
return get_sponsored_message_object(dialog_id, sponsored_message);
}));
}
2021-09-07 14:49:34 +02:00
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_sponsored_messages")) {
2021-08-24 22:44:47 +02:00
return promise.set_error(Status::Error(400, "Chat not found"));
}
if (dialog_id.get_type() != DialogType::Channel ||
2021-09-07 14:49:34 +02:00
td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) != ContactsManager::ChannelType::Broadcast) {
2021-08-24 22:44:47 +02:00
return promise.set_value(td_api::make_object<td_api::sponsoredMessages>());
}
2021-09-07 16:21:18 +02:00
auto &messages = dialog_sponsored_messages_[dialog_id];
2021-09-07 19:27:04 +02:00
if (messages != nullptr && messages->promises.empty()) {
return promise.set_value(get_sponsored_messages_object(dialog_id, *messages));
}
2021-09-07 16:21:18 +02:00
if (messages == nullptr) {
messages = make_unique<DialogSponsoredMessages>();
}
messages->promises.push_back(std::move(promise));
if (messages->promises.size() == 1) {
auto query_promise = PromiseCreator::lambda(
[actor_id = actor_id(this),
dialog_id](Result<telegram_api::object_ptr<telegram_api::messages_sponsoredMessages>> &&result) mutable {
send_closure(actor_id, &SponsoredMessageManager::on_get_dialog_sponsored_messages, dialog_id,
std::move(result));
});
td_->create_handler<GetSponsoredMessagesQuery>(std::move(query_promise))->send(dialog_id.get_channel_id());
}
}
void SponsoredMessageManager::on_get_dialog_sponsored_messages(
DialogId dialog_id, Result<telegram_api::object_ptr<telegram_api::messages_sponsoredMessages>> &&result) {
auto &messages = dialog_sponsored_messages_[dialog_id];
CHECK(messages != nullptr);
auto promises = std::move(messages->promises);
reset_to_empty(messages->promises);
2021-09-10 10:44:50 +02:00
CHECK(messages->messages.empty());
CHECK(messages->message_random_ids.empty());
2021-09-07 16:21:18 +02:00
if (result.is_ok() && G()->close_flag()) {
2021-10-07 15:36:21 +02:00
result = Global::request_aborted_error();
2021-09-07 16:21:18 +02:00
}
if (result.is_error()) {
2021-09-07 19:27:04 +02:00
dialog_sponsored_messages_.erase(dialog_id);
2021-09-07 16:21:18 +02:00
for (auto &promise : promises) {
promise.set_error(result.error().clone());
}
return;
}
auto sponsored_messages = result.move_as_ok();
td_->contacts_manager_->on_get_users(std::move(sponsored_messages->users_), "on_get_dialog_sponsored_messages");
td_->contacts_manager_->on_get_chats(std::move(sponsored_messages->chats_), "on_get_dialog_sponsored_messages");
for (auto &sponsored_message : sponsored_messages->messages_) {
DialogId sponsor_dialog_id(sponsored_message->from_id_);
if (!sponsor_dialog_id.is_valid() || !td_->messages_manager_->have_dialog_info_force(sponsor_dialog_id)) {
LOG(ERROR) << "Receive unknown sponsor " << sponsor_dialog_id;
continue;
}
auto server_message_id = ServerMessageId(sponsored_message->channel_post_);
if (!server_message_id.is_valid() && server_message_id != ServerMessageId()) {
LOG(ERROR) << "Receive invalid channel post in " << to_string(sponsored_message);
server_message_id = ServerMessageId();
}
2021-09-07 16:21:18 +02:00
td_->messages_manager_->force_create_dialog(sponsor_dialog_id, "on_get_dialog_sponsored_messages");
auto message_text = get_message_text(td_->contacts_manager_.get(), std::move(sponsored_message->message_),
std::move(sponsored_message->entities_), true, true, 0, false,
"on_get_dialog_sponsored_messages");
int32 ttl = 0;
bool disable_web_page_preview = false;
auto content = get_message_content(td_, std::move(message_text), nullptr, sponsor_dialog_id, true, UserId(), &ttl,
&disable_web_page_preview);
2021-09-07 16:21:18 +02:00
if (ttl != 0) {
LOG(ERROR) << "Receive sponsored message with TTL " << ttl;
continue;
}
CHECK(disable_web_page_preview);
2021-09-07 16:21:18 +02:00
2021-09-10 10:44:50 +02:00
CHECK(current_sponsored_message_id_ < std::numeric_limits<int32>::max());
auto local_id = ++current_sponsored_message_id_;
messages->message_random_ids[local_id] = sponsored_message->random_id_.as_slice().str();
messages->messages.emplace_back(local_id, sponsor_dialog_id, server_message_id,
std::move(sponsored_message->start_param_), std::move(content));
2021-09-07 16:21:18 +02:00
}
for (auto &promise : promises) {
promise.set_value(get_sponsored_messages_object(dialog_id, *messages));
}
2021-09-07 19:27:04 +02:00
delete_cached_sponsored_messages_timeout_.set_timeout_in(dialog_id.get(), 300.0);
2021-08-24 22:44:47 +02:00
}
2021-09-10 10:44:50 +02:00
void SponsoredMessageManager::view_sponsored_message(DialogId dialog_id, int32 sponsored_message_id,
2021-09-07 14:49:34 +02:00
Promise<Unit> &&promise) {
if (!td_->messages_manager_->have_dialog_force(dialog_id, "view_sponsored_message")) {
2021-08-25 21:34:18 +02:00
return promise.set_error(Status::Error(400, "Chat not found"));
}
2021-12-07 10:32:23 +01:00
if (!td_->messages_manager_->is_dialog_opened(dialog_id)) {
return promise.set_value(Unit());
}
2021-09-10 10:44:50 +02:00
auto it = dialog_sponsored_messages_.find(dialog_id);
if (it == dialog_sponsored_messages_.end()) {
return promise.set_value(Unit());
}
auto random_id_it = it->second->message_random_ids.find(sponsored_message_id);
if (random_id_it == it->second->message_random_ids.end()) {
return promise.set_value(Unit());
2021-08-25 21:34:18 +02:00
}
2021-09-10 10:44:50 +02:00
auto random_id = std::move(random_id_it->second);
it->second->message_random_ids.erase(random_id_it);
td_->create_handler<ViewSponsoredMessageQuery>(std::move(promise))->send(dialog_id.get_channel_id(), random_id);
2021-08-25 21:34:18 +02:00
}
2021-08-24 22:44:47 +02:00
} // namespace td