Combine getSponsoredMessages queries.

This commit is contained in:
levlam 2021-09-07 17:21:18 +03:00
parent ae6b79d57b
commit efbb5bde96
2 changed files with 119 additions and 32 deletions

View File

@ -16,6 +16,7 @@
#include "td/telegram/Td.h" #include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h" #include "td/telegram/telegram_api.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h" #include "td/utils/buffer.h"
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/Status.h" #include "td/utils/Status.h"
@ -23,11 +24,12 @@
namespace td { namespace td {
class GetSponsoredMessagesQuery final : public Td::ResultHandler { class GetSponsoredMessagesQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::sponsoredMessages>> promise_; Promise<telegram_api::object_ptr<telegram_api::messages_sponsoredMessages>> promise_;
ChannelId channel_id_; ChannelId channel_id_;
public: public:
explicit GetSponsoredMessagesQuery(Promise<td_api::object_ptr<td_api::sponsoredMessages>> &&promise) explicit GetSponsoredMessagesQuery(
Promise<telegram_api::object_ptr<telegram_api::messages_sponsoredMessages>> &&promise)
: promise_(std::move(promise)) { : promise_(std::move(promise)) {
} }
@ -46,35 +48,7 @@ class GetSponsoredMessagesQuery final : public Td::ResultHandler {
return on_error(id, result_ptr.move_as_error()); return on_error(id, result_ptr.move_as_error());
} }
auto sponsored_messages = result_ptr.move_as_ok(); promise_.set_value(result_ptr.move_as_ok());
td->contacts_manager_->on_get_users(std::move(sponsored_messages->users_), "GetSponsoredMessagesQuery");
td->contacts_manager_->on_get_chats(std::move(sponsored_messages->chats_), "GetSponsoredMessagesQuery");
vector<td_api::object_ptr<td_api::sponsoredMessage>> messages;
for (auto &sponsored_message : sponsored_messages->messages_) {
DialogId dialog_id(sponsored_message->from_id_);
if (!dialog_id.is_valid() || !td->messages_manager_->have_dialog_info_force(dialog_id)) {
LOG(ERROR) << "Receive unknown sponsor " << dialog_id;
continue;
}
td->messages_manager_->force_create_dialog(dialog_id, "GetSponsoredMessagesQuery");
auto message_text =
get_message_text(td->contacts_manager_.get(), std::move(sponsored_message->message_),
std::move(sponsored_message->entities_), true, true, 0, false, "GetSponsoredMessagesQuery");
int32 ttl = 0;
auto content = get_message_content(td, std::move(message_text), nullptr, dialog_id, true, UserId(), &ttl);
if (ttl != 0) {
LOG(ERROR) << "Receive sponsored message with TTL " << ttl;
continue;
}
messages.push_back(td_api::make_object<td_api::sponsoredMessage>(
sponsored_message->random_id_.as_slice().str(), dialog_id.get(), sponsored_message->start_param_,
get_message_content_object(content.get(), td, DialogId(channel_id_), 0, false, true, -1)));
}
promise_.set_value(td_api::make_object<td_api::sponsoredMessages>(std::move(messages)));
} }
void on_error(uint64 id, Status status) final { void on_error(uint64 id, Status status) final {
@ -116,6 +90,26 @@ class ViewSponsoredMessageQuery final : public Td::ResultHandler {
} }
}; };
struct SponsoredMessageManager::SponsoredMessage {
string random_id;
DialogId sponsor_dialog_id;
string start_param;
unique_ptr<MessageContent> content;
SponsoredMessage() = default;
SponsoredMessage(string random_id, DialogId sponsor_dialog_id, string start_param, unique_ptr<MessageContent> content)
: random_id(std::move(random_id))
, sponsor_dialog_id(sponsor_dialog_id)
, 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;
};
SponsoredMessageManager::SponsoredMessageManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { SponsoredMessageManager::SponsoredMessageManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
} }
@ -125,6 +119,21 @@ void SponsoredMessageManager::tear_down() {
parent_.reset(); parent_.reset();
} }
td_api::object_ptr<td_api::sponsoredMessage> SponsoredMessageManager::get_sponsored_message_object(
DialogId dialog_id, const SponsoredMessage &sponsored_message) const {
return td_api::make_object<td_api::sponsoredMessage>(
sponsored_message.random_id, sponsored_message.sponsor_dialog_id.get(), sponsored_message.start_param,
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);
}));
}
void SponsoredMessageManager::get_dialog_sponsored_messages( void SponsoredMessageManager::get_dialog_sponsored_messages(
DialogId dialog_id, Promise<td_api::object_ptr<td_api::sponsoredMessages>> &&promise) { DialogId dialog_id, Promise<td_api::object_ptr<td_api::sponsoredMessages>> &&promise) {
if (!td_->messages_manager_->have_dialog_force(dialog_id, "get_sponsored_messages")) { if (!td_->messages_manager_->have_dialog_force(dialog_id, "get_sponsored_messages")) {
@ -135,7 +144,69 @@ void SponsoredMessageManager::get_dialog_sponsored_messages(
return promise.set_value(td_api::make_object<td_api::sponsoredMessages>()); return promise.set_value(td_api::make_object<td_api::sponsoredMessages>());
} }
td_->create_handler<GetSponsoredMessagesQuery>(std::move(promise))->send(dialog_id.get_channel_id()); auto &messages = dialog_sponsored_messages_[dialog_id];
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);
if (result.is_ok() && G()->close_flag()) {
result = Status::Error(500, "Request aborted");
}
if (result.is_error()) {
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");
reset_to_empty(messages->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;
}
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;
auto content = get_message_content(td_, std::move(message_text), nullptr, sponsor_dialog_id, true, UserId(), &ttl);
if (ttl != 0) {
LOG(ERROR) << "Receive sponsored message with TTL " << ttl;
continue;
}
messages->messages.emplace_back(sponsored_message->random_id_.as_slice().str(), sponsor_dialog_id,
std::move(sponsored_message->start_param_), std::move(content));
}
for (auto &promise : promises) {
promise.set_value(get_sponsored_messages_object(dialog_id, *messages));
}
} }
void SponsoredMessageManager::view_sponsored_message(DialogId dialog_id, const string &message_id, void SponsoredMessageManager::view_sponsored_message(DialogId dialog_id, const string &message_id,

View File

@ -8,11 +8,13 @@
#include "td/telegram/DialogId.h" #include "td/telegram/DialogId.h"
#include "td/telegram/td_api.h" #include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
#include "td/actor/actor.h" #include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h" #include "td/actor/PromiseFuture.h"
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/Status.h"
namespace td { namespace td {
@ -33,8 +35,22 @@ class SponsoredMessageManager final : public Actor {
void view_sponsored_message(DialogId dialog_id, const string &message_id, Promise<Unit> &&promise); void view_sponsored_message(DialogId dialog_id, const string &message_id, Promise<Unit> &&promise);
private: private:
struct SponsoredMessage;
struct DialogSponsoredMessages;
void tear_down() final; void tear_down() final;
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::sponsoredMessages> get_sponsored_messages_object(
DialogId dialog_id, const DialogSponsoredMessages &sponsored_messages) const;
void on_get_dialog_sponsored_messages(
DialogId dialog_id, Result<telegram_api::object_ptr<telegram_api::messages_sponsoredMessages>> &&result);
std::unordered_map<DialogId, unique_ptr<DialogSponsoredMessages>, DialogIdHash> dialog_sponsored_messages_;
Td *td_; Td *td_;
ActorShared<> parent_; ActorShared<> parent_;
}; };