2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 22:55:11 +00:00

Add ability to reset cloud password in 7 days.

This commit is contained in:
John Preston
2021-07-30 14:32:49 +03:00
parent c100055fac
commit 256546071b
11 changed files with 299 additions and 30 deletions

View File

@@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "settings/settings_common.h"
#include "settings/settings_privacy_controllers.h"
#include "base/timer_rpl.h"
#include "base/unixtime.h"
#include "boxes/peer_list_box.h"
#include "boxes/edit_privacy_box.h"
#include "boxes/passcode_box.h"
@@ -359,6 +360,10 @@ void SetupCloudPassword(
) | rpl::then(rpl::duplicate(
unconfirmed
));
auto resetAt = session->api().passwordState(
) | rpl::map([](const State &state) {
return state.pendingResetDate;
});
const auto label = container->add(
object_ptr<Ui::SlideWrap<Ui::FlatLabel>>(
container,
@@ -464,6 +469,118 @@ void SetupCloudPassword(
_1 && !_2));
disable->entity()->addClickHandler(remove);
auto resetInSeconds = rpl::duplicate(
resetAt
) | rpl::filter([](TimeId time) {
return time != 0;
}) | rpl::map([](TimeId time) {
return rpl::single(
rpl::empty_value()
) | rpl::then(base::timer_each(
999
)) | rpl::map([=] {
const auto now = base::unixtime::now();
return (time - now);
}) | rpl::distinct_until_changed(
) | rpl::take_while([](TimeId left) {
return left > 0;
}) | rpl::then(rpl::single(TimeId(0)));
}) | rpl::flatten_latest(
) | rpl::start_spawning(container->lifetime());
auto resetText = rpl::duplicate(
resetInSeconds
) | rpl::map([](TimeId left) {
return (left > 0);
}) | rpl::distinct_until_changed(
) | rpl::map([](bool waiting) {
return waiting
? tr::lng_cloud_password_reset_in()
: tr::lng_cloud_password_reset_ready();
}) | rpl::flatten_latest();
constexpr auto kMinute = 60;
constexpr auto kHour = 3600;
constexpr auto kDay = 86400;
auto resetLabel = rpl::duplicate(
resetInSeconds
) | rpl::map([](TimeId left) {
return (left >= kDay)
? ((left / kDay) * kDay)
: (left >= kHour)
? ((left / kHour) * kHour)
: (left >= kMinute)
? ((left / kMinute) * kMinute)
: left;
}) | rpl::distinct_until_changed(
) | rpl::map([](TimeId left) {
const auto days = left / kDay;
const auto hours = left / kHour;
const auto minutes = left / kMinute;
return days
? tr::lng_group_call_duration_days(tr::now, lt_count, days)
: hours
? tr::lng_group_call_duration_hours(tr::now, lt_count, hours)
: minutes
? tr::lng_group_call_duration_minutes(tr::now, lt_count, minutes)
: left
? tr::lng_group_call_duration_seconds(tr::now, lt_count, left)
: QString();
});
const auto reset = container->add(
object_ptr<Ui::SlideWrap<Button>>(
container,
object_ptr<Button>(
container,
rpl::duplicate(resetText),
st::settingsButton))
)->setDuration(0);
CreateRightLabel(
reset->entity(),
std::move(resetLabel),
st::settingsButton,
std::move(resetText));
reset->toggleOn(rpl::duplicate(
resetAt
) | rpl::map([](TimeId time) {
return time != 0;
}));
const auto sent = std::make_shared<mtpRequestId>(0);
reset->entity()->addClickHandler([=] {
const auto api = &session->api();
const auto state = api->passwordStateCurrent();
const auto date = state ? state->pendingResetDate : TimeId(0);
if (!date || *sent) {
return;
} else if (base::unixtime::now() >= date) {
*sent = api->request(MTPaccount_ResetPassword(
)).done([=](const MTPaccount_ResetPasswordResult &result) {
*sent = 0;
api->applyPendingReset(result);
}).fail([=](const MTP::Error &error) {
*sent = 0;
}).send();
} else {
const auto cancel = [=] {
Ui::hideLayer();
*sent = api->request(MTPaccount_DeclinePasswordReset(
)).done([=] {
*sent = 0;
api->reloadPasswordState();
}).fail([=](const MTP::Error &error) {
*sent = 0;
}).send();
};
Ui::show(Box<ConfirmBox>(
tr::lng_cloud_password_reset_cancel_sure(tr::now),
tr::lng_box_yes(tr::now),
tr::lng_box_no(tr::now),
cancel));
}
});
const auto abort = container->add(
object_ptr<Ui::SlideWrap<Button>>(
container,