Add SponsoredMessageManager.

This commit is contained in:
levlam 2021-09-07 15:49:34 +03:00
parent 218c693910
commit ae6b79d57b
8 changed files with 88 additions and 40 deletions

View File

@ -400,7 +400,7 @@ set(TDLIB_SOURCE
td/telegram/SendCodeHelper.cpp
td/telegram/SequenceDispatcher.cpp
td/telegram/SpecialStickerSetType.cpp
td/telegram/SponsoredMessages.cpp
td/telegram/SponsoredMessageManager.cpp
td/telegram/StateManager.cpp
td/telegram/StickersManager.cpp
td/telegram/StorageManager.cpp
@ -615,7 +615,7 @@ set(TDLIB_SOURCE
td/telegram/ServerMessageId.h
td/telegram/SetWithPosition.h
td/telegram/SpecialStickerSetType.h
td/telegram/SponsoredMessages.h
td/telegram/SponsoredMessageManager.h
td/telegram/StateManager.h
td/telegram/StickerSetId.h
td/telegram/StickersManager.h

View File

@ -301,6 +301,7 @@ function split_file($file, $chunks, $undo) {
'PublicDialogType|get_public_dialog_type' => 'PublicDialogType',
'SecretChatActor' => 'SecretChatActor',
'secret_chats_manager[_(-][^.]|SecretChatsManager' => 'SecretChatsManager',
'sponsored_message_manager[_(-][^.]|SponsoredMessageManager' => 'SponsoredMessageManager',
'stickers_manager[_(-][^.]|StickersManager' => 'StickersManager',
'[>](td_db[(][)]|get_td_db_impl[(])|TdDb[^A-Za-z]' => 'TdDb',
'theme_manager[_(-][^.]|ThemeManager' => "ThemeManager",

View File

@ -48,6 +48,7 @@ class NetQueryDispatcher;
class NotificationManager;
class PasswordManager;
class SecretChatsManager;
class SponsoredMessageManager;
class StateManager;
class StickersManager;
class StorageManager;
@ -275,6 +276,13 @@ class Global final : public ActorContext {
secret_chats_manager_ = secret_chats_manager;
}
ActorId<SponsoredMessageManager> sponsored_message_manager() const {
return sponsored_message_manager_;
}
void set_sponsored_message_manager(ActorId<SponsoredMessageManager> sponsored_message_manager) {
sponsored_message_manager_ = sponsored_message_manager;
}
ActorId<StickersManager> stickers_manager() const {
return stickers_manager_;
}
@ -425,6 +433,7 @@ class Global final : public ActorContext {
ActorId<NotificationManager> notification_manager_;
ActorId<PasswordManager> password_manager_;
ActorId<SecretChatsManager> secret_chats_manager_;
ActorId<SponsoredMessageManager> sponsored_message_manager_;
ActorId<StickersManager> stickers_manager_;
ActorId<StorageManager> storage_manager_;
ActorId<ThemeManager> theme_manager_;

View File

@ -4,7 +4,7 @@
// 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)
//
#include "td/telegram/SponsoredMessages.h"
#include "td/telegram/SponsoredMessageManager.h"
#include "td/telegram/ChannelId.h"
#include "td/telegram/ContactsManager.h"
@ -116,30 +116,40 @@ class ViewSponsoredMessageQuery final : public Td::ResultHandler {
}
};
void get_dialog_sponsored_messages(Td *td, DialogId dialog_id,
Promise<td_api::object_ptr<td_api::sponsoredMessages>> &&promise) {
if (!td->messages_manager_->have_dialog_force(dialog_id, "get_sponsored_messages")) {
SponsoredMessageManager::SponsoredMessageManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
}
SponsoredMessageManager::~SponsoredMessageManager() = default;
void SponsoredMessageManager::tear_down() {
parent_.reset();
}
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")) {
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) {
td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) != ContactsManager::ChannelType::Broadcast) {
return promise.set_value(td_api::make_object<td_api::sponsoredMessages>());
}
td->create_handler<GetSponsoredMessagesQuery>(std::move(promise))->send(dialog_id.get_channel_id());
td_->create_handler<GetSponsoredMessagesQuery>(std::move(promise))->send(dialog_id.get_channel_id());
}
void view_sponsored_message(Td *td, DialogId dialog_id, const string &message_id, Promise<Unit> &&promise) {
if (!td->messages_manager_->have_dialog_force(dialog_id, "view_sponsored_message")) {
void SponsoredMessageManager::view_sponsored_message(DialogId dialog_id, const string &message_id,
Promise<Unit> &&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 ||
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<ViewSponsoredMessageQuery>(std::move(promise))->send(dialog_id.get_channel_id(), message_id);
td_->create_handler<ViewSponsoredMessageQuery>(std::move(promise))->send(dialog_id.get_channel_id(), message_id);
}
} // namespace td

View File

@ -0,0 +1,42 @@
//
// 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)
//
#pragma once
#include "td/telegram/DialogId.h"
#include "td/telegram/td_api.h"
#include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h"
#include "td/utils/common.h"
namespace td {
class Td;
class SponsoredMessageManager final : public Actor {
public:
SponsoredMessageManager(Td *td, ActorShared<> parent);
SponsoredMessageManager(const SponsoredMessageManager &) = delete;
SponsoredMessageManager &operator=(const SponsoredMessageManager &) = delete;
SponsoredMessageManager(SponsoredMessageManager &&) = delete;
SponsoredMessageManager &operator=(SponsoredMessageManager &&) = delete;
~SponsoredMessageManager() final;
void get_dialog_sponsored_messages(DialogId dialog_id,
Promise<td_api::object_ptr<td_api::sponsoredMessages>> &&promise);
void view_sponsored_message(DialogId dialog_id, const string &message_id, Promise<Unit> &&promise);
private:
void tear_down() final;
Td *td_;
ActorShared<> parent_;
};
} // namespace td

View File

@ -1,25 +0,0 @@
//
// 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)
//
#pragma once
#include "td/telegram/DialogId.h"
#include "td/telegram/td_api.h"
#include "td/actor/PromiseFuture.h"
#include "td/utils/common.h"
namespace td {
class Td;
void get_dialog_sponsored_messages(Td *td, DialogId dialog_id,
Promise<td_api::object_ptr<td_api::sponsoredMessages>> &&promise);
void view_sponsored_message(Td *td, DialogId dialog_id, const string &message_id, Promise<Unit> &&promise);
} // namespace td

View File

@ -91,7 +91,7 @@
#include "td/telegram/SecretChatsManager.h"
#include "td/telegram/SecureManager.h"
#include "td/telegram/SecureValue.h"
#include "td/telegram/SponsoredMessages.h"
#include "td/telegram/SponsoredMessageManager.h"
#include "td/telegram/StateManager.h"
#include "td/telegram/StickerSetId.h"
#include "td/telegram/StickersManager.h"
@ -3639,6 +3639,8 @@ void Td::dec_actor_refcnt() {
LOG(DEBUG) << "NotificationManager was cleared" << timer;
poll_manager_.reset();
LOG(DEBUG) << "PollManager was cleared" << timer;
sponsored_message_manager_.reset();
LOG(DEBUG) << "SponsoredMessageManager was cleared" << timer;
stickers_manager_.reset();
LOG(DEBUG) << "StickersManager was cleared" << timer;
theme_manager_.reset();
@ -3831,6 +3833,8 @@ void Td::clear() {
LOG(DEBUG) << "NotificationManager actor was cleared" << timer;
poll_manager_actor_.reset();
LOG(DEBUG) << "PollManager actor was cleared" << timer;
sponsored_message_manager_actor_.reset();
LOG(DEBUG) << "SponsoredMessageManager actor was cleared" << timer;
stickers_manager_actor_.reset();
LOG(DEBUG) << "StickersManager actor was cleared" << timer;
theme_manager_actor_.reset();
@ -4287,6 +4291,9 @@ void Td::init_managers() {
poll_manager_ = make_unique<PollManager>(this, create_reference());
poll_manager_actor_ = register_actor("PollManager", poll_manager_.get());
G()->set_notification_manager(notification_manager_actor_.get());
sponsored_message_manager_ = make_unique<SponsoredMessageManager>(this, create_reference());
sponsored_message_manager_actor_ = register_actor("SponsoredMessageManager", sponsored_message_manager_.get());
G()->set_sponsored_message_manager(sponsored_message_manager_actor_.get());
stickers_manager_ = make_unique<StickersManager>(this, create_reference());
stickers_manager_actor_ = register_actor("StickersManager", stickers_manager_.get());
G()->set_stickers_manager(stickers_manager_actor_.get());
@ -4992,13 +4999,14 @@ void Td::on_request(uint64 id, const td_api::getMessages &request) {
void Td::on_request(uint64 id, const td_api::getChatSponsoredMessages &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
get_dialog_sponsored_messages(this, 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::viewSponsoredMessage &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
view_sponsored_message(this, DialogId(request.chat_id_), request.message_id_, std::move(promise));
sponsored_message_manager_->view_sponsored_message(DialogId(request.chat_id_), request.message_id_,
std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getMessageThread &request) {

View File

@ -66,6 +66,7 @@ class PollManager;
class PrivacyManager;
class SecureManager;
class SecretChatsManager;
class SponsoredMessageManager;
class StickersManager;
class StorageManager;
class ThemeManager;
@ -173,6 +174,8 @@ class Td final : public NetQueryCallback {
ActorOwn<NotificationManager> notification_manager_actor_;
unique_ptr<PollManager> poll_manager_;
ActorOwn<PollManager> poll_manager_actor_;
unique_ptr<SponsoredMessageManager> sponsored_message_manager_;
ActorOwn<SponsoredMessageManager> sponsored_message_manager_actor_;
unique_ptr<StickersManager> stickers_manager_;
ActorOwn<StickersManager> stickers_manager_actor_;
unique_ptr<ThemeManager> theme_manager_;