From c4b7100ad8f6dbb373a571ec260ee489bfa4de0a Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 22 Feb 2023 13:09:24 +0300 Subject: [PATCH] Repair unread mention count in channels when a mention is read. --- td/telegram/MessagesManager.cpp | 50 ++++++++++++++++++++++++++++++--- td/telegram/MessagesManager.h | 3 ++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 91c72c0bc..02d366524 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -5572,6 +5572,7 @@ void MessagesManager::Dialog::store(StorerT &storer) const { STORE_FLAG(has_history_generation); STORE_FLAG(need_repair_unread_reaction_count); STORE_FLAG(is_translatable); + STORE_FLAG(need_repair_unread_mention_count); END_STORE_FLAGS(); } @@ -5836,6 +5837,7 @@ void MessagesManager::Dialog::parse(ParserT &parser) { PARSE_FLAG(has_history_generation); PARSE_FLAG(need_repair_unread_reaction_count); PARSE_FLAG(is_translatable); + PARSE_FLAG(need_repair_unread_mention_count); END_PARSE_FLAGS(); } else { need_repair_action_bar = false; @@ -12429,8 +12431,23 @@ void MessagesManager::read_channel_message_content_from_updates(Dialog *d, Messa Message *m = get_message_force(d, message_id, "read_channel_message_content_from_updates"); if (m != nullptr) { read_message_content(d, m, false, "read_channel_message_content_from_updates"); - } else if (message_id > d->last_new_message_id && d->last_new_message_id.is_valid()) { - get_channel_difference(d->dialog_id, d->pts, true, "read_channel_message_content_from_updates"); + } else { + if (!have_input_peer(d->dialog_id, AccessRights::Read)) { + LOG(INFO) << "Ignore updateChannelReadMessagesContents in inaccessible " << d->dialog_id; + if (d->unread_mention_count != 0) { + set_dialog_unread_mention_count(d, 0); + } + return; + } + if (message_id > d->last_new_message_id && d->last_new_message_id.is_valid()) { + get_channel_difference(d->dialog_id, d->pts, true, "read_channel_message_content_from_updates"); + } else { + // there is no message, so the update can be ignored + if (d->unread_mention_count > 0) { + // but if the chat has unread mentions, then number of unread mentions could have been changed + repair_dialog_unread_mention_count(d, "read_channel_message_content_from_updates"); + } + } } } @@ -12604,6 +12621,20 @@ void MessagesManager::repair_dialog_unread_reaction_count(Dialog *d, Promisedialog_id, std::move(promise), 0, source); } +void MessagesManager::repair_dialog_unread_mention_count(Dialog *d, const char *source) { + CHECK(d != nullptr); + + if (td_->auth_manager_->is_bot()) { + return; + } + if (!d->need_repair_unread_mention_count) { + d->need_repair_unread_mention_count = true; + on_dialog_updated(d->dialog_id, "repair_dialog_unread_mention_count"); + } + + send_get_dialog_query(d->dialog_id, Promise(), 0, source); +} + void MessagesManager::read_history_inbox(DialogId dialog_id, MessageId max_message_id, int32 unread_count, const char *source) { CHECK(!max_message_id.is_scheduled()); @@ -16163,7 +16194,15 @@ void MessagesManager::on_get_dialogs(FolderId folder_id, vectorparameters().use_message_db || is_new) { + if (!G()->parameters().use_message_db || is_new || d->need_repair_unread_mention_count) { + if (d->need_repair_unread_mention_count) { + if (d->unread_mention_count != dialog->unread_mentions_count_) { + LOG(INFO) << "Repaired unread mention count in " << dialog_id << " from " << d->unread_mention_count << " to " + << dialog->unread_mentions_count_; + } + d->need_repair_unread_mention_count = false; + on_dialog_updated(dialog_id, "repaired dialog unread mention count"); + } if (d->unread_mention_count != dialog->unread_mentions_count_) { set_dialog_unread_mention_count(d, dialog->unread_mentions_count_); update_dialog_mention_notification_count(d); @@ -16181,7 +16220,7 @@ void MessagesManager::on_get_dialogs(FolderId folder_id, vectorunread_reaction_count != dialog->unread_reactions_count_) { set_dialog_unread_reaction_count(d, dialog->unread_reactions_count_); - // update_dialog_mention_notification_count(d); + // update_dialog_reaction_notification_count(d); send_update_chat_unread_reaction_count(d, source); } } @@ -38180,6 +38219,9 @@ void MessagesManager::fix_new_dialog(Dialog *d, unique_ptr &&last_datab if (d->need_repair_unread_reaction_count) { repair_dialog_unread_reaction_count(d, Promise(), "fix_new_dialog"); } + if (d->need_repair_unread_mention_count) { + repair_dialog_unread_mention_count(d, "fix_new_dialog"); + } } bool MessagesManager::add_dialog_last_database_message(Dialog *d, unique_ptr &&last_database_message) { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index d0c04ce65..669aec812 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -1390,6 +1390,7 @@ class MessagesManager final : public Actor { bool is_available_reactions_inited = false; bool had_yet_unsent_message_id_overflow = false; bool need_repair_unread_reaction_count = false; + bool need_repair_unread_mention_count = false; bool is_translatable = false; bool increment_view_counter = false; @@ -2272,6 +2273,8 @@ class MessagesManager final : public Actor { void repair_dialog_unread_reaction_count(Dialog *d, Promise &&promise, const char *source); + void repair_dialog_unread_mention_count(Dialog *d, const char *source); + void read_history_outbox(DialogId dialog_id, MessageId max_message_id, int32 read_date = -1); void read_history_on_server(Dialog *d, MessageId max_message_id);