From 07beb3e86b4fe089b9fd215d0f2cfa649cd26448 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 31 Jan 2022 11:19:21 +0300 Subject: [PATCH] Correctly clear unread reactions. --- Telegram/SourceFiles/api/api_updates.cpp | 10 ++++- .../data/data_message_reactions.cpp | 9 ++--- .../SourceFiles/data/data_message_reactions.h | 2 +- Telegram/SourceFiles/history/history_item.cpp | 39 +++++++------------ Telegram/SourceFiles/history/history_item.h | 2 +- .../SourceFiles/history/history_message.cpp | 2 +- 6 files changed, 28 insertions(+), 36 deletions(-) diff --git a/Telegram/SourceFiles/api/api_updates.cpp b/Telegram/SourceFiles/api/api_updates.cpp index fcdefb247..3fd636be5 100644 --- a/Telegram/SourceFiles/api/api_updates.cpp +++ b/Telegram/SourceFiles/api/api_updates.cpp @@ -36,6 +36,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_cloud_manager.h" #include "history/history.h" #include "history/history_item.h" +#include "history/history_unread_things.h" #include "core/application.h" #include "storage/storage_account.h" #include "storage/storage_facade.h" @@ -1630,8 +1631,13 @@ void Updates::feedUpdate(const MTPUpdate &update) { d.vmsg_id().v); if (item) { item->updateReactions(&d.vreactions()); - } else if (Data::Reactions::HasUnread(d.vreactions())) { - history->owner().histories().requestDialogEntry(history); + } else { + const auto hasUnreadReaction = Data::Reactions::HasUnread( + d.vreactions()); + if (hasUnreadReaction || history->unreadReactions().has()) { + // The unread reactions count could change. + history->owner().histories().requestDialogEntry(history); + } } } } break; diff --git a/Telegram/SourceFiles/data/data_message_reactions.cpp b/Telegram/SourceFiles/data/data_message_reactions.cpp index 9eeccff72..f6bf0bb4d 100644 --- a/Telegram/SourceFiles/data/data_message_reactions.cpp +++ b/Telegram/SourceFiles/data/data_message_reactions.cpp @@ -601,14 +601,14 @@ bool MessageReactions::checkIfChanged( }); } -void MessageReactions::set( +bool MessageReactions::change( const QVector &list, const QVector &recent, bool ignoreChosen) { auto &owner = _item->history()->owner(); if (owner.reactions().sending(_item)) { // We'll apply non-stale data from the request response. - return; + return false; } auto changed = false; auto existing = base::flat_set(); @@ -663,10 +663,7 @@ void MessageReactions::set( _recent = std::move(parsed); changed = true; } - - if (changed) { - owner.notifyItemDataChange(_item); - } + return changed; } const base::flat_map &MessageReactions::list() const { diff --git a/Telegram/SourceFiles/data/data_message_reactions.h b/Telegram/SourceFiles/data/data_message_reactions.h index 555828dad..9b0654fe0 100644 --- a/Telegram/SourceFiles/data/data_message_reactions.h +++ b/Telegram/SourceFiles/data/data_message_reactions.h @@ -150,7 +150,7 @@ public: void add(const QString &reaction); void remove(); - void set( + bool change( const QVector &list, const QVector &recent, bool ignoreChosen); diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index f41bfa87b..09e0cd1b2 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -847,9 +847,14 @@ void HistoryItem::toggleReaction(const QString &reaction) { void HistoryItem::updateReactions(const MTPMessageReactions *reactions) { const auto hadUnread = hasUnreadReaction(); - setReactions(reactions); - const auto hasUnread = hasUnreadReaction(); + const auto changed = changeReactions(reactions); + if (!changed) { + return; + } + const auto hasUnread = _reactions && !_reactions->findUnread().isEmpty(); if (hasUnread && !hadUnread) { + _flags |= MessageFlag::HasUnreadReaction; + addToUnreadThings(HistoryUnreadThings::AddType::New); // Call to addToUnreadThings may have read the reaction already. @@ -864,38 +869,25 @@ void HistoryItem::updateReactions(const MTPMessageReactions *reactions) { } else if (!hasUnread && hadUnread) { markReactionsRead(); } + history()->owner().notifyItemDataChange(this); } -void HistoryItem::setReactions(const MTPMessageReactions *reactions) { +bool HistoryItem::changeReactions(const MTPMessageReactions *reactions) { if (reactions || _reactionsLastRefreshed) { _reactionsLastRefreshed = crl::now(); } if (!reactions) { _flags &= ~MessageFlag::CanViewReactions; - if (_reactions) { - _reactions = nullptr; - if (hasUnreadReaction()) { - markReactionsRead(); - } - history()->owner().notifyItemDataChange(this); - } - return; + return (base::take(_reactions) != nullptr); } - reactions->match([&](const MTPDmessageReactions &data) { + return reactions->match([&](const MTPDmessageReactions &data) { if (data.is_can_see_list()) { _flags |= MessageFlag::CanViewReactions; } else { _flags &= ~MessageFlag::CanViewReactions; } if (data.vresults().v.isEmpty()) { - if (_reactions) { - _reactions = nullptr; - if (hasUnreadReaction()) { - markReactionsRead(); - } - history()->owner().notifyItemDataChange(this); - } - return; + return (base::take(_reactions) != nullptr); } else if (!_reactions) { _reactions = std::make_unique(this); } @@ -907,12 +899,9 @@ void HistoryItem::setReactions(const MTPMessageReactions *reactions) { if (_reactions->checkIfChanged(list, recent)) { updateReactionsUnknown(); } - } else { - _reactions->set(list, recent, min); - if (!_reactions->findUnread().isEmpty()) { - _flags |= MessageFlag::HasUnreadReaction; - } + return false; } + return _reactions->change(list, recent, min); }); } diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index a132b6e4c..6af925ffd 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -462,7 +462,7 @@ protected: void finishEdition(int oldKeyboardTop); void finishEditionToEmpty(); - void setReactions(const MTPMessageReactions *reactions); + bool changeReactions(const MTPMessageReactions *reactions); const not_null _history; const not_null _from; diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index 9f66763a8..6033df9f6 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -513,7 +513,7 @@ HistoryMessage::HistoryMessage( MessageGroupId::FromRaw(history->peer->id, groupedId->v)); } if (const auto reactions = data.vreactions()) { - setReactions(reactions); + changeReactions(reactions); } applyTTL(data);