Move website authorization functions to Account.cpp.

This commit is contained in:
levlam 2021-11-16 13:13:06 +03:00
parent c64cc8ad26
commit 36d4de747f
5 changed files with 126 additions and 122 deletions

View File

@ -6,11 +6,13 @@
//
#include "td/telegram/Account.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/DeviceTokenManager.h"
#include "td/telegram/Global.h"
#include "td/telegram/net/NetQueryCreator.h"
#include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/UserId.h"
#include "td/actor/actor.h"
@ -144,7 +146,7 @@ class GetAuthorizationsQuery final : public Td::ResultHandler {
LOG(INFO) << "Receive result for GetAuthorizationsQuery: " << to_string(ptr);
auto results =
make_tl_object<td_api::sessions>(transform(std::move(ptr->authorizations_), convert_authorization_object));
td_api::make_object<td_api::sessions>(transform(std::move(ptr->authorizations_), convert_authorization_object));
std::sort(results->sessions_.begin(), results->sessions_.end(),
[](const td_api::object_ptr<td_api::session> &lhs, const td_api::object_ptr<td_api::session> &rhs) {
if (lhs->is_current_ != rhs->is_current_) {
@ -219,6 +221,108 @@ class ResetAuthorizationsQuery final : public Td::ResultHandler {
}
};
class GetWebAuthorizationsQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::connectedWebsites>> promise_;
public:
explicit GetWebAuthorizationsQuery(Promise<td_api::object_ptr<td_api::connectedWebsites>> &&promise)
: promise_(std::move(promise)) {
}
void send() {
send_query(G()->net_query_creator().create(telegram_api::account_getWebAuthorizations()));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::account_getWebAuthorizations>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for GetWebAuthorizationsQuery: " << to_string(ptr);
td_->contacts_manager_->on_get_users(std::move(ptr->users_), "GetWebAuthorizationsQuery");
auto results = td_api::make_object<td_api::connectedWebsites>();
results->websites_.reserve(ptr->authorizations_.size());
for (auto &authorization : ptr->authorizations_) {
CHECK(authorization != nullptr);
UserId bot_user_id(authorization->bot_id_);
if (!bot_user_id.is_valid()) {
LOG(ERROR) << "Receive invalid bot " << bot_user_id;
bot_user_id = UserId();
}
results->websites_.push_back(td_api::make_object<td_api::connectedWebsite>(
authorization->hash_, authorization->domain_,
td_->contacts_manager_->get_user_id_object(bot_user_id, "GetWebAuthorizationsQuery"), authorization->browser_,
authorization->platform_, authorization->date_created_, authorization->date_active_, authorization->ip_,
authorization->region_));
}
promise_.set_value(std::move(results));
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class ResetWebAuthorizationQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
public:
explicit ResetWebAuthorizationQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(int64 hash) {
send_query(G()->net_query_creator().create(telegram_api::account_resetWebAuthorization(hash)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::account_resetWebAuthorization>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
bool result = result_ptr.move_as_ok();
LOG_IF(WARNING, !result) << "Failed to disconnect website";
promise_.set_value(Unit());
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class ResetWebAuthorizationsQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
public:
explicit ResetWebAuthorizationsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send() {
send_query(G()->net_query_creator().create(telegram_api::account_resetWebAuthorizations()));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::account_resetWebAuthorizations>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
bool result = result_ptr.move_as_ok();
LOG_IF(WARNING, !result) << "Failed to disconnect all websites";
promise_.set_value(Unit());
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
void set_account_ttl(Td *td, int32 account_ttl, Promise<Unit> &&promise) {
td->create_handler<SetAccountTtlQuery>(std::move(promise))->send(account_ttl);
}
@ -252,4 +356,16 @@ void terminate_all_other_sessions(Td *td, Promise<Unit> &&promise) {
td->create_handler<ResetAuthorizationsQuery>(std::move(promise))->send();
}
void get_connected_websites(Td *td, Promise<td_api::object_ptr<td_api::connectedWebsites>> &&promise) {
td->create_handler<GetWebAuthorizationsQuery>(std::move(promise))->send();
}
void disconnect_website(Td *td, int64 website_id, Promise<Unit> &&promise) {
td->create_handler<ResetWebAuthorizationQuery>(std::move(promise))->send(website_id);
}
void disconnect_all_websites(Td *td, Promise<Unit> &&promise) {
td->create_handler<ResetWebAuthorizationsQuery>(std::move(promise))->send();
}
} // namespace td

View File

@ -28,4 +28,10 @@ void terminate_session(Td *td, int64 session_id, Promise<Unit> &&promise);
void terminate_all_other_sessions(Td *td, Promise<Unit> &&promise);
void get_connected_websites(Td *td, Promise<td_api::object_ptr<td_api::connectedWebsites>> &&promise);
void disconnect_website(Td *td, int64 website_id, Promise<Unit> &&promise);
void disconnect_all_websites(Td *td, Promise<Unit> &&promise);
} // namespace td

View File

@ -105,108 +105,6 @@ class DismissSuggestionQuery final : public Td::ResultHandler {
}
};
class GetWebAuthorizationsQuery final : public Td::ResultHandler {
Promise<tl_object_ptr<td_api::connectedWebsites>> promise_;
public:
explicit GetWebAuthorizationsQuery(Promise<tl_object_ptr<td_api::connectedWebsites>> &&promise)
: promise_(std::move(promise)) {
}
void send() {
send_query(G()->net_query_creator().create(telegram_api::account_getWebAuthorizations()));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::account_getWebAuthorizations>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for GetWebAuthorizationsQuery: " << to_string(ptr);
td_->contacts_manager_->on_get_users(std::move(ptr->users_), "GetWebAuthorizationsQuery");
auto results = make_tl_object<td_api::connectedWebsites>();
results->websites_.reserve(ptr->authorizations_.size());
for (auto &authorization : ptr->authorizations_) {
CHECK(authorization != nullptr);
UserId bot_user_id(authorization->bot_id_);
if (!bot_user_id.is_valid()) {
LOG(ERROR) << "Receive invalid bot " << bot_user_id;
bot_user_id = UserId();
}
results->websites_.push_back(make_tl_object<td_api::connectedWebsite>(
authorization->hash_, authorization->domain_,
td_->contacts_manager_->get_user_id_object(bot_user_id, "GetWebAuthorizationsQuery"), authorization->browser_,
authorization->platform_, authorization->date_created_, authorization->date_active_, authorization->ip_,
authorization->region_));
}
promise_.set_value(std::move(results));
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class ResetWebAuthorizationQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
public:
explicit ResetWebAuthorizationQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(int64 hash) {
send_query(G()->net_query_creator().create(telegram_api::account_resetWebAuthorization(hash)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::account_resetWebAuthorization>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
bool result = result_ptr.move_as_ok();
LOG_IF(WARNING, !result) << "Failed to disconnect website";
promise_.set_value(Unit());
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class ResetWebAuthorizationsQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
public:
explicit ResetWebAuthorizationsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send() {
send_query(G()->net_query_creator().create(telegram_api::account_resetWebAuthorizations()));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::account_resetWebAuthorizations>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
bool result = result_ptr.move_as_ok();
LOG_IF(WARNING, !result) << "Failed to disconnect all websites";
promise_.set_value(Unit());
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class GetContactsQuery final : public Td::ResultHandler {
public:
void send(int64 hash) {
@ -5040,18 +4938,6 @@ td_api::object_ptr<td_api::CheckChatUsernameResult> ContactsManager::get_check_c
}
}
void ContactsManager::get_connected_websites(Promise<tl_object_ptr<td_api::connectedWebsites>> &&promise) const {
td_->create_handler<GetWebAuthorizationsQuery>(std::move(promise))->send();
}
void ContactsManager::disconnect_website(int64 website_id, Promise<Unit> &&promise) const {
td_->create_handler<ResetWebAuthorizationQuery>(std::move(promise))->send(website_id);
}
void ContactsManager::disconnect_all_websites(Promise<Unit> &&promise) const {
td_->create_handler<ResetWebAuthorizationsQuery>(std::move(promise))->send();
}
bool ContactsManager::is_valid_username(const string &username) {
if (username.size() < 5 || username.size() > 32) {
return false;

View File

@ -273,10 +273,6 @@ class ContactsManager final : public Actor {
static td_api::object_ptr<td_api::CheckChatUsernameResult> get_check_chat_username_result_object(
CheckDialogUsernameResult result);
void get_connected_websites(Promise<tl_object_ptr<td_api::connectedWebsites>> &&promise) const;
void disconnect_website(int64 website_id, Promise<Unit> &&promise) const;
void disconnect_all_websites(Promise<Unit> &&promise) const;
void add_contact(Contact contact, bool share_phone_number, Promise<Unit> &&promise);
std::pair<vector<UserId>, vector<int32>> import_contacts(const vector<Contact> &contacts, int64 &random_id,

View File

@ -4791,19 +4791,19 @@ void Td::on_request(uint64 id, const td_api::terminateAllOtherSessions &request)
void Td::on_request(uint64 id, const td_api::getConnectedWebsites &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
contacts_manager_->get_connected_websites(std::move(promise));
get_connected_websites(this, std::move(promise));
}
void Td::on_request(uint64 id, const td_api::disconnectWebsite &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
contacts_manager_->disconnect_website(request.website_id_, std::move(promise));
disconnect_website(this, request.website_id_, std::move(promise));
}
void Td::on_request(uint64 id, const td_api::disconnectAllWebsites &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
contacts_manager_->disconnect_all_websites(std::move(promise));
disconnect_all_websites(this, std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getMe &request) {