From 3ad9ecc3a8e9108dc99f3014e373a846c3bf5b1e Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 6 Sep 2022 14:53:19 +0300 Subject: [PATCH] Use Promise. --- td/telegram/PasswordManager.cpp | 21 +++++---------------- td/telegram/PasswordManager.h | 8 +++----- td/telegram/Td.cpp | 28 +++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/td/telegram/PasswordManager.cpp b/td/telegram/PasswordManager.cpp index eb0d598af..e27f52c93 100644 --- a/td/telegram/PasswordManager.cpp +++ b/td/telegram/PasswordManager.cpp @@ -420,8 +420,7 @@ void PasswordManager::resend_recovery_email_address_code(Promise promise) })); } -void PasswordManager::send_email_address_verification_code( - string email, Promise> promise) { +void PasswordManager::send_email_address_verification_code(string email, Promise promise) { last_verified_email_address_ = email; auto query = G()->net_query_creator().create(telegram_api::account_sendVerifyEmailCode( make_tl_object(), std::move(email))); @@ -431,16 +430,11 @@ void PasswordManager::send_email_address_verification_code( if (r_result.is_error()) { return promise.set_error(r_result.move_as_error()); } - SentEmailCode sent_code(r_result.move_as_ok()); - if (sent_code.is_empty()) { - return promise.set_error(Status::Error(500, "Receive invalid response")); - } - return promise.set_value(sent_code.get_email_address_authentication_code_info_object()); + return promise.set_value(SentEmailCode(r_result.move_as_ok())); })); } -void PasswordManager::resend_email_address_verification_code( - Promise> promise) { +void PasswordManager::resend_email_address_verification_code(Promise promise) { if (last_verified_email_address_.empty()) { return promise.set_error(Status::Error(400, "No email address verification was sent")); } @@ -464,8 +458,7 @@ void PasswordManager::check_email_address_verification_code(string code, Promise })); } -void PasswordManager::request_password_recovery( - Promise> promise) { +void PasswordManager::request_password_recovery(Promise promise) { // is called only after authorization send_with_promise(G()->net_query_creator().create(telegram_api::auth_requestPasswordRecovery()), PromiseCreator::lambda([promise = std::move(promise)](Result r_query) mutable { @@ -474,11 +467,7 @@ void PasswordManager::request_password_recovery( return promise.set_error(r_result.move_as_error()); } auto result = r_result.move_as_ok(); - SentEmailCode sent_code(std::move(result->email_pattern_), 0); - if (sent_code.is_empty()) { - return promise.set_error(Status::Error(500, "Receive invalid response")); - } - return promise.set_value(sent_code.get_email_address_authentication_code_info_object()); + return promise.set_value(SentEmailCode(std::move(result->email_pattern_), 0)); })); } diff --git a/td/telegram/PasswordManager.h b/td/telegram/PasswordManager.h index 292c6a0c3..8b465bc94 100644 --- a/td/telegram/PasswordManager.h +++ b/td/telegram/PasswordManager.h @@ -75,13 +75,11 @@ class PasswordManager final : public NetQueryCallback { void check_recovery_email_address_code(string code, Promise promise); void resend_recovery_email_address_code(Promise promise); - void send_email_address_verification_code( - string email, Promise> promise); - void resend_email_address_verification_code( - Promise> promise); + void send_email_address_verification_code(string email, Promise promise); + void resend_email_address_verification_code(Promise promise); void check_email_address_verification_code(string code, Promise promise); - void request_password_recovery(Promise> promise); + void request_password_recovery(Promise promise); void check_password_recovery_code(string code, Promise promise); void recover_password(string code, string new_password, string new_hint, Promise promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index d9d6057c1..93c96311e 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -104,6 +104,7 @@ #include "td/telegram/SecretChatsManager.h" #include "td/telegram/SecureManager.h" #include "td/telegram/SecureValue.h" +#include "td/telegram/SentEmailCode.h" #include "td/telegram/SponsoredMessageManager.h" #include "td/telegram/StateManager.h" #include "td/telegram/StickerSetId.h" @@ -4434,7 +4435,14 @@ void Td::on_request(uint64 id, const td_api::resendRecoveryEmailAddressCode &req void Td::on_request(uint64 id, td_api::requestPasswordRecovery &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); - send_closure(password_manager_, &PasswordManager::request_password_recovery, std::move(promise)); + auto query_promise = PromiseCreator::lambda([promise = std::move(promise)](Result result) mutable { + if (result.is_error()) { + promise.set_error(result.move_as_error()); + } else { + promise.set_value(result.ok().get_email_address_authentication_code_info_object()); + } + }); + send_closure(password_manager_, &PasswordManager::request_password_recovery, std::move(query_promise)); } void Td::on_request(uint64 id, td_api::checkPasswordRecoveryCode &request) { @@ -7651,14 +7659,28 @@ void Td::on_request(uint64 id, td_api::sendEmailAddressVerificationCode &request CHECK_IS_USER(); CLEAN_INPUT_STRING(request.email_address_); CREATE_REQUEST_PROMISE(); + auto query_promise = PromiseCreator::lambda([promise = std::move(promise)](Result result) mutable { + if (result.is_error()) { + promise.set_error(result.move_as_error()); + } else { + promise.set_value(result.ok().get_email_address_authentication_code_info_object()); + } + }); send_closure(password_manager_, &PasswordManager::send_email_address_verification_code, - std::move(request.email_address_), std::move(promise)); + std::move(request.email_address_), std::move(query_promise)); } void Td::on_request(uint64 id, const td_api::resendEmailAddressVerificationCode &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); - send_closure(password_manager_, &PasswordManager::resend_email_address_verification_code, std::move(promise)); + auto query_promise = PromiseCreator::lambda([promise = std::move(promise)](Result result) mutable { + if (result.is_error()) { + promise.set_error(result.move_as_error()); + } else { + promise.set_value(result.ok().get_email_address_authentication_code_info_object()); + } + }); + send_closure(password_manager_, &PasswordManager::resend_email_address_verification_code, std::move(query_promise)); } void Td::on_request(uint64 id, td_api::checkEmailAddressVerificationCode &request) {