2021-08-02 19:06:33 +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_cloud_password.h"
|
|
|
|
|
2021-09-15 10:21:45 +00:00
|
|
|
#include "base/random.h"
|
2021-08-02 19:06:33 +00:00
|
|
|
#include "core/core_cloud_password.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
|
|
|
|
// #TODO Add ability to set recovery email separately.
|
|
|
|
|
|
|
|
CloudPassword::CloudPassword(not_null<ApiWrap*> api)
|
|
|
|
: _api(&api->instance()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudPassword::reload() {
|
|
|
|
if (_requestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_requestId = _api.request(MTPaccount_GetPassword(
|
|
|
|
)).done([=](const MTPaccount_Password &result) {
|
|
|
|
_requestId = 0;
|
|
|
|
result.match([&](const MTPDaccount_password &data) {
|
2021-09-15 10:21:45 +00:00
|
|
|
base::RandomAddSeed(bytes::make_span(data.vsecure_random().v));
|
2021-08-02 19:06:33 +00:00
|
|
|
if (_state) {
|
|
|
|
*_state = Core::ParseCloudPasswordState(data);
|
|
|
|
} else {
|
|
|
|
_state = std::make_unique<Core::CloudPasswordState>(
|
|
|
|
Core::ParseCloudPasswordState(data));
|
|
|
|
}
|
|
|
|
_stateChanges.fire_copy(*_state);
|
|
|
|
});
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2021-08-02 19:06:33 +00:00
|
|
|
_requestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudPassword::clearUnconfirmedPassword() {
|
|
|
|
_requestId = _api.request(MTPaccount_CancelPasswordEmail(
|
2021-11-26 20:46:53 +00:00
|
|
|
)).done([=] {
|
2021-08-02 19:06:33 +00:00
|
|
|
_requestId = 0;
|
|
|
|
reload();
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2021-08-02 19:06:33 +00:00
|
|
|
_requestId = 0;
|
|
|
|
reload();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<Core::CloudPasswordState> CloudPassword::state() const {
|
|
|
|
return _state
|
|
|
|
? _stateChanges.events_starting_with_copy(*_state)
|
|
|
|
: (_stateChanges.events() | rpl::type_erased());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto CloudPassword::stateCurrent() const
|
|
|
|
-> std::optional<Core::CloudPasswordState> {
|
|
|
|
return _state
|
|
|
|
? base::make_optional(*_state)
|
|
|
|
: std::nullopt;
|
|
|
|
}
|
|
|
|
|
2021-08-15 10:11:48 +00:00
|
|
|
auto CloudPassword::resetPassword()
|
|
|
|
-> rpl::producer<CloudPassword::ResetRetryDate, QString> {
|
|
|
|
return [=](auto consumer) {
|
|
|
|
_api.request(MTPaccount_ResetPassword(
|
|
|
|
)).done([=](const MTPaccount_ResetPasswordResult &result) {
|
|
|
|
result.match([&](const MTPDaccount_resetPasswordOk &data) {
|
|
|
|
reload();
|
|
|
|
}, [&](const MTPDaccount_resetPasswordRequestedWait &data) {
|
|
|
|
if (!_state) {
|
|
|
|
reload();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto until = data.vuntil_date().v;
|
|
|
|
if (_state->pendingResetDate != until) {
|
|
|
|
_state->pendingResetDate = until;
|
|
|
|
_stateChanges.fire_copy(*_state);
|
|
|
|
}
|
|
|
|
}, [&](const MTPDaccount_resetPasswordFailedWait &data) {
|
|
|
|
consumer.put_next_copy(data.vretry_date().v);
|
|
|
|
});
|
|
|
|
consumer.put_done();
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
consumer.put_error_copy(error.type());
|
|
|
|
}).send();
|
|
|
|
|
|
|
|
return rpl::lifetime();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto CloudPassword::cancelResetPassword()
|
|
|
|
-> rpl::producer<rpl::no_value, QString> {
|
|
|
|
return [=](auto consumer) {
|
|
|
|
_api.request(MTPaccount_DeclinePasswordReset(
|
|
|
|
)).done([=] {
|
|
|
|
reload();
|
|
|
|
consumer.put_done();
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
consumer.put_error_copy(error.type());
|
|
|
|
}).send();
|
|
|
|
|
|
|
|
return rpl::lifetime();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-02 19:06:33 +00:00
|
|
|
} // namespace Api
|