diff --git a/td/telegram/AuthManager.cpp b/td/telegram/AuthManager.cpp index cf5167fe5..1f02f50e9 100644 --- a/td/telegram/AuthManager.cpp +++ b/td/telegram/AuthManager.cpp @@ -192,32 +192,33 @@ PhoneNumberManager::PhoneNumberManager(PhoneNumberManager::Type type, ActorShare : type_(type), parent_(std::move(parent)) { } +template +void PhoneNumberManager::process_send_code_result(uint64 query_id, T r_send_code) { + if (r_send_code.is_error()) { + return on_query_error(query_id, r_send_code.move_as_error()); + } + + on_new_query(query_id); + + start_net_query(NetQueryType::SendCode, G()->net_query_creator().create(create_storer(r_send_code.move_as_ok()))); +} + void PhoneNumberManager::set_phone_number(uint64 query_id, string phone_number, bool allow_flash_call, bool is_current_phone_number) { if (phone_number.empty()) { return on_query_error(query_id, Status::Error(8, "Phone number can't be empty")); } - auto process_send_code = [&](auto r_send_code) { - if (r_send_code.is_error()) { - return on_query_error(query_id, r_send_code.move_as_error()); - } - - on_new_query(query_id); - - start_net_query(NetQueryType::SendCode, G()->net_query_creator().create(create_storer(r_send_code.move_as_ok()))); - }; - switch (type_) { case Type::ChangePhone: - return process_send_code( - send_code_helper_.send_change_phone_code(phone_number, allow_flash_call, is_current_phone_number)); + return process_send_code_result( + query_id, send_code_helper_.send_change_phone_code(phone_number, allow_flash_call, is_current_phone_number)); case Type::VerifyPhone: - return process_send_code( - send_code_helper_.send_verify_phone_code(phone_number, allow_flash_call, is_current_phone_number)); + return process_send_code_result( + query_id, send_code_helper_.send_verify_phone_code(phone_number, allow_flash_call, is_current_phone_number)); case Type::ConfirmPhone: - return process_send_code( - send_code_helper_.send_confirm_phone_code(phone_number, allow_flash_call, is_current_phone_number)); + return process_send_code_result( + query_id, send_code_helper_.send_confirm_phone_code(phone_number, allow_flash_call, is_current_phone_number)); default: UNREACHABLE(); } @@ -240,25 +241,28 @@ void PhoneNumberManager::resend_authentication_code(uint64 query_id) { NetQuery::Type::Common, NetQuery::AuthFlag::Off)); } +template +void PhoneNumberManager::send_new_check_code_query(const T &query) { + start_net_query(NetQueryType::CheckCode, G()->net_query_creator().create(create_storer(query))); +} + void PhoneNumberManager::check_code(uint64 query_id, string code) { if (state_ != State::WaitCode) { return on_query_error(query_id, Status::Error(8, "checkAuthenticationCode unexpected")); } on_new_query(query_id); - auto send_new_query = [&](auto q) { - start_net_query(NetQueryType::CheckCode, G()->net_query_creator().create(create_storer(q))); - }; switch (type_) { case Type::ChangePhone: - return send_new_query(telegram_api::account_changePhone(send_code_helper_.phone_number().str(), - send_code_helper_.phone_code_hash().str(), code)); + return send_new_check_code_query(telegram_api::account_changePhone( + send_code_helper_.phone_number().str(), send_code_helper_.phone_code_hash().str(), code)); case Type::ConfirmPhone: - return send_new_query(telegram_api::account_confirmPhone(send_code_helper_.phone_code_hash().str(), code)); + return send_new_check_code_query( + telegram_api::account_confirmPhone(send_code_helper_.phone_code_hash().str(), code)); case Type::VerifyPhone: - return send_new_query(telegram_api::account_verifyPhone(send_code_helper_.phone_number().str(), - send_code_helper_.phone_code_hash().str(), code)); + return send_new_check_code_query(telegram_api::account_verifyPhone( + send_code_helper_.phone_number().str(), send_code_helper_.phone_code_hash().str(), code)); default: UNREACHABLE(); } @@ -303,22 +307,23 @@ void PhoneNumberManager::start_net_query(NetQueryType net_query_type, NetQueryPt G()->net_query_dispatcher().dispatch_with_callback(std::move(net_query), actor_shared(this)); } -void PhoneNumberManager::on_check_code_result(NetQueryPtr &result) { - auto process_result = [&](auto result) { - if (result.is_error()) { - return on_query_error(result.move_as_error()); - } - state_ = State::Ok; - on_query_ok(); - }; +template +void PhoneNumberManager::process_check_code_result(T result) { + if (result.is_error()) { + return on_query_error(result.move_as_error()); + } + state_ = State::Ok; + on_query_ok(); +} +void PhoneNumberManager::on_check_code_result(NetQueryPtr &result) { switch (type_) { case Type::ChangePhone: - return process_result(fetch_result(result->ok())); + return process_check_code_result(fetch_result(result->ok())); case Type::VerifyPhone: - return process_result(fetch_result(result->ok())); + return process_check_code_result(fetch_result(result->ok())); case Type::ConfirmPhone: - return process_result(fetch_result(result->ok())); + return process_check_code_result(fetch_result(result->ok())); default: UNREACHABLE(); } diff --git a/td/telegram/AuthManager.h b/td/telegram/AuthManager.h index fdd53703a..1a436403b 100644 --- a/td/telegram/AuthManager.h +++ b/td/telegram/AuthManager.h @@ -123,6 +123,15 @@ class PhoneNumberManager : public NetActor { void on_query_ok(); void start_net_query(NetQueryType net_query_type, NetQueryPtr net_query); + template + void process_send_code_result(uint64 query_id, T r_send_code); + + template + void send_new_check_code_query(const T &query); + + template + void process_check_code_result(T result); + void on_check_code_result(NetQueryPtr &result); void on_send_code_result(NetQueryPtr &result); void on_result(NetQueryPtr result) override;