Move LoginUrl-related methods to LinkManager.

This commit is contained in:
levlam 2021-05-25 02:39:29 +03:00
parent d946ab9267
commit 62db52a23e
7 changed files with 199 additions and 186 deletions

View File

@ -10,8 +10,8 @@
#include "td/telegram/ConfigShared.h"
#include "td/telegram/Global.h"
#include "td/telegram/JsonValue.h"
#include "td/telegram/LinkManager.h"
#include "td/telegram/logevent/LogEvent.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/net/AuthDataShared.h"
#include "td/telegram/net/ConnectionCreator.h"
#include "td/telegram/net/DcId.h"
@ -984,7 +984,7 @@ void ConfigManager::get_external_link_info(string &&link, Promise<td_api::object
if (!td::contains(autologin_domains_, r_url.ok().host_)) {
if (td::contains(url_auth_domains_, r_url.ok().host_)) {
send_closure(G()->messages_manager(), &MessagesManager::get_link_login_url_info, link, std::move(promise));
send_closure(G()->link_manager(), &LinkManager::get_link_login_url_info, link, std::move(promise));
return;
}
return promise.set_value(std::move(default_result));

View File

@ -40,6 +40,7 @@ class FileManager;
class FileReferenceManager;
class GroupCallManager;
class LanguagePackManager;
class LinkManager;
class MessagesManager;
class MtprotoHeader;
class NetQueryDispatcher;
@ -230,6 +231,13 @@ class Global : public ActorContext {
language_pack_manager_ = language_pack_manager;
}
ActorId<LinkManager> link_manager() const {
return link_manager_;
}
void set_link_manager(ActorId<LinkManager> link_manager) {
link_manager_ = link_manager;
}
ActorId<MessagesManager> messages_manager() const {
return messages_manager_;
}
@ -395,6 +403,7 @@ class Global : public ActorContext {
ActorId<FileReferenceManager> file_reference_manager_;
ActorId<GroupCallManager> group_call_manager_;
ActorId<LanguagePackManager> language_pack_manager_;
ActorId<LinkManager> link_manager_;
ActorId<MessagesManager> messages_manager_;
ActorId<NotificationManager> notification_manager_;
ActorId<PasswordManager> password_manager_;

View File

@ -6,10 +6,146 @@
//
#include "td/telegram/LinkManager.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/Global.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h"
#include "td/utils/buffer.h"
#include "td/utils/logging.h"
namespace td {
class RequestUrlAuthQuery : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::LoginUrlInfo>> promise_;
string url_;
DialogId dialog_id_;
public:
explicit RequestUrlAuthQuery(Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise)
: promise_(std::move(promise)) {
}
void send(string url, DialogId dialog_id, MessageId message_id, int32 button_id) {
url_ = std::move(url);
int32 flags = 0;
tl_object_ptr<telegram_api::InputPeer> input_peer;
if (dialog_id.is_valid()) {
dialog_id_ = dialog_id;
input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Read);
CHECK(input_peer != nullptr);
flags |= telegram_api::messages_requestUrlAuth::PEER_MASK;
} else {
flags |= telegram_api::messages_requestUrlAuth::URL_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::messages_requestUrlAuth(
flags, std::move(input_peer), message_id.get_server_message_id().get(), button_id, url_)));
}
void on_result(uint64 id, BufferSlice packet) override {
auto result_ptr = fetch_result<telegram_api::messages_requestUrlAuth>(packet);
if (result_ptr.is_error()) {
return on_error(id, result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
LOG(INFO) << "Receive " << to_string(result);
switch (result->get_id()) {
case telegram_api::urlAuthResultRequest::ID: {
auto request = telegram_api::move_object_as<telegram_api::urlAuthResultRequest>(result);
UserId bot_user_id = ContactsManager::get_user_id(request->bot_);
if (!bot_user_id.is_valid()) {
return on_error(id, Status::Error(500, "Receive invalid bot_user_id"));
}
td->contacts_manager_->on_get_user(std::move(request->bot_), "RequestUrlAuthQuery");
bool request_write_access =
(request->flags_ & telegram_api::urlAuthResultRequest::REQUEST_WRITE_ACCESS_MASK) != 0;
promise_.set_value(td_api::make_object<td_api::loginUrlInfoRequestConfirmation>(
url_, request->domain_, td->contacts_manager_->get_user_id_object(bot_user_id, "RequestUrlAuthQuery"),
request_write_access));
break;
}
case telegram_api::urlAuthResultAccepted::ID: {
auto accepted = telegram_api::move_object_as<telegram_api::urlAuthResultAccepted>(result);
promise_.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(accepted->url_, true));
break;
}
case telegram_api::urlAuthResultDefault::ID:
promise_.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(url_, false));
break;
}
}
void on_error(uint64 id, Status status) override {
if (!dialog_id_.is_valid() ||
!td->messages_manager_->on_get_dialog_error(dialog_id_, status, "RequestUrlAuthQuery")) {
LOG(INFO) << "RequestUrlAuthQuery returned " << status;
}
promise_.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(url_, false));
}
};
class AcceptUrlAuthQuery : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::httpUrl>> promise_;
string url_;
DialogId dialog_id_;
public:
explicit AcceptUrlAuthQuery(Promise<td_api::object_ptr<td_api::httpUrl>> &&promise) : promise_(std::move(promise)) {
}
void send(string url, DialogId dialog_id, MessageId message_id, int32 button_id, bool allow_write_access) {
url_ = std::move(url);
int32 flags = 0;
tl_object_ptr<telegram_api::InputPeer> input_peer;
if (dialog_id.is_valid()) {
dialog_id_ = dialog_id;
input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Read);
CHECK(input_peer != nullptr);
flags |= telegram_api::messages_acceptUrlAuth::PEER_MASK;
} else {
flags |= telegram_api::messages_acceptUrlAuth::URL_MASK;
}
if (allow_write_access) {
flags |= telegram_api::messages_acceptUrlAuth::WRITE_ALLOWED_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::messages_acceptUrlAuth(
flags, false /*ignored*/, std::move(input_peer), message_id.get_server_message_id().get(), button_id, url_)));
}
void on_result(uint64 id, BufferSlice packet) override {
auto result_ptr = fetch_result<telegram_api::messages_acceptUrlAuth>(packet);
if (result_ptr.is_error()) {
return on_error(id, result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
LOG(INFO) << "Receive " << to_string(result);
switch (result->get_id()) {
case telegram_api::urlAuthResultRequest::ID:
LOG(ERROR) << "Receive unexpected " << to_string(result);
return on_error(id, Status::Error(500, "Receive unexpected urlAuthResultRequest"));
case telegram_api::urlAuthResultAccepted::ID: {
auto accepted = telegram_api::move_object_as<telegram_api::urlAuthResultAccepted>(result);
promise_.set_value(td_api::make_object<td_api::httpUrl>(accepted->url_));
break;
}
case telegram_api::urlAuthResultDefault::ID:
promise_.set_value(td_api::make_object<td_api::httpUrl>(url_));
break;
}
}
void on_error(uint64 id, Status status) override {
if (!dialog_id_.is_valid() ||
!td->messages_manager_->on_get_dialog_error(dialog_id_, status, "AcceptUrlAuthQuery")) {
LOG(INFO) << "AcceptUrlAuthQuery returned " << status;
}
promise_.set_error(std::move(status));
}
};
LinkManager::LinkManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
}
@ -19,4 +155,32 @@ void LinkManager::tear_down() {
parent_.reset();
}
void LinkManager::get_login_url_info(DialogId dialog_id, MessageId message_id, int32 button_id,
Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise) {
TRY_RESULT_PROMISE(promise, url, td_->messages_manager_->get_login_button_url(dialog_id, message_id, button_id));
td_->create_handler<RequestUrlAuthQuery>(std::move(promise))->send(std::move(url), dialog_id, message_id, button_id);
}
void LinkManager::get_login_url(DialogId dialog_id, MessageId message_id, int32 button_id, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise) {
TRY_RESULT_PROMISE(promise, url, td_->messages_manager_->get_login_button_url(dialog_id, message_id, button_id));
td_->create_handler<AcceptUrlAuthQuery>(std::move(promise))
->send(std::move(url), dialog_id, message_id, button_id, allow_write_access);
}
void LinkManager::get_link_login_url_info(const string &url,
Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise) {
if (G()->close_flag()) {
return promise.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(url, false));
}
td_->create_handler<RequestUrlAuthQuery>(std::move(promise))->send(url, DialogId(), MessageId(), 0);
}
void LinkManager::get_link_login_url(const string &url, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise) {
td_->create_handler<AcceptUrlAuthQuery>(std::move(promise))
->send(url, DialogId(), MessageId(), 0, allow_write_access);
}
} // namespace td

View File

@ -6,7 +6,12 @@
//
#pragma once
#include "td/telegram/DialogId.h"
#include "td/telegram/MessageId.h"
#include "td/telegram/td_api.h"
#include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h"
#include "td/utils/common.h"
@ -24,6 +29,17 @@ class LinkManager : public Actor {
LinkManager &operator=(LinkManager &&) = delete;
~LinkManager() override;
void get_login_url_info(DialogId dialog_id, MessageId message_id, int32 button_id,
Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise);
void get_login_url(DialogId dialog_id, MessageId message_id, int32 button_id, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise);
void get_link_login_url_info(const string &url, Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise);
void get_link_login_url(const string &url, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise);
private:
void tear_down() override;

View File

@ -4604,135 +4604,6 @@ class GetStatsUrlQuery : public Td::ResultHandler {
}
};
class RequestUrlAuthQuery : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::LoginUrlInfo>> promise_;
string url_;
DialogId dialog_id_;
public:
explicit RequestUrlAuthQuery(Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise)
: promise_(std::move(promise)) {
}
void send(string url, DialogId dialog_id, MessageId message_id, int32 button_id) {
url_ = std::move(url);
int32 flags = 0;
tl_object_ptr<telegram_api::InputPeer> input_peer;
if (dialog_id.is_valid()) {
dialog_id_ = dialog_id;
input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Read);
CHECK(input_peer != nullptr);
flags |= telegram_api::messages_requestUrlAuth::PEER_MASK;
} else {
flags |= telegram_api::messages_requestUrlAuth::URL_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::messages_requestUrlAuth(
flags, std::move(input_peer), message_id.get_server_message_id().get(), button_id, url_)));
}
void on_result(uint64 id, BufferSlice packet) override {
auto result_ptr = fetch_result<telegram_api::messages_requestUrlAuth>(packet);
if (result_ptr.is_error()) {
return on_error(id, result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
LOG(INFO) << "Receive " << to_string(result);
switch (result->get_id()) {
case telegram_api::urlAuthResultRequest::ID: {
auto request = telegram_api::move_object_as<telegram_api::urlAuthResultRequest>(result);
UserId bot_user_id = ContactsManager::get_user_id(request->bot_);
if (!bot_user_id.is_valid()) {
return on_error(id, Status::Error(500, "Receive invalid bot_user_id"));
}
td->contacts_manager_->on_get_user(std::move(request->bot_), "RequestUrlAuthQuery");
bool request_write_access =
(request->flags_ & telegram_api::urlAuthResultRequest::REQUEST_WRITE_ACCESS_MASK) != 0;
promise_.set_value(td_api::make_object<td_api::loginUrlInfoRequestConfirmation>(
url_, request->domain_, td->contacts_manager_->get_user_id_object(bot_user_id, "RequestUrlAuthQuery"),
request_write_access));
break;
}
case telegram_api::urlAuthResultAccepted::ID: {
auto accepted = telegram_api::move_object_as<telegram_api::urlAuthResultAccepted>(result);
promise_.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(accepted->url_, true));
break;
}
case telegram_api::urlAuthResultDefault::ID:
promise_.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(url_, false));
break;
}
}
void on_error(uint64 id, Status status) override {
if (!dialog_id_.is_valid() ||
!td->messages_manager_->on_get_dialog_error(dialog_id_, status, "RequestUrlAuthQuery")) {
LOG(INFO) << "RequestUrlAuthQuery returned " << status;
}
promise_.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(url_, false));
}
};
class AcceptUrlAuthQuery : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::httpUrl>> promise_;
string url_;
DialogId dialog_id_;
public:
explicit AcceptUrlAuthQuery(Promise<td_api::object_ptr<td_api::httpUrl>> &&promise) : promise_(std::move(promise)) {
}
void send(string url, DialogId dialog_id, MessageId message_id, int32 button_id, bool allow_write_access) {
url_ = std::move(url);
int32 flags = 0;
tl_object_ptr<telegram_api::InputPeer> input_peer;
if (dialog_id.is_valid()) {
dialog_id_ = dialog_id;
input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Read);
CHECK(input_peer != nullptr);
flags |= telegram_api::messages_acceptUrlAuth::PEER_MASK;
} else {
flags |= telegram_api::messages_acceptUrlAuth::URL_MASK;
}
if (allow_write_access) {
flags |= telegram_api::messages_acceptUrlAuth::WRITE_ALLOWED_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::messages_acceptUrlAuth(
flags, false /*ignored*/, std::move(input_peer), message_id.get_server_message_id().get(), button_id, url_)));
}
void on_result(uint64 id, BufferSlice packet) override {
auto result_ptr = fetch_result<telegram_api::messages_acceptUrlAuth>(packet);
if (result_ptr.is_error()) {
return on_error(id, result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
LOG(INFO) << "Receive " << to_string(result);
switch (result->get_id()) {
case telegram_api::urlAuthResultRequest::ID:
LOG(ERROR) << "Receive unexpected " << to_string(result);
return on_error(id, Status::Error(500, "Receive unexpected urlAuthResultRequest"));
case telegram_api::urlAuthResultAccepted::ID: {
auto accepted = telegram_api::move_object_as<telegram_api::urlAuthResultAccepted>(result);
promise_.set_value(td_api::make_object<td_api::httpUrl>(accepted->url_));
break;
}
case telegram_api::urlAuthResultDefault::ID:
promise_.set_value(td_api::make_object<td_api::httpUrl>(url_));
break;
}
}
void on_error(uint64 id, Status status) override {
if (!dialog_id_.is_valid() ||
!td->messages_manager_->on_get_dialog_error(dialog_id_, status, "AcceptUrlAuthQuery")) {
LOG(INFO) << "AcceptUrlAuthQuery returned " << status;
}
promise_.set_error(std::move(status));
}
};
class GetChannelDifferenceQuery : public Td::ResultHandler {
DialogId dialog_id_;
int32 pts_;
@ -8626,43 +8497,6 @@ Result<string> MessagesManager::get_login_button_url(DialogId dialog_id, Message
return Status::Error(5, "Button not found");
}
void MessagesManager::get_login_url_info(DialogId dialog_id, MessageId message_id, int32 button_id,
Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise) {
auto r_url = get_login_button_url(dialog_id, message_id, button_id);
if (r_url.is_error()) {
return promise.set_error(r_url.move_as_error());
}
td_->create_handler<RequestUrlAuthQuery>(std::move(promise))
->send(r_url.move_as_ok(), dialog_id, message_id, button_id);
}
void MessagesManager::get_login_url(DialogId dialog_id, MessageId message_id, int32 button_id, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise) {
auto r_url = get_login_button_url(dialog_id, message_id, button_id);
if (r_url.is_error()) {
return promise.set_error(r_url.move_as_error());
}
td_->create_handler<AcceptUrlAuthQuery>(std::move(promise))
->send(r_url.move_as_ok(), dialog_id, message_id, button_id, allow_write_access);
}
void MessagesManager::get_link_login_url_info(const string &url,
Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise) {
if (G()->close_flag()) {
return promise.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(url, false));
}
td_->create_handler<RequestUrlAuthQuery>(std::move(promise))->send(url, DialogId(), MessageId(), 0);
}
void MessagesManager::get_link_login_url(const string &url, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise) {
td_->create_handler<AcceptUrlAuthQuery>(std::move(promise))
->send(url, DialogId(), MessageId(), 0, allow_write_access);
}
void MessagesManager::load_secret_thumbnail(FileId thumbnail_file_id) {
class Callback : public FileManager::DownloadCallback {
public:

View File

@ -796,17 +796,6 @@ class MessagesManager : public Actor {
void get_dialog_statistics_url(DialogId dialog_id, const string &parameters, bool is_dark,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise);
void get_login_url_info(DialogId dialog_id, MessageId message_id, int32 button_id,
Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise);
void get_login_url(DialogId dialog_id, MessageId message_id, int32 button_id, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise);
void get_link_login_url_info(const string &url, Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise);
void get_link_login_url(const string &url, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise);
void on_authorization_success();
void before_get_difference();
@ -911,6 +900,8 @@ class MessagesManager : public Actor {
void stop_poll(FullMessageId full_message_id, td_api::object_ptr<td_api::ReplyMarkup> &&reply_markup,
Promise<Unit> &&promise);
Result<string> get_login_button_url(DialogId dialog_id, MessageId message_id, int32 button_id);
Result<ServerMessageId> get_invoice_message_id(FullMessageId full_message_id);
Result<ServerMessageId> get_payment_successful_message_id(FullMessageId full_message_id);
@ -2964,8 +2955,6 @@ class MessagesManager : public Actor {
void suffix_load_till_date(Dialog *d, int32 date, Promise<> promise);
void suffix_load_till_message_id(Dialog *d, MessageId message_id, Promise<> promise);
Result<string> get_login_button_url(DialogId dialog_id, MessageId message_id, int32 button_id);
bool is_broadcast_channel(DialogId dialog_id) const;
bool is_deleted_secret_chat(const Dialog *d) const;

View File

@ -4445,6 +4445,7 @@ void Td::init_managers() {
inline_queries_manager_actor_ = register_actor("InlineQueriesManager", inline_queries_manager_.get());
link_manager_ = make_unique<LinkManager>(this, create_reference());
link_manager_actor_ = register_actor("LinkManager", link_manager_.get());
G()->set_link_manager(link_manager_actor_.get());
messages_manager_ = make_unique<MessagesManager>(this, create_reference());
messages_manager_actor_ = register_actor("MessagesManager", messages_manager_.get());
G()->set_messages_manager(messages_manager_actor_.get());
@ -5497,7 +5498,7 @@ void Td::on_request(uint64 id, td_api::getExternalLink &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.link_);
CREATE_REQUEST_PROMISE();
messages_manager_->get_link_login_url(request.link_, request.allow_write_access_, std::move(promise));
link_manager_->get_link_login_url(request.link_, request.allow_write_access_, std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getChatHistory &request) {
@ -7719,15 +7720,15 @@ void Td::on_request(uint64 id, const td_api::hideSuggestedAction &request) {
void Td::on_request(uint64 id, const td_api::getLoginUrlInfo &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
messages_manager_->get_login_url_info(DialogId(request.chat_id_), MessageId(request.message_id_), request.button_id_,
std::move(promise));
link_manager_->get_login_url_info(DialogId(request.chat_id_), MessageId(request.message_id_), request.button_id_,
std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getLoginUrl &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
messages_manager_->get_login_url(DialogId(request.chat_id_), MessageId(request.message_id_), request.button_id_,
request.allow_write_access_, std::move(promise));
link_manager_->get_login_url(DialogId(request.chat_id_), MessageId(request.message_id_), request.button_id_,
request.allow_write_access_, std::move(promise));
}
void Td::on_request(uint64 id, td_api::getInlineQueryResults &request) {