From 74679944afad7239e4f6bb5b0f0b1e1fb5ff4172 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 4 Oct 2023 12:24:58 +0300 Subject: [PATCH] Remove error-prone server_time_cached. --- td/telegram/Application.cpp | 2 +- td/telegram/ContactsManager.cpp | 75 ++++++++++++--------- td/telegram/ContactsManager.h | 4 +- td/telegram/Global.h | 11 +-- td/telegram/InlineQueriesManager.cpp | 4 +- td/telegram/MessageContent.cpp | 2 +- td/telegram/MessagesManager.cpp | 46 ++++++------- td/telegram/MessagesManager.h | 2 +- td/telegram/NotificationManager.cpp | 8 +-- td/telegram/NotificationSettingsManager.cpp | 22 +++--- td/telegram/NotificationSettingsManager.h | 2 +- td/telegram/PasswordManager.cpp | 5 +- td/telegram/TopDialogManager.cpp | 9 +-- td/telegram/TopDialogManager.h | 2 +- 14 files changed, 97 insertions(+), 97 deletions(-) diff --git a/td/telegram/Application.cpp b/td/telegram/Application.cpp index 0b29e3525..f84aa1bdb 100644 --- a/td/telegram/Application.cpp +++ b/td/telegram/Application.cpp @@ -150,7 +150,7 @@ static void save_app_log_impl(Td *td, telegram_api::object_ptr &&data, Promise &&promise) { CHECK(data != nullptr); - auto input_app_event = telegram_api::make_object(G()->server_time_cached(), type, + auto input_app_event = telegram_api::make_object(G()->server_time(), type, dialog_id.get(), std::move(data)); save_app_log_impl(td, std::move(input_app_event), 0, std::move(promise)); } diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index c50745197..e840c7e68 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -3868,8 +3868,9 @@ ContactsManager::ContactsManager(Td *td, ActorShared<> parent) : td_(td), parent was_online_local_ = to_integer(G()->td_db()->get_binlog_pmc()->get("my_was_online_local")); was_online_remote_ = to_integer(G()->td_db()->get_binlog_pmc()->get("my_was_online_remote")); - if (was_online_local_ >= G()->unix_time_cached() && !td_->is_online()) { - was_online_local_ = G()->unix_time_cached() - 1; + auto unix_time = G()->unix_time(); + if (was_online_local_ >= unix_time && !td_->is_online()) { + was_online_local_ = unix_time - 1; } location_visibility_expire_date_ = @@ -3998,7 +3999,8 @@ void ContactsManager::on_user_online_timeout(UserId user_id) { LOG(INFO) << "Update " << user_id << " online status to offline"; send_closure(G()->td(), &Td::send_update, - td_api::make_object(user_id.get(), get_user_status_object(user_id, u))); + td_api::make_object(user_id.get(), + get_user_status_object(user_id, u, G()->unix_time()))); update_user_online_member_count(u); } @@ -5960,14 +5962,14 @@ void ContactsManager::set_my_online_status(bool is_online, bool send_update, boo User *u = get_user_force(my_id, "set_my_online_status"); if (u != nullptr) { int32 new_online; - int32 now = G()->unix_time(); + int32 unix_time = G()->unix_time(); if (is_online) { - new_online = now + 300; + new_online = unix_time + 300; } else { - new_online = now - 1; + new_online = unix_time - 1; } - auto old_was_online = get_user_was_online(u, my_id); + auto old_was_online = get_user_was_online(u, my_id, unix_time); if (is_local) { LOG(INFO) << "Update my local online from " << my_was_online_local_ << " to " << new_online; if (!is_online) { @@ -5984,7 +5986,7 @@ void ContactsManager::set_my_online_status(bool is_online, bool send_update, boo u->need_save_to_database = true; } } - if (old_was_online != get_user_was_online(u, my_id)) { + if (old_was_online != get_user_was_online(u, my_id, unix_time)) { u->is_status_changed = true; u->is_online_status_changed = true; } @@ -6004,7 +6006,7 @@ void ContactsManager::set_my_online_status(bool is_online, bool send_update, boo ContactsManager::MyOnlineStatusInfo ContactsManager::get_my_online_status() const { MyOnlineStatusInfo status_info; status_info.is_online_local = td_->is_online(); - status_info.is_online_remote = was_online_remote_ > G()->unix_time_cached(); + status_info.is_online_remote = was_online_remote_ > G()->unix_time(); status_info.was_online_local = was_online_local_; status_info.was_online_remote = was_online_remote_; @@ -6184,7 +6186,7 @@ bool ContactsManager::is_allowed_username(const string &username) { return true; } -int32 ContactsManager::get_user_was_online(const User *u, UserId user_id) const { +int32 ContactsManager::get_user_was_online(const User *u, UserId user_id, int32 unix_time) const { if (u == nullptr || u->is_deleted) { return 0; } @@ -6195,7 +6197,7 @@ int32 ContactsManager::get_user_was_online(const User *u, UserId user_id) const was_online = my_was_online_local_; } } else { - if (u->local_was_online > 0 && u->local_was_online > was_online && u->local_was_online > G()->unix_time_cached()) { + if (u->local_was_online > 0 && u->local_was_online > was_online && u->local_was_online > unix_time) { was_online = u->local_was_online; } } @@ -11738,8 +11740,9 @@ void ContactsManager::update_user(User *u, UserId user_id, bool from_binlog, boo } u->is_phone_number_changed = false; } + auto unix_time = G()->unix_time(); if (u->is_status_changed && user_id != get_my_id()) { - auto left_time = get_user_was_online(u, user_id) - G()->server_time_cached(); + auto left_time = get_user_was_online(u, user_id, unix_time) - G()->server_time(); if (left_time >= 0 && left_time < 30 * 86400) { left_time += 2.0; // to guarantee expiration LOG(DEBUG) << "Set online timeout for " << user_id << " in " << left_time << " seconds"; @@ -11762,7 +11765,6 @@ void ContactsManager::update_user(User *u, UserId user_id, bool from_binlog, boo } } - auto unix_time = G()->unix_time(); auto effective_emoji_status = u->emoji_status.get_effective_emoji_status(u->is_premium, unix_time); if (effective_emoji_status != u->last_sent_emoji_status) { u->last_sent_emoji_status = effective_emoji_status; @@ -11812,8 +11814,9 @@ void ContactsManager::update_user(User *u, UserId user_id, bool from_binlog, boo u->is_status_saved = false; } CHECK(u->is_update_user_sent); - send_closure(G()->td(), &Td::send_update, - make_tl_object(user_id.get(), get_user_status_object(user_id, u))); + send_closure( + G()->td(), &Td::send_update, + make_tl_object(user_id.get(), get_user_status_object(user_id, u, unix_time))); u->is_status_changed = false; } if (u->is_online_status_changed) { @@ -11980,7 +11983,7 @@ void ContactsManager::update_channel(Channel *c, ChannelId channel_id, bool from auto until_date = c->status.get_until_date(); int32 left_time = 0; if (until_date > 0) { - left_time = until_date - G()->unix_time_cached() + 1; + left_time = until_date - G()->unix_time() + 1; CHECK(left_time > 0); } if (left_time > 0 && left_time < 366 * 86400) { @@ -13581,8 +13584,9 @@ void ContactsManager::on_update_user_online(User *u, UserId user_id, tl_object_p if (new_online != u->was_online) { LOG(DEBUG) << "Update " << user_id << " online from " << u->was_online << " to " << new_online; - bool old_is_online = u->was_online > G()->unix_time_cached(); - bool new_is_online = new_online > G()->unix_time_cached(); + auto unix_time = G()->unix_time(); + bool old_is_online = u->was_online > unix_time; + bool new_is_online = new_online > unix_time; u->was_online = new_online; u->is_status_changed = true; if (u->was_online > 0) { @@ -13623,20 +13627,21 @@ void ContactsManager::on_update_user_local_was_online(User *u, UserId user_id, i if (u->is_deleted || u->is_bot || u->is_support || user_id == get_my_id()) { return; } - if (u->was_online > G()->unix_time_cached()) { + int32 unix_time = G()->unix_time(); + if (u->was_online > unix_time) { // if user is currently online, ignore local online return; } // bring users online for 30 seconds local_was_online += 30; - if (local_was_online < G()->unix_time_cached() + 2 || local_was_online <= u->local_was_online || + if (local_was_online < unix_time + 2 || local_was_online <= u->local_was_online || local_was_online <= u->was_online) { return; } LOG(DEBUG) << "Update " << user_id << " local online from " << u->local_was_online << " to " << local_was_online; - bool old_is_online = u->local_was_online > G()->unix_time_cached(); + bool old_is_online = u->local_was_online > unix_time; u->local_was_online = local_was_online; u->is_status_changed = true; @@ -14097,7 +14102,7 @@ void ContactsManager::update_user_online_member_count(User *u) { return; } - auto now = G()->unix_time_cached(); + auto now = G()->unix_time(); vector expired_dialog_ids; for (const auto &it : u->online_member_dialogs) { auto dialog_id = it.first; @@ -14160,7 +14165,7 @@ void ContactsManager::update_dialog_online_member_count(const vectorunix_time(); + int32 unix_time = G()->unix_time(); for (const auto &participant : participants) { if (participant.dialog_id_.get_type() != DialogType::User) { continue; @@ -14168,11 +14173,11 @@ void ContactsManager::update_dialog_online_member_count(const vectoris_deleted && !u->is_bot) { - if (get_user_was_online(u, user_id) > time) { + if (get_user_was_online(u, user_id, unix_time) > unix_time) { online_member_count++; } if (is_from_server) { - u->online_member_dialogs[dialog_id] = time; + u->online_member_dialogs[dialog_id] = unix_time; } } } @@ -16737,8 +16742,9 @@ Result ContactsManager::get_bot_data(UserId user_id) c } bool ContactsManager::is_user_online(UserId user_id, int32 tolerance) const { - int32 was_online = get_user_was_online(get_user(user_id), user_id); - return was_online > G()->unix_time() - tolerance; + auto unix_time = G()->unix_time(); + int32 was_online = get_user_was_online(get_user(user_id), user_id, unix_time); + return was_online > unix_time - tolerance; } bool ContactsManager::is_user_status_exact(UserId user_id) const { @@ -17858,6 +17864,7 @@ std::pair> ContactsManager::search_among_dialogs(const v const string &query, int32 limit) const { Hints hints; // TODO cache Hints + auto unix_time = G()->unix_time(); for (auto dialog_id : dialog_ids) { int64 rating = 0; if (dialog_id.get_type() == DialogType::User) { @@ -17871,7 +17878,7 @@ std::pair> ContactsManager::search_among_dialogs(const v } else { hints.add(dialog_id.get(), get_user_search_text(u)); } - rating = -get_user_was_online(u, user_id); + rating = -get_user_was_online(u, user_id, unix_time); } else { if (!td_->messages_manager_->have_dialog_info(dialog_id)) { continue; @@ -19085,12 +19092,13 @@ void ContactsManager::on_upload_profile_photo_error(FileId file_id, Status statu promise.set_error(std::move(status)); // TODO check that status has valid error code } -td_api::object_ptr ContactsManager::get_user_status_object(UserId user_id, const User *u) const { +td_api::object_ptr ContactsManager::get_user_status_object(UserId user_id, const User *u, + int32 unix_time) const { if (u->is_bot) { return make_tl_object(std::numeric_limits::max()); } - int32 was_online = get_user_was_online(u, user_id); + int32 was_online = get_user_was_online(u, user_id, unix_time); switch (was_online) { case -3: return make_tl_object(); @@ -19164,9 +19172,10 @@ tl_object_ptr ContactsManager::get_user_object(UserId user_id, con auto have_access = user_id == get_my_id() || have_input_peer_user(u, user_id, AccessRights::Know); return td_api::make_object( user_id.get(), u->first_name, u->last_name, u->usernames.get_usernames_object(), u->phone_number, - get_user_status_object(user_id, u), get_profile_photo_object(td_->file_manager_.get(), u->photo), - std::move(emoji_status), u->is_contact, u->is_mutual_contact, u->is_close_friend, u->is_verified, u->is_premium, - u->is_support, get_restriction_reason_description(u->restriction_reasons), u->is_scam, u->is_fake, + get_user_status_object(user_id, u, G()->unix_time()), + get_profile_photo_object(td_->file_manager_.get(), u->photo), std::move(emoji_status), u->is_contact, + u->is_mutual_contact, u->is_close_friend, u->is_verified, u->is_premium, u->is_support, + get_restriction_reason_description(u->restriction_reasons), u->is_scam, u->is_fake, u->max_active_story_id.is_valid(), get_user_has_unread_stories(u), have_access, std::move(type), u->language_code, u->attach_menu_enabled); } diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 94a50f0e9..8046d8052 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -1614,7 +1614,7 @@ class ContactsManager final : public Actor { static bool is_user_bot(const User *u); - int32 get_user_was_online(const User *u, UserId user_id) const; + int32 get_user_was_online(const User *u, UserId user_id, int32 unix_time) const; int64 get_contacts_hash(); @@ -1758,7 +1758,7 @@ class ContactsManager final : public Actor { td_api::object_ptr get_update_unknown_user_object(UserId user_id) const; - td_api::object_ptr get_user_status_object(UserId user_id, const User *u) const; + td_api::object_ptr get_user_status_object(UserId user_id, const User *u, int32 unix_time) const; tl_object_ptr get_user_object(UserId user_id, const User *u) const; diff --git a/td/telegram/Global.h b/td/telegram/Global.h index 6c3f21463..ea0b1a8cf 100644 --- a/td/telegram/Global.h +++ b/td/telegram/Global.h @@ -146,21 +146,12 @@ class Global final : public ActorContext { bool is_server_time_reliable() const { return server_time_difference_was_updated_.load(std::memory_order_relaxed); } - double to_server_time(double now) const { - return now + get_server_time_difference(); - } double server_time() const { - return to_server_time(Time::now()); - } - double server_time_cached() const { - return to_server_time(Time::now_cached()); + return Time::now() + get_server_time_difference(); } int32 unix_time() const { return to_unix_time(server_time()); } - int32 unix_time_cached() const { - return to_unix_time(server_time_cached()); - } void update_server_time_difference(double diff, bool force); diff --git a/td/telegram/InlineQueriesManager.cpp b/td/telegram/InlineQueriesManager.cpp index 60212d6d1..c79282c02 100644 --- a/td/telegram/InlineQueriesManager.cpp +++ b/td/telegram/InlineQueriesManager.cpp @@ -1095,8 +1095,8 @@ void InlineQueriesManager::loop() { pending_inline_query_ = nullptr; } else { if (!has_timeout()) { - LOG(INFO) << "Schedule send inline query " << pending_inline_query_->query_hash << " at " - << G()->to_server_time(next_inline_query_time_); + LOG(INFO) << "Schedule send inline query " << pending_inline_query_->query_hash << " in " + << next_inline_query_time_ - now; set_timeout_at(next_inline_query_time_); } } diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 529e539d9..1eb79f64b 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -5787,7 +5787,7 @@ tl_object_ptr get_message_content_object(const MessageCo } case MessageContentType::LiveLocation: { const auto *m = static_cast(content); - auto passed = max(G()->unix_time_cached() - message_date, 0); + auto passed = max(G()->unix_time() - message_date, 0); auto expires_in = max(0, m->period - passed); auto heading = expires_in == 0 ? 0 : m->heading; auto proximity_alert_radius = expires_in == 0 ? 0 : m->proximity_alert_radius; diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 961232340..1996ca053 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -7632,7 +7632,7 @@ void MessagesManager::on_dialog_action(DialogId dialog_id, MessageId top_thread_ active_dialog_action_timeout_.cancel_timeout(dialog_id.get()); } } else { - if (date < G()->unix_time_cached() - DIALOG_ACTION_TIMEOUT - 60) { + if (date < G()->unix_time() - DIALOG_ACTION_TIMEOUT - 60) { LOG(DEBUG) << "Ignore too old action of " << typing_dialog_id << " in " << dialog_id << " sent at " << date; return; } @@ -8060,10 +8060,9 @@ bool MessagesManager::update_dialog_notification_settings(DialogId dialog_id, return need_update.need_update_server; } -void MessagesManager::schedule_dialog_unmute(DialogId dialog_id, bool use_default, int32 mute_until) { - auto now = G()->unix_time_cached(); - if (!use_default && mute_until >= now && mute_until < now + 366 * 86400) { - dialog_unmute_timeout_.set_timeout_in(dialog_id.get(), mute_until - now + 1); +void MessagesManager::schedule_dialog_unmute(DialogId dialog_id, bool use_default, int32 mute_until, int32 unix_time) { + if (!use_default && mute_until >= unix_time && mute_until < unix_time + 366 * 86400) { + dialog_unmute_timeout_.set_timeout_in(dialog_id.get(), mute_until - unix_time + 1); } else { dialog_unmute_timeout_.cancel_timeout(dialog_id.get()); } @@ -8082,7 +8081,7 @@ void MessagesManager::update_dialog_unmute_timeout(Dialog *d, bool &old_use_defa CHECK(d != nullptr); CHECK(old_mute_until >= 0); - schedule_dialog_unmute(d->dialog_id, new_use_default, new_mute_until); + schedule_dialog_unmute(d->dialog_id, new_use_default, new_mute_until, G()->unix_time()); auto scope = get_dialog_notification_setting_scope(d->dialog_id); auto scope_mute_until = td_->notification_settings_manager_->get_scope_mute_until(scope); @@ -8208,11 +8207,11 @@ void MessagesManager::on_dialog_unmute(DialogId dialog_id) { return; } - auto now = G()->unix_time(); - if (d->notification_settings.mute_until > now) { - LOG(ERROR) << "Failed to unmute " << dialog_id << " in " << now << ", will be unmuted in " - << d->notification_settings.mute_until; - schedule_dialog_unmute(dialog_id, false, d->notification_settings.mute_until); + auto unix_time = G()->unix_time(); + if (d->notification_settings.mute_until > unix_time) { + LOG(INFO) << "Failed to unmute " << dialog_id << " in " << unix_time << ", will be unmuted in " + << d->notification_settings.mute_until; + schedule_dialog_unmute(dialog_id, false, d->notification_settings.mute_until, unix_time); return; } @@ -10737,7 +10736,7 @@ bool MessagesManager::can_delete_channel_message(const DialogParticipantStatus & return true; } - if (is_bot && G()->unix_time_cached() >= m->date + 2 * 86400) { + if (is_bot && G()->unix_time() >= m->date + 2 * 86400) { // bots can't delete messages older than 2 days return false; } @@ -10818,12 +10817,12 @@ bool MessagesManager::can_revoke_message(DialogId dialog_id, const Message *m) c int64 revoke_time_limit = td_->option_manager_->get_option_integer("revoke_pm_time_limit", DEFAULT_REVOKE_TIME_LIMIT); - if (G()->unix_time_cached() - m->date < 86400 && content_type == MessageContentType::Dice) { + if (G()->unix_time() - m->date < 86400 && content_type == MessageContentType::Dice) { return false; } return ((m->is_outgoing && !is_service_message_content(content_type)) || (can_revoke_incoming && content_type != MessageContentType::ScreenshotTaken)) && - G()->unix_time_cached() - m->date <= revoke_time_limit; + G()->unix_time() - m->date <= revoke_time_limit; } case DialogType::Chat: { bool is_appointed_administrator = @@ -10832,7 +10831,7 @@ bool MessagesManager::can_revoke_message(DialogId dialog_id, const Message *m) c td_->option_manager_->get_option_integer("revoke_time_limit", DEFAULT_REVOKE_TIME_LIMIT); return ((m->is_outgoing && !is_service_message_content(content_type)) || is_appointed_administrator) && - G()->unix_time_cached() - m->date <= revoke_time_limit; + G()->unix_time() - m->date <= revoke_time_limit; } case DialogType::Channel: return true; // any server message that can be deleted will be deleted for all participants @@ -11734,8 +11733,7 @@ void MessagesManager::unload_dialog(DialogId dialog_id, int32 delay) { } bool has_left_to_unload_messages = false; - auto to_unload_message_ids = - find_unloadable_messages(d, G()->unix_time_cached() - delay, has_left_to_unload_messages); + auto to_unload_message_ids = find_unloadable_messages(d, G()->unix_time() - delay, has_left_to_unload_messages); vector unloaded_message_ids; vector> unloaded_messages; @@ -26191,7 +26189,7 @@ bool MessagesManager::can_edit_message(DialogId dialog_id, const Message *m, boo if (has_edit_time_limit) { const int32 DEFAULT_EDIT_TIME_LIMIT = 2 * 86400; int64 edit_time_limit = td_->option_manager_->get_option_integer("edit_time_limit", DEFAULT_EDIT_TIME_LIMIT); - if (G()->unix_time_cached() - m->date - (is_editing ? 300 : 0) >= edit_time_limit) { + if (G()->unix_time() - m->date - (is_editing ? 300 : 0) >= edit_time_limit) { return false; } } @@ -26211,7 +26209,7 @@ bool MessagesManager::can_edit_message(DialogId dialog_id, const Message *m, boo // there is no caption to edit, but bot can edit inline reply_markup return true; } - return G()->unix_time_cached() - m->date < get_message_content_live_location_period(m->content.get()); + return G()->unix_time() - m->date < get_message_content_live_location_period(m->content.get()); } case MessageContentType::Poll: { if (is_bot && only_reply_markup) { @@ -34026,7 +34024,7 @@ const MessagesManager::Message *MessagesManager::get_message(const Dialog *d, Me } else { result = d->messages.get_pointer(message_id); if (result != nullptr) { - auto unix_time = G()->unix_time_cached(); + auto unix_time = G()->unix_time(); if (unix_time > result->last_access_date + 5) { result->last_access_date = unix_time; auto list_node = const_cast(static_cast(result)); @@ -34263,7 +34261,7 @@ void MessagesManager::fix_new_message(const Dialog *d, Message *m, bool from_dat } } - m->last_access_date = G()->unix_time_cached(); + m->last_access_date = G()->unix_time(); if (m->contains_mention) { CHECK(!td_->auth_manager_->is_bot()); @@ -34287,7 +34285,7 @@ void MessagesManager::fix_new_message(const Dialog *d, Message *m, bool from_dat if (dialog_type == DialogType::Channel && !m->contains_unread_mention) { auto channel_read_media_period = td_->option_manager_->get_option_integer("channels_read_media_period", (G()->is_test_dc() ? 300 : 7 * 86400)); - if (m->date < G()->unix_time_cached() - channel_read_media_period) { + if (m->date < G()->unix_time() - channel_read_media_period) { update_opened_message_content(m->content.get()); } } @@ -34815,7 +34813,7 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq } if (from_update && !m->is_failed_to_send && dialog_type == DialogType::Channel) { - auto now = max(G()->unix_time_cached(), m->date); + auto now = max(G()->unix_time(), m->date); if (m->date < now - 2 * 86400 && Slice(source) == Slice("updateNewChannelMessage")) { // if the message is pretty old, we might have missed the update that the message has already been read repair_channel_server_unread_count(d); @@ -36604,7 +36602,7 @@ void MessagesManager::fix_new_dialog(Dialog *d, unique_ptr &&last_datab d->notification_settings.mute_until <= G()->unix_time()) { d->notification_settings.mute_until = 0; } else { - schedule_dialog_unmute(dialog_id, false, d->notification_settings.mute_until); + schedule_dialog_unmute(dialog_id, false, d->notification_settings.mute_until, G()->unix_time()); } if (d->notification_info != nullptr && d->notification_info->pinned_message_notification_message_id_.is_valid()) { auto pinned_message_id = d->notification_info->pinned_message_notification_message_id_; diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 33219805f..136835249 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -2634,7 +2634,7 @@ class MessagesManager final : public Actor { bool update_dialog_notification_settings(DialogId dialog_id, DialogNotificationSettings *current_settings, DialogNotificationSettings &&new_settings); - void schedule_dialog_unmute(DialogId dialog_id, bool use_default, int32 mute_until); + void schedule_dialog_unmute(DialogId dialog_id, bool use_default, int32 mute_until, int32 unix_time); void update_dialog_unmute_timeout(Dialog *d, bool &old_use_default, int32 &old_mute_until, bool new_use_default, int32 new_mute_until); diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index 39b7c909a..b7d15f879 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -843,6 +843,7 @@ int32 NotificationManager::get_notification_delay_ms(DialogId dialog_id, const P return MIN_NOTIFICATION_DELAY_MS; } + auto server_time = G()->server_time(); auto delay_ms = [&] { auto online_info = td_->contacts_manager_->get_my_online_status(); if (!online_info.is_online_local && online_info.is_online_remote) { @@ -852,8 +853,8 @@ int32 NotificationManager::get_notification_delay_ms(DialogId dialog_id, const P } if (!online_info.is_online_local && - online_info.was_online_remote > max(static_cast(online_info.was_online_local), - G()->server_time_cached() - online_cloud_timeout_ms_ * 1e-3)) { + online_info.was_online_remote > + max(static_cast(online_info.was_online_local), server_time - online_cloud_timeout_ms_ * 1e-3)) { // If we are offline, but was online from some other client in last 'online_cloud_timeout' seconds // after we had gone offline, then delay notification for 'notification_cloud_delay' seconds. return notification_cloud_delay_ms_; @@ -868,8 +869,7 @@ int32 NotificationManager::get_notification_delay_ms(DialogId dialog_id, const P return 0; }(); - auto passed_time_ms = - static_cast(clamp(G()->server_time_cached() - notification.date - 1, 0.0, 1000000.0) * 1000); + auto passed_time_ms = static_cast(clamp(server_time - notification.date - 1, 0.0, 1000000.0) * 1000); return max(max(min_delay_ms, delay_ms) - passed_time_ms, MIN_NOTIFICATION_DELAY_MS); } diff --git a/td/telegram/NotificationSettingsManager.cpp b/td/telegram/NotificationSettingsManager.cpp index 3801c1eda..c86c3bcfe 100644 --- a/td/telegram/NotificationSettingsManager.cpp +++ b/td/telegram/NotificationSettingsManager.cpp @@ -590,7 +590,7 @@ void NotificationSettingsManager::init() { VLOG(notifications) << "Loaded notification settings in " << scope << ": " << *current_settings; - schedule_scope_unmute(scope, current_settings->mute_until); + schedule_scope_unmute(scope, current_settings->mute_until, G()->unix_time()); send_closure(G()->td(), &Td::send_update, get_update_scope_notification_settings_object(scope)); } @@ -728,11 +728,11 @@ void NotificationSettingsManager::on_scope_unmute(NotificationSettingsScope scop return; } - auto now = G()->unix_time(); - if (notification_settings->mute_until > now) { - LOG(ERROR) << "Failed to unmute " << scope << " in " << now << ", will be unmuted in " - << notification_settings->mute_until; - schedule_scope_unmute(scope, notification_settings->mute_until); + auto unix_time = G()->unix_time(); + if (notification_settings->mute_until > unix_time) { + LOG(INFO) << "Failed to unmute " << scope << " in " << unix_time << ", will be unmuted in " + << notification_settings->mute_until; + schedule_scope_unmute(scope, notification_settings->mute_until, unix_time); return; } @@ -824,10 +824,10 @@ bool NotificationSettingsManager::update_scope_notification_settings(Notificatio return need_update_server; } -void NotificationSettingsManager::schedule_scope_unmute(NotificationSettingsScope scope, int32 mute_until) { - auto now = G()->unix_time_cached(); - if (mute_until >= now && mute_until < now + 366 * 86400) { - scope_unmute_timeout_.set_timeout_in(static_cast(scope) + 1, mute_until - now + 1); +void NotificationSettingsManager::schedule_scope_unmute(NotificationSettingsScope scope, int32 mute_until, + int32 unix_time) { + if (mute_until >= unix_time && mute_until < unix_time + 366 * 86400) { + scope_unmute_timeout_.set_timeout_in(static_cast(scope) + 1, mute_until - unix_time + 1); } else { scope_unmute_timeout_.cancel_timeout(static_cast(scope) + 1); } @@ -846,7 +846,7 @@ void NotificationSettingsManager::update_scope_unmute_timeout(NotificationSettin } CHECK(old_mute_until >= 0); - schedule_scope_unmute(scope, new_mute_until); + schedule_scope_unmute(scope, new_mute_until, G()->unix_time()); auto was_muted = old_mute_until != 0; auto is_muted = new_mute_until != 0; diff --git a/td/telegram/NotificationSettingsManager.h b/td/telegram/NotificationSettingsManager.h index 6a8ea2229..a39f58b6b 100644 --- a/td/telegram/NotificationSettingsManager.h +++ b/td/telegram/NotificationSettingsManager.h @@ -188,7 +188,7 @@ class NotificationSettingsManager final : public Actor { void update_scope_notification_settings_on_server(NotificationSettingsScope scope, uint64 log_event_id); - void schedule_scope_unmute(NotificationSettingsScope scope, int32 mute_until); + void schedule_scope_unmute(NotificationSettingsScope scope, int32 mute_until, int32 unix_time); void update_scope_unmute_timeout(NotificationSettingsScope scope, int32 &old_mute_until, int32 new_mute_until); diff --git a/td/telegram/PasswordManager.cpp b/td/telegram/PasswordManager.cpp index cffe24c63..ba3fbd15e 100644 --- a/td/telegram/PasswordManager.cpp +++ b/td/telegram/PasswordManager.cpp @@ -32,10 +32,11 @@ namespace td { tl_object_ptr TempPasswordState::get_temporary_password_state_object() const { - if (!has_temp_password || valid_until <= G()->unix_time()) { + auto unix_time = G()->unix_time(); + if (!has_temp_password || valid_until <= unix_time) { return make_tl_object(false, 0); } - return make_tl_object(true, valid_until - G()->unix_time_cached()); + return make_tl_object(true, valid_until - unix_time); } static void hash_sha256(Slice data, Slice salt, MutableSlice dest) { diff --git a/td/telegram/TopDialogManager.cpp b/td/telegram/TopDialogManager.cpp index caca8e384..5f9b2ccd0 100644 --- a/td/telegram/TopDialogManager.cpp +++ b/td/telegram/TopDialogManager.cpp @@ -351,14 +351,15 @@ double TopDialogManager::rating_add(double now, double rating_timestamp) const { return std::exp((now - rating_timestamp) / rating_e_decay_); } -double TopDialogManager::current_rating_add(double rating_timestamp) const { - return rating_add(G()->server_time_cached(), rating_timestamp); +double TopDialogManager::current_rating_add(double server_time, double rating_timestamp) const { + return rating_add(server_time, rating_timestamp); } void TopDialogManager::normalize_rating() { + auto server_time = G()->server_time(); for (auto &top_dialogs : by_category_) { - auto div_by = current_rating_add(top_dialogs.rating_timestamp); - top_dialogs.rating_timestamp = G()->server_time_cached(); + auto div_by = current_rating_add(server_time, top_dialogs.rating_timestamp); + top_dialogs.rating_timestamp = server_time; for (auto &dialog : top_dialogs.dialogs) { dialog.rating /= div_by; } diff --git a/td/telegram/TopDialogManager.h b/td/telegram/TopDialogManager.h index efa192303..b9513ee28 100644 --- a/td/telegram/TopDialogManager.h +++ b/td/telegram/TopDialogManager.h @@ -99,7 +99,7 @@ class TopDialogManager final : public Actor { std::array(TopDialogCategory::Size)> by_category_; double rating_add(double now, double rating_timestamp) const; - double current_rating_add(double rating_timestamp) const; + double current_rating_add(double server_time, double rating_timestamp) const; void normalize_rating(); bool set_is_enabled(bool is_enabled);