intro_phone.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "intro/intro_phone.h"
  8. #include "lang/lang_keys.h"
  9. #include "intro/intro_code.h"
  10. #include "intro/intro_qr.h"
  11. #include "styles/style_intro.h"
  12. #include "ui/widgets/buttons.h"
  13. #include "ui/widgets/labels.h"
  14. #include "ui/wrap/fade_wrap.h"
  15. #include "ui/widgets/fields/special_fields.h"
  16. #include "main/main_account.h"
  17. #include "main/main_domain.h"
  18. #include "main/main_app_config.h"
  19. #include "main/main_session.h"
  20. #include "data/data_user.h"
  21. #include "ui/boxes/confirm_box.h"
  22. #include "boxes/abstract_box.h"
  23. #include "boxes/phone_banned_box.h"
  24. #include "core/application.h"
  25. #include "window/window_controller.h"
  26. #include "window/window_session_controller.h"
  27. #include "countries/countries_instance.h" // Countries::Groups
  28. namespace Intro {
  29. namespace details {
  30. namespace {
  31. [[nodiscard]] bool AllowPhoneAttempt(const QString &phone) {
  32. const auto digits = ranges::count_if(
  33. phone,
  34. [](QChar ch) { return ch.isNumber(); });
  35. return (digits > 1);
  36. }
  37. [[nodiscard]] QString DigitsOnly(QString value) {
  38. static const auto RegExp = QRegularExpression("[^0-9]");
  39. return value.replace(RegExp, QString());
  40. }
  41. } // namespace
  42. PhoneWidget::PhoneWidget(
  43. QWidget *parent,
  44. not_null<Main::Account*> account,
  45. not_null<Data*> data)
  46. : Step(parent, account, data)
  47. , _country(
  48. this,
  49. getData()->controller->uiShow(),
  50. st::introCountry)
  51. , _code(this, st::introCountryCode)
  52. , _phone(
  53. this,
  54. st::introPhone,
  55. [](const QString &s) { return Countries::Groups(s); })
  56. , _checkRequestTimer([=] { checkRequest(); }) {
  57. _phone->frontBackspaceEvent(
  58. ) | rpl::start_with_next([=](not_null<QKeyEvent*> e) {
  59. _code->startErasing(e);
  60. }, _code->lifetime());
  61. _country->codeChanged(
  62. ) | rpl::start_with_next([=](const QString &code) {
  63. _code->codeSelected(code);
  64. _phone->chooseCode(code);
  65. }, _country->lifetime());
  66. _code->codeChanged(
  67. ) | rpl::start_with_next([=](const QString &code) {
  68. _country->onChooseCode(code);
  69. _phone->chooseCode(code);
  70. }, _code->lifetime());
  71. _code->addedToNumber(
  72. ) | rpl::start_with_next([=](const QString &added) {
  73. _phone->addedToNumber(added);
  74. }, _phone->lifetime());
  75. connect(_phone, &Ui::PhonePartInput::changed, [=] { phoneChanged(); });
  76. connect(_code, &Ui::CountryCodeInput::changed, [=] { phoneChanged(); });
  77. setTitleText(tr::lng_phone_title());
  78. setDescriptionText(tr::lng_phone_desc());
  79. getData()->updated.events(
  80. ) | rpl::start_with_next([=] {
  81. countryChanged();
  82. }, lifetime());
  83. setErrorCentered(true);
  84. setupQrLogin();
  85. if (!_country->chooseCountry(getData()->country)) {
  86. _country->chooseCountry(u"US"_q);
  87. }
  88. _changed = false;
  89. }
  90. void PhoneWidget::setupQrLogin() {
  91. const auto qrLogin = Ui::CreateChild<Ui::LinkButton>(
  92. this,
  93. tr::lng_phone_to_qr(tr::now));
  94. qrLogin->show();
  95. DEBUG_LOG(("PhoneWidget.qrLogin link created and shown."));
  96. rpl::combine(
  97. sizeValue(),
  98. qrLogin->widthValue()
  99. ) | rpl::start_with_next([=](QSize size, int qrLoginWidth) {
  100. qrLogin->moveToLeft(
  101. (size.width() - qrLoginWidth) / 2,
  102. contentTop() + st::introQrLoginLinkTop);
  103. }, qrLogin->lifetime());
  104. qrLogin->setClickedCallback([=] {
  105. goReplace<QrWidget>(Animate::Forward);
  106. });
  107. }
  108. void PhoneWidget::resizeEvent(QResizeEvent *e) {
  109. Step::resizeEvent(e);
  110. _country->moveToLeft(contentLeft(), contentTop() + st::introStepFieldTop);
  111. auto phoneTop = _country->y() + _country->height() + st::introPhoneTop;
  112. _code->moveToLeft(contentLeft(), phoneTop);
  113. _phone->moveToLeft(contentLeft() + _country->width() - st::introPhone.width, phoneTop);
  114. }
  115. void PhoneWidget::showPhoneError(rpl::producer<QString> text) {
  116. _phone->showError();
  117. showError(std::move(text));
  118. }
  119. void PhoneWidget::hidePhoneError() {
  120. hideError();
  121. }
  122. void PhoneWidget::countryChanged() {
  123. if (!_changed) {
  124. selectCountry(getData()->country);
  125. }
  126. }
  127. void PhoneWidget::phoneChanged() {
  128. _changed = true;
  129. hidePhoneError();
  130. }
  131. void PhoneWidget::submit() {
  132. if (_sentRequest || isHidden()) {
  133. return;
  134. }
  135. {
  136. const auto hasCodeButWaitingPhone = _code->hasFocus()
  137. && (_code->getLastText().size() > 1)
  138. && _phone->getLastText().isEmpty();
  139. if (hasCodeButWaitingPhone) {
  140. _phone->hideError();
  141. _phone->setFocus();
  142. return;
  143. }
  144. }
  145. const auto phone = fullNumber();
  146. if (!AllowPhoneAttempt(phone)) {
  147. showPhoneError(tr::lng_bad_phone());
  148. _phone->setFocus();
  149. return;
  150. }
  151. cancelNearestDcRequest();
  152. // Check if such account is authorized already.
  153. const auto phoneDigits = DigitsOnly(phone);
  154. for (const auto &[index, existing] : Core::App().domain().accounts()) {
  155. const auto raw = existing.get();
  156. if (const auto session = raw->maybeSession()) {
  157. if (raw->mtp().environment() == account().mtp().environment()
  158. && DigitsOnly(session->user()->phone()) == phoneDigits) {
  159. crl::on_main(raw, [=] {
  160. Core::App().domain().activate(raw);
  161. });
  162. return;
  163. }
  164. }
  165. }
  166. hidePhoneError();
  167. _checkRequestTimer.callEach(1000);
  168. _sentPhone = phone;
  169. api().instance().setUserPhone(_sentPhone);
  170. _sentRequest = api().request(MTPauth_SendCode(
  171. MTP_string(_sentPhone),
  172. MTP_int(ApiId),
  173. MTP_string(ApiHash),
  174. MTP_codeSettings(
  175. MTP_flags(0),
  176. MTPVector<MTPbytes>(),
  177. MTPstring(),
  178. MTPBool())
  179. )).done([=](const MTPauth_SentCode &result) {
  180. phoneSubmitDone(result);
  181. }).fail([=](const MTP::Error &error) {
  182. phoneSubmitFail(error);
  183. }).handleFloodErrors().send();
  184. }
  185. void PhoneWidget::stopCheck() {
  186. _checkRequestTimer.cancel();
  187. }
  188. void PhoneWidget::checkRequest() {
  189. auto status = api().instance().state(_sentRequest);
  190. if (status < 0) {
  191. auto leftms = -status;
  192. if (leftms >= 1000) {
  193. api().request(base::take(_sentRequest)).cancel();
  194. }
  195. }
  196. if (!_sentRequest && status == MTP::RequestSent) {
  197. stopCheck();
  198. }
  199. }
  200. void PhoneWidget::phoneSubmitDone(const MTPauth_SentCode &result) {
  201. stopCheck();
  202. _sentRequest = 0;
  203. result.match([&](const MTPDauth_sentCode &data) {
  204. fillSentCodeData(data);
  205. getData()->phone = DigitsOnly(_sentPhone);
  206. getData()->phoneHash = qba(data.vphone_code_hash());
  207. const auto next = data.vnext_type();
  208. if (next && next->type() == mtpc_auth_codeTypeCall) {
  209. getData()->callStatus = CallStatus::Waiting;
  210. getData()->callTimeout = data.vtimeout().value_or(60);
  211. } else {
  212. getData()->callStatus = CallStatus::Disabled;
  213. getData()->callTimeout = 0;
  214. }
  215. goNext<CodeWidget>();
  216. }, [&](const MTPDauth_sentCodeSuccess &data) {
  217. finish(data.vauthorization());
  218. });
  219. }
  220. void PhoneWidget::phoneSubmitFail(const MTP::Error &error) {
  221. if (MTP::IsFloodError(error)) {
  222. stopCheck();
  223. _sentRequest = 0;
  224. showPhoneError(tr::lng_flood_error());
  225. return;
  226. }
  227. stopCheck();
  228. _sentRequest = 0;
  229. auto &err = error.type();
  230. if (err == u"PHONE_NUMBER_FLOOD"_q) {
  231. Ui::show(Ui::MakeInformBox(tr::lng_error_phone_flood()));
  232. } else if (err == u"PHONE_NUMBER_INVALID"_q) { // show error
  233. showPhoneError(tr::lng_bad_phone());
  234. } else if (err == u"PHONE_NUMBER_BANNED"_q) {
  235. Ui::ShowPhoneBannedError(getData()->controller, _sentPhone);
  236. } else if (Logs::DebugEnabled()) { // internal server error
  237. showPhoneError(rpl::single(err + ": " + error.description()));
  238. } else {
  239. showPhoneError(rpl::single(Lang::Hard::ServerError()));
  240. }
  241. }
  242. QString PhoneWidget::fullNumber() const {
  243. return _code->getLastText() + _phone->getLastText();
  244. }
  245. void PhoneWidget::selectCountry(const QString &country) {
  246. _country->chooseCountry(country);
  247. }
  248. void PhoneWidget::setInnerFocus() {
  249. _phone->setFocusFast();
  250. }
  251. void PhoneWidget::activate() {
  252. Step::activate();
  253. showChildren();
  254. setInnerFocus();
  255. }
  256. void PhoneWidget::finished() {
  257. Step::finished();
  258. _checkRequestTimer.cancel();
  259. apiClear();
  260. cancelled();
  261. }
  262. void PhoneWidget::cancelled() {
  263. api().request(base::take(_sentRequest)).cancel();
  264. }
  265. } // namespace details
  266. } // namespace Intro