From fa457236e086da571b917ff0cf1fa698fa6eab95 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 13 Feb 2020 03:03:50 +0300 Subject: [PATCH] Ignore duplicate auth notifications. GitOrigin-RevId: 103e8ca99f2b2e35455ccd426a415177287bd814 --- td/telegram/MessagesManager.cpp | 60 +++++++++++++++++++++++++++++++++ td/telegram/MessagesManager.h | 5 +++ 2 files changed, 65 insertions(+) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 95326b79..46f83c4f 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -5438,11 +5438,45 @@ MessagesManager::Dialog *MessagesManager::get_service_notifications_dialog() { return get_dialog(service_notifications_dialog_id); } +void MessagesManager::save_auth_notification_ids() { + auto min_date = G()->unix_time() - AUTH_NOTIFICATION_ID_CACHE_TIME; + vector ids; + for (auto &it : auth_notification_id_date_) { + auto date = it.second; + if (date < min_date) { + continue; + } + ids.push_back(it.first); + ids.push_back(to_string(date)); + } + + if (ids.empty()) { + G()->td_db()->get_binlog_pmc()->erase("auth_notification_ids"); + return; + } + + G()->td_db()->get_binlog_pmc()->set("auth_notification_ids", implode(ids, ',')); +} + void MessagesManager::on_update_service_notification(tl_object_ptr &&update, bool skip_new_entities, Promise &&promise) { int32 ttl = 0; bool has_date = (update->flags_ & telegram_api::updateServiceNotification::INBOX_DATE_MASK) != 0; auto date = has_date ? update->inbox_date_ : G()->unix_time(); + if (date <= 0) { + LOG(ERROR) << "Receive message date " << date << " in " << to_string(update); + return; + } + bool is_auth_notification = begins_with(update->type_, "auth"); + if (is_auth_notification) { + auto &old_date = auth_notification_id_date_[update->type_.substr(4)]; + if (date <= old_date) { + LOG(INFO) << "Skip already applied " << to_string(update); + return; + } + old_date = date; + } + auto message_text = get_message_text(td_->contacts_manager_.get(), std::move(update->message_), std::move(update->entities_), skip_new_entities, date, false, "on_update_service_notification"); @@ -5450,6 +5484,7 @@ void MessagesManager::on_update_service_notification(tl_object_ptrmedia_), td_->auth_manager_->is_bot() ? DialogId() : get_service_notifications_dialog()->dialog_id, false, UserId(), &ttl); bool is_content_secret = is_secret_message_content(ttl, content->get_type()); + if ((update->flags_ & telegram_api::updateServiceNotification::POPUP_MASK) != 0) { send_closure(G()->td(), &Td::send_update, td_api::make_object( @@ -5460,6 +5495,7 @@ void MessagesManager::on_update_service_notification(tl_object_ptrdialog_id; CHECK(dialog_id.get_type() == DialogType::User); + auto new_message = make_unique(); set_message_id(new_message, get_next_local_message_id(d)); new_message->sender_user_id = dialog_id.get_user_id(); @@ -5484,6 +5520,10 @@ void MessagesManager::on_update_service_notification(tl_object_ptr &&update) { @@ -10399,6 +10439,26 @@ void MessagesManager::init() { } G()->td_db()->get_binlog_pmc()->erase("nsfac"); + auto auth_notification_ids_string = G()->td_db()->get_binlog_pmc()->get("auth_notification_ids"); + if (!auth_notification_ids_string.empty()) { + VLOG(notifications) << "Load auth_notification_ids = " << auth_notification_ids_string; + auto ids = full_split(auth_notification_ids_string, ','); + CHECK(ids.size() % 2 == 0); + bool is_changed = false; + auto min_date = G()->unix_time() - AUTH_NOTIFICATION_ID_CACHE_TIME; + for (size_t i = 0; i < ids.size(); i += 2) { + auto date = to_integer_safe(ids[i + 1]).ok(); + if (date < min_date) { + is_changed = true; + continue; + } + auth_notification_id_date_.emplace(std::move(ids[i]), date); + } + if (is_changed) { + save_auth_notification_ids(); + } + } + /* FI LE *f = std::f open("error.txt", "r"); if (f != nullptr) { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 70fcf9b8..7eea508f 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -1451,6 +1451,7 @@ class MessagesManager : public Actor { static constexpr int32 USERNAME_CACHE_EXPIRE_TIME = 3 * 86400; static constexpr int32 USERNAME_CACHE_EXPIRE_TIME_SHORT = 900; + static constexpr int32 AUTH_NOTIFICATION_ID_CACHE_TIME = 7 * 86400; static constexpr int32 ONLINE_MEMBER_COUNT_UPDATE_TIME = 5 * 60; @@ -2372,6 +2373,8 @@ class MessagesManager : public Actor { Dialog *get_service_notifications_dialog(); + void save_auth_notification_ids(); + static MessageId get_next_message_id(Dialog *d, MessageType type); static MessageId get_next_local_message_id(Dialog *d); @@ -2737,6 +2740,8 @@ class MessagesManager : public Actor { std::unordered_map dialog_online_member_counts_; + std::unordered_map auth_notification_id_date_; + uint32 scheduled_messages_sync_generation_ = 1; DialogId sponsored_dialog_id_;