2021-10-18 19:49:42 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "api/api_confirm_phone.h"
|
|
|
|
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "lang/lang_keys.h"
|
2022-12-28 23:33:57 +00:00
|
|
|
#include "main/main_account.h"
|
2021-10-18 19:49:42 +00:00
|
|
|
#include "main/main_session.h"
|
2021-10-18 22:43:50 +00:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
|
|
|
#include "ui/boxes/confirm_phone_box.h"
|
2021-10-18 19:49:42 +00:00
|
|
|
#include "ui/text/format_values.h" // Ui::FormatPhone
|
|
|
|
#include "window/window_session_controller.h"
|
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
|
|
|
|
ConfirmPhone::ConfirmPhone(not_null<ApiWrap*> api)
|
|
|
|
: _api(&api->instance()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfirmPhone::resolve(
|
|
|
|
not_null<Window::SessionController*> controller,
|
|
|
|
const QString &phone,
|
|
|
|
const QString &hash) {
|
|
|
|
if (_sendRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_sendRequestId = _api.request(MTPaccount_SendConfirmPhoneCode(
|
|
|
|
MTP_string(hash),
|
2023-01-10 13:10:34 +00:00
|
|
|
MTP_codeSettings(
|
|
|
|
MTP_flags(0),
|
|
|
|
MTPVector<MTPbytes>(),
|
|
|
|
MTPstring(),
|
|
|
|
MTPBool())
|
2021-10-18 19:49:42 +00:00
|
|
|
)).done([=](const MTPauth_SentCode &result) {
|
|
|
|
_sendRequestId = 0;
|
|
|
|
|
|
|
|
result.match([&](const MTPDauth_sentCode &data) {
|
2022-08-12 19:08:59 +00:00
|
|
|
const auto bad = [](const char *type) {
|
|
|
|
LOG(("API Error: Should not be '%1'.").arg(type));
|
|
|
|
return 0;
|
|
|
|
};
|
2021-10-18 19:49:42 +00:00
|
|
|
const auto sentCodeLength = data.vtype().match([&](
|
|
|
|
const MTPDauth_sentCodeTypeApp &data) {
|
|
|
|
LOG(("Error: should not be in-app code!"));
|
|
|
|
return 0;
|
|
|
|
}, [&](const MTPDauth_sentCodeTypeSms &data) {
|
|
|
|
return data.vlength().v;
|
2022-11-25 15:57:09 +00:00
|
|
|
}, [&](const MTPDauth_sentCodeTypeFragmentSms &data) {
|
|
|
|
return data.vlength().v;
|
2021-10-18 19:49:42 +00:00
|
|
|
}, [&](const MTPDauth_sentCodeTypeCall &data) {
|
|
|
|
return data.vlength().v;
|
2022-08-12 19:08:59 +00:00
|
|
|
}, [&](const MTPDauth_sentCodeTypeFlashCall &) {
|
|
|
|
return bad("FlashCall");
|
|
|
|
}, [&](const MTPDauth_sentCodeTypeMissedCall &) {
|
|
|
|
return bad("MissedCall");
|
2023-01-10 13:10:34 +00:00
|
|
|
}, [&](const MTPDauth_sentCodeTypeFirebaseSms &) {
|
|
|
|
return bad("FirebaseSms");
|
2022-08-12 19:08:59 +00:00
|
|
|
}, [&](const MTPDauth_sentCodeTypeEmailCode &) {
|
|
|
|
return bad("EmailCode");
|
|
|
|
}, [&](const MTPDauth_sentCodeTypeSetUpEmailRequired &) {
|
|
|
|
return bad("SetUpEmailRequired");
|
2021-10-18 19:49:42 +00:00
|
|
|
});
|
2022-12-28 21:33:01 +00:00
|
|
|
const auto fragmentUrl = data.vtype().match([](
|
|
|
|
const MTPDauth_sentCodeTypeFragmentSms &data) {
|
|
|
|
return qs(data.vurl());
|
|
|
|
}, [](const auto &) { return QString(); });
|
2021-10-18 19:49:42 +00:00
|
|
|
const auto phoneHash = qs(data.vphone_code_hash());
|
|
|
|
const auto timeout = [&]() -> std::optional<int> {
|
|
|
|
if (const auto nextType = data.vnext_type()) {
|
|
|
|
if (nextType->type() == mtpc_auth_codeTypeCall) {
|
|
|
|
return data.vtimeout().value_or(60);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}();
|
2021-10-18 22:43:50 +00:00
|
|
|
auto box = Box<Ui::ConfirmPhoneBox>(
|
2021-10-18 19:49:42 +00:00
|
|
|
phone,
|
|
|
|
sentCodeLength,
|
2022-12-28 21:33:01 +00:00
|
|
|
fragmentUrl,
|
2021-10-18 19:49:42 +00:00
|
|
|
timeout);
|
|
|
|
const auto boxWeak = Ui::MakeWeak(box.data());
|
2022-12-28 23:33:57 +00:00
|
|
|
using LoginCode = rpl::event_stream<QString>;
|
|
|
|
const auto codeHandles = box->lifetime().make_state<LoginCode>();
|
|
|
|
controller->session().account().setHandleLoginCode([=](
|
|
|
|
const QString &code) {
|
|
|
|
codeHandles->fire_copy(code);
|
|
|
|
});
|
2021-10-18 19:49:42 +00:00
|
|
|
box->resendRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_api.request(MTPauth_ResendCode(
|
|
|
|
MTP_string(phone),
|
|
|
|
MTP_string(phoneHash)
|
2023-01-10 13:10:34 +00:00
|
|
|
)).done([=] {
|
2021-10-18 19:49:42 +00:00
|
|
|
if (boxWeak) {
|
|
|
|
boxWeak->callDone();
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}, box->lifetime());
|
2022-12-28 23:33:57 +00:00
|
|
|
rpl::merge(
|
|
|
|
codeHandles->events(),
|
|
|
|
box->checkRequests()
|
2021-10-18 19:49:42 +00:00
|
|
|
) | rpl::start_with_next([=](const QString &code) {
|
|
|
|
if (_checkRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_checkRequestId = _api.request(MTPaccount_ConfirmPhone(
|
|
|
|
MTP_string(phoneHash),
|
|
|
|
MTP_string(code)
|
2021-11-26 20:54:46 +00:00
|
|
|
)).done([=] {
|
2021-10-18 19:49:42 +00:00
|
|
|
_checkRequestId = 0;
|
|
|
|
controller->show(
|
2022-02-27 08:23:20 +00:00
|
|
|
Ui::MakeInformBox(
|
2021-10-18 19:49:42 +00:00
|
|
|
tr::lng_confirm_phone_success(
|
|
|
|
tr::now,
|
|
|
|
lt_phone,
|
|
|
|
Ui::FormatPhone(phone))),
|
|
|
|
Ui::LayerOption::CloseOther);
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
_checkRequestId = 0;
|
|
|
|
if (!boxWeak) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto errorText = MTP::IsFloodError(error)
|
|
|
|
? tr::lng_flood_error(tr::now)
|
|
|
|
: (error.type() == (u"PHONE_CODE_EMPTY"_q)
|
|
|
|
|| error.type() == (u"PHONE_CODE_INVALID"_q))
|
|
|
|
? tr::lng_bad_code(tr::now)
|
|
|
|
: Lang::Hard::ServerError();
|
|
|
|
boxWeak->showServerError(errorText);
|
|
|
|
}).handleFloodErrors().send();
|
|
|
|
}, box->lifetime());
|
2022-12-28 23:33:57 +00:00
|
|
|
box->boxClosing(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
controller->session().account().setHandleLoginCode(nullptr);
|
|
|
|
}, box->lifetime());
|
2021-10-18 19:49:42 +00:00
|
|
|
|
|
|
|
controller->show(std::move(box), Ui::LayerOption::CloseOther);
|
2023-01-10 13:10:34 +00:00
|
|
|
}, [](const MTPDauth_sentCodeSuccess &) {
|
|
|
|
LOG(("API Error: Unexpected auth.sentCodeSuccess "
|
|
|
|
"(Api::ConfirmPhone)."));
|
2021-10-18 19:49:42 +00:00
|
|
|
});
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
_sendRequestId = 0;
|
|
|
|
_checkRequestId = 0;
|
|
|
|
|
|
|
|
const auto errorText = MTP::IsFloodError(error)
|
|
|
|
? tr::lng_flood_error(tr::now)
|
|
|
|
: (error.code() == 400)
|
|
|
|
? tr::lng_confirm_phone_link_invalid(tr::now)
|
|
|
|
: Lang::Hard::ServerError();
|
|
|
|
controller->show(
|
2022-02-27 08:23:20 +00:00
|
|
|
Ui::MakeInformBox(errorText),
|
2021-10-18 19:49:42 +00:00
|
|
|
Ui::LayerOption::CloseOther);
|
|
|
|
}).handleFloodErrors().send();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Api
|