Correctly clear unread reactions.

This commit is contained in:
John Preston 2022-01-31 11:19:21 +03:00
parent 18919a6b4a
commit 07beb3e86b
6 changed files with 28 additions and 36 deletions

View file

@ -36,6 +36,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "lang/lang_cloud_manager.h" #include "lang/lang_cloud_manager.h"
#include "history/history.h" #include "history/history.h"
#include "history/history_item.h" #include "history/history_item.h"
#include "history/history_unread_things.h"
#include "core/application.h" #include "core/application.h"
#include "storage/storage_account.h" #include "storage/storage_account.h"
#include "storage/storage_facade.h" #include "storage/storage_facade.h"
@ -1630,8 +1631,13 @@ void Updates::feedUpdate(const MTPUpdate &update) {
d.vmsg_id().v); d.vmsg_id().v);
if (item) { if (item) {
item->updateReactions(&d.vreactions()); item->updateReactions(&d.vreactions());
} else if (Data::Reactions::HasUnread(d.vreactions())) { } else {
history->owner().histories().requestDialogEntry(history); 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; } break;

View file

@ -601,14 +601,14 @@ bool MessageReactions::checkIfChanged(
}); });
} }
void MessageReactions::set( bool MessageReactions::change(
const QVector<MTPReactionCount> &list, const QVector<MTPReactionCount> &list,
const QVector<MTPMessagePeerReaction> &recent, const QVector<MTPMessagePeerReaction> &recent,
bool ignoreChosen) { bool ignoreChosen) {
auto &owner = _item->history()->owner(); auto &owner = _item->history()->owner();
if (owner.reactions().sending(_item)) { if (owner.reactions().sending(_item)) {
// We'll apply non-stale data from the request response. // We'll apply non-stale data from the request response.
return; return false;
} }
auto changed = false; auto changed = false;
auto existing = base::flat_set<QString>(); auto existing = base::flat_set<QString>();
@ -663,10 +663,7 @@ void MessageReactions::set(
_recent = std::move(parsed); _recent = std::move(parsed);
changed = true; changed = true;
} }
return changed;
if (changed) {
owner.notifyItemDataChange(_item);
}
} }
const base::flat_map<QString, int> &MessageReactions::list() const { const base::flat_map<QString, int> &MessageReactions::list() const {

View file

@ -150,7 +150,7 @@ public:
void add(const QString &reaction); void add(const QString &reaction);
void remove(); void remove();
void set( bool change(
const QVector<MTPReactionCount> &list, const QVector<MTPReactionCount> &list,
const QVector<MTPMessagePeerReaction> &recent, const QVector<MTPMessagePeerReaction> &recent,
bool ignoreChosen); bool ignoreChosen);

View file

@ -847,9 +847,14 @@ void HistoryItem::toggleReaction(const QString &reaction) {
void HistoryItem::updateReactions(const MTPMessageReactions *reactions) { void HistoryItem::updateReactions(const MTPMessageReactions *reactions) {
const auto hadUnread = hasUnreadReaction(); const auto hadUnread = hasUnreadReaction();
setReactions(reactions); const auto changed = changeReactions(reactions);
const auto hasUnread = hasUnreadReaction(); if (!changed) {
return;
}
const auto hasUnread = _reactions && !_reactions->findUnread().isEmpty();
if (hasUnread && !hadUnread) { if (hasUnread && !hadUnread) {
_flags |= MessageFlag::HasUnreadReaction;
addToUnreadThings(HistoryUnreadThings::AddType::New); addToUnreadThings(HistoryUnreadThings::AddType::New);
// Call to addToUnreadThings may have read the reaction already. // Call to addToUnreadThings may have read the reaction already.
@ -864,38 +869,25 @@ void HistoryItem::updateReactions(const MTPMessageReactions *reactions) {
} else if (!hasUnread && hadUnread) { } else if (!hasUnread && hadUnread) {
markReactionsRead(); markReactionsRead();
} }
history()->owner().notifyItemDataChange(this);
} }
void HistoryItem::setReactions(const MTPMessageReactions *reactions) { bool HistoryItem::changeReactions(const MTPMessageReactions *reactions) {
if (reactions || _reactionsLastRefreshed) { if (reactions || _reactionsLastRefreshed) {
_reactionsLastRefreshed = crl::now(); _reactionsLastRefreshed = crl::now();
} }
if (!reactions) { if (!reactions) {
_flags &= ~MessageFlag::CanViewReactions; _flags &= ~MessageFlag::CanViewReactions;
if (_reactions) { return (base::take(_reactions) != nullptr);
_reactions = nullptr;
if (hasUnreadReaction()) {
markReactionsRead();
}
history()->owner().notifyItemDataChange(this);
}
return;
} }
reactions->match([&](const MTPDmessageReactions &data) { return reactions->match([&](const MTPDmessageReactions &data) {
if (data.is_can_see_list()) { if (data.is_can_see_list()) {
_flags |= MessageFlag::CanViewReactions; _flags |= MessageFlag::CanViewReactions;
} else { } else {
_flags &= ~MessageFlag::CanViewReactions; _flags &= ~MessageFlag::CanViewReactions;
} }
if (data.vresults().v.isEmpty()) { if (data.vresults().v.isEmpty()) {
if (_reactions) { return (base::take(_reactions) != nullptr);
_reactions = nullptr;
if (hasUnreadReaction()) {
markReactionsRead();
}
history()->owner().notifyItemDataChange(this);
}
return;
} else if (!_reactions) { } else if (!_reactions) {
_reactions = std::make_unique<Data::MessageReactions>(this); _reactions = std::make_unique<Data::MessageReactions>(this);
} }
@ -907,12 +899,9 @@ void HistoryItem::setReactions(const MTPMessageReactions *reactions) {
if (_reactions->checkIfChanged(list, recent)) { if (_reactions->checkIfChanged(list, recent)) {
updateReactionsUnknown(); updateReactionsUnknown();
} }
} else { return false;
_reactions->set(list, recent, min);
if (!_reactions->findUnread().isEmpty()) {
_flags |= MessageFlag::HasUnreadReaction;
}
} }
return _reactions->change(list, recent, min);
}); });
} }

View file

@ -462,7 +462,7 @@ protected:
void finishEdition(int oldKeyboardTop); void finishEdition(int oldKeyboardTop);
void finishEditionToEmpty(); void finishEditionToEmpty();
void setReactions(const MTPMessageReactions *reactions); bool changeReactions(const MTPMessageReactions *reactions);
const not_null<History*> _history; const not_null<History*> _history;
const not_null<PeerData*> _from; const not_null<PeerData*> _from;

View file

@ -513,7 +513,7 @@ HistoryMessage::HistoryMessage(
MessageGroupId::FromRaw(history->peer->id, groupedId->v)); MessageGroupId::FromRaw(history->peer->id, groupedId->v));
} }
if (const auto reactions = data.vreactions()) { if (const auto reactions = data.vreactions()) {
setReactions(reactions); changeReactions(reactions);
} }
applyTTL(data); applyTTL(data);