From 47fd88b33f9e01a1f7b07a9d4435137db0ac066c Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 30 Jan 2023 01:29:33 +0300 Subject: [PATCH] Add and use "since_last_open" internal option. --- td/telegram/OptionManager.cpp | 6 +++--- td/telegram/Td.cpp | 20 ++++++++++++++------ td/telegram/TdDb.cpp | 3 +++ td/telegram/TdDb.h | 2 ++ td/telegram/UpdatesManager.cpp | 11 ++++++++++- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/td/telegram/OptionManager.cpp b/td/telegram/OptionManager.cpp index 57a551fe4..81db8707a 100644 --- a/td/telegram/OptionManager.cpp +++ b/td/telegram/OptionManager.cpp @@ -283,9 +283,9 @@ bool OptionManager::is_internal_option(Slice name) { name == "revoke_time_limit" || name == "revoke_pm_time_limit"; case 's': return name == "saved_animations_limit" || name == "saved_gifs_limit_default" || - name == "saved_gifs_limit_premium" || name == "session_count" || name == "stickers_faved_limit_default" || - name == "stickers_faved_limit_premium" || name == "stickers_normal_by_emoji_per_premium_num" || - name == "stickers_premium_by_emoji_num"; + name == "saved_gifs_limit_premium" || name == "session_count" || name == "since_last_open" || + name == "stickers_faved_limit_default" || name == "stickers_faved_limit_premium" || + name == "stickers_normal_by_emoji_per_premium_num" || name == "stickers_premium_by_emoji_num"; case 'v': return name == "video_note_size_max"; case 'w': diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 9de1a781e..4db54c6d6 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3580,14 +3580,22 @@ void Td::init(Result r_opened_database) { } }); - options_.language_pack = G()->get_option_string("localization_target"); - options_.language_code = G()->get_option_string("language_pack_id"); - options_.parameters = G()->get_option_string("connection_parameters"); - options_.tz_offset = static_cast(G()->get_option_integer("utc_time_offset")); - options_.is_emulator = G()->get_option_boolean("is_emulator"); + if (events.since_last_open >= 3600) { + auto old_since_last_open = option_manager_->get_option_integer("since_last_open"); + if (events.since_last_open > old_since_last_open) { + option_manager_->set_option_integer("since_last_open", events.since_last_open); + } + } + + options_.language_pack = option_manager_->get_option_string("localization_target"); + options_.language_code = option_manager_->get_option_string("language_pack_id"); + options_.parameters = option_manager_->get_option_string("connection_parameters"); + options_.tz_offset = static_cast(option_manager_->get_option_integer("utc_time_offset")); + options_.is_emulator = option_manager_->get_option_boolean("is_emulator"); // options_.proxy = Proxy(); G()->set_mtproto_header(make_unique(options_)); - G()->set_store_all_files_in_files_directory(G()->get_option_boolean("store_all_files_in_files_directory")); + G()->set_store_all_files_in_files_directory( + option_manager_->get_option_boolean("store_all_files_in_files_directory")); VLOG(td_init) << "Create NetQueryDispatcher"; auto net_query_dispatcher = make_unique([&] { return create_reference(); }); diff --git a/td/telegram/TdDb.cpp b/td/telegram/TdDb.cpp index 137339951..d9ba160cb 100644 --- a/td/telegram/TdDb.cpp +++ b/td/telegram/TdDb.cpp @@ -63,6 +63,9 @@ Status init_binlog(Binlog &binlog, string path, BinlogKeyValue &binlog_p if (since_last_open >= 86400) { LOG(WARNING) << "Binlog wasn't opened for " << since_last_open << " seconds"; } + if (since_last_open > 0 && since_last_open < 1e12) { + events.since_last_open = static_cast(since_last_open); + } } auto callback = [&](const BinlogEvent &event) { diff --git a/td/telegram/TdDb.h b/td/telegram/TdDb.h index 726dcf190..ee487c0a5 100644 --- a/td/telegram/TdDb.h +++ b/td/telegram/TdDb.h @@ -67,6 +67,8 @@ class TdDb { vector to_messages_manager; vector to_notification_manager; vector to_notification_settings_manager; + + int64 since_last_open = 0; }; static void open(int32 scheduler_id, TdParameters parameters, DbKey key, Promise &&promise); diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index b33eb2123..51f9023c7 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -183,6 +183,10 @@ UpdatesManager::UpdatesManager(Td *td, ActorShared<> parent) : td_(td), parent_( pending_audio_transcription_timeout_.set_callback(on_pending_audio_transcription_timeout_callback); pending_audio_transcription_timeout_.set_callback_data(static_cast(td_)); + + if (td_->option_manager_->get_option_integer("since_last_open") < 3600) { + finished_first_get_difference_ = true; + } } void UpdatesManager::tear_down() { @@ -1463,8 +1467,12 @@ void UpdatesManager::init_state() { return; } + bool drop_state = !G()->parameters().use_file_db && !G()->parameters().use_secret_chats && + td_->auth_manager_->is_bot() && + td_->option_manager_->get_option_integer("since_last_open") >= 2 * 86400; + auto pmc = G()->td_db()->get_binlog_pmc(); - if (G()->ignore_background_updates()) { + if (G()->ignore_background_updates() || drop_state) { // just in case pmc->erase("updates.pts"); pmc->erase("updates.qts"); @@ -1755,6 +1763,7 @@ void UpdatesManager::after_get_difference() { retry_time_ = 1; finished_first_get_difference_ = true; + td_->option_manager_->set_option_empty("since_last_open"); // cancels qts_gap_timeout_ if needed, can apply some updates received during getDifference, // but missed in getDifference