From 7f2d1cdd9d1c978ccc48d7da0b3d0cf3ea03f4b3 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 12 Sep 2023 14:48:42 +0300 Subject: [PATCH] Delete expired unconfirmed sessions after loading them after restart. --- td/telegram/AccountManager.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/td/telegram/AccountManager.cpp b/td/telegram/AccountManager.cpp index 5aaef47d9..28064c595 100644 --- a/td/telegram/AccountManager.cpp +++ b/td/telegram/AccountManager.cpp @@ -664,6 +664,10 @@ class AccountManager::UnconfirmedAuthorization { class AccountManager::UnconfirmedAuthorizations { vector authorizations_; + static int32 get_authorization_autoconfirm_period() { + return narrow_cast(G()->get_option_integer("authorization_autoconfirm_period", 604800)); + } + public: bool is_empty() const { return authorizations_.empty(); @@ -701,6 +705,19 @@ class AccountManager::UnconfirmedAuthorizations { return true; } + bool delete_expired_authorizations() { + auto up_to_date = G()->unix_time() - get_authorization_autoconfirm_period(); + auto it = authorizations_.begin(); + while (it != authorizations_.end() && it->get_date() <= up_to_date) { + ++it; + } + if (it == authorizations_.begin()) { + return false; + } + authorizations_.erase(authorizations_.begin(), it); + return true; + } + td_api::object_ptr get_first_unconfirmed_session_object() const { CHECK(!authorizations_.empty()); return authorizations_[0].get_unconfirmed_session_object(); @@ -728,6 +745,13 @@ void AccountManager::start_up() { G()->td_db()->get_binlog_pmc()->get(get_unconfirmed_authorizations_key()); if (!unconfirmed_authorizations_log_event_string.empty()) { log_event_parse(unconfirmed_authorizations_, unconfirmed_authorizations_log_event_string).ensure(); + CHECK(unconfirmed_authorizations_ != nullptr); + if (unconfirmed_authorizations_->delete_expired_authorizations()) { + save_unconfirmed_authorizations(); + if (unconfirmed_authorizations_->is_empty()) { + unconfirmed_authorizations_ = nullptr; + } + } if (unconfirmed_authorizations_ != nullptr) { send_update_unconfirmed_session(); }