Skip to content

Commit

Permalink
Move website authorization functions to Account.cpp.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Nov 16, 2021
1 parent c64cc8a commit 36d4de7
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 122 deletions.
118 changes: 117 additions & 1 deletion td/telegram/Account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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_) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions td/telegram/Account.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
114 changes: 0 additions & 114 deletions td/telegram/ContactsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions td/telegram/ContactsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions td/telegram/Td.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 36d4de7

Please sign in to comment.