From e667436a98cb9c766a3b24cf23d53af7c96095f0 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 4 Jan 2024 12:08:49 +0400 Subject: [PATCH] Track my tags usages. --- .../data/data_message_reactions.cpp | 57 +++++++++++++++++++ .../SourceFiles/data/data_message_reactions.h | 4 ++ 2 files changed, 61 insertions(+) diff --git a/Telegram/SourceFiles/data/data_message_reactions.cpp b/Telegram/SourceFiles/data/data_message_reactions.cpp index b6eadde69..9e1726b7a 100644 --- a/Telegram/SourceFiles/data/data_message_reactions.cpp +++ b/Telegram/SourceFiles/data/data_message_reactions.cpp @@ -378,6 +378,56 @@ void Reactions::setFavorite(const ReactionId &id) { applyFavorite(id); } +void Reactions::incrementMyTag(const ReactionId &id) { + auto i = ranges::find(_myTagsInfo, id, &MyTagInfo::id); + if (i == end(_myTagsInfo)) { + _myTagsInfo.push_back({ .id = id, .count = 0 }); + i = end(_myTagsInfo) - 1; + } + ++i->count; + while (i != begin(_myTagsInfo)) { + auto j = i - 1; + if (j->count >= i->count) { + break; + } + std::swap(*i, *j); + i = j; + } + scheduleMyTagsUpdate(); +} + +void Reactions::decrementMyTag(const ReactionId &id) { + auto i = ranges::find(_myTagsInfo, id, &MyTagInfo::id); + if (i->count <= 0) { + return; + } + --i->count; + while (i + 1 != end(_myTagsInfo)) { + auto j = i + 1; + if (j->count <= i->count) { + break; + } + std::swap(*i, *j); + i = j; + } + scheduleMyTagsUpdate(); +} + +void Reactions::scheduleMyTagsUpdate() { + _myTagsUpdateScheduled = true; + crl::on_main(&session(), [=] { + if (!_myTagsUpdateScheduled) { + return; + } + _myTagsUpdateScheduled = false; + _myTagsIds = _myTagsInfo | ranges::views::transform( + &MyTagInfo::id + ) | ranges::to_vector; + _myTags = resolveByIds(_myTagsIds, _unresolvedMyTags); + _myTagsUpdated.fire({}); + }); +} + DocumentData *Reactions::chooseGenericAnimation( not_null custom) const { const auto sticker = custom->sticker(); @@ -1155,6 +1205,10 @@ void MessageReactions::add(const ReactionId &id, bool addToRecent) { return; } auto my = 0; + const auto tags = _item->reactionsAreTags(); + if (tags) { + history->owner().reactions().incrementMyTag(id); + } _list.erase(ranges::remove_if(_list, [&](MessageReaction &one) { const auto removing = one.my && (my == myLimit || ++my == myLimit); if (!removing) { @@ -1176,6 +1230,9 @@ void MessageReactions::add(const ReactionId &id, bool addToRecent) { } } } + if (tags) { + history->owner().reactions().decrementMyTag(one.id); + } return removed; }), end(_list)); const auto peer = history->peer; diff --git a/Telegram/SourceFiles/data/data_message_reactions.h b/Telegram/SourceFiles/data/data_message_reactions.h index e43538a11..2743bffc8 100644 --- a/Telegram/SourceFiles/data/data_message_reactions.h +++ b/Telegram/SourceFiles/data/data_message_reactions.h @@ -93,6 +93,8 @@ public: [[nodiscard]] ReactionId favoriteId() const; [[nodiscard]] const Reaction *favorite() const; void setFavorite(const ReactionId &id); + void incrementMyTag(const ReactionId &id); + void decrementMyTag(const ReactionId &id); [[nodiscard]] DocumentData *chooseGenericAnimation( not_null custom) const; @@ -165,6 +167,7 @@ private: base::flat_set &unresolved); void resolve(const ReactionId &id); void applyFavorite(const ReactionId &id); + void scheduleMyTagsUpdate(); [[nodiscard]] std::optional parse( const MTPAvailableReaction &entry); @@ -234,6 +237,7 @@ private: mtpRequestId _myTagsRequestId = 0; bool _myTagsRequestScheduled = false; + bool _myTagsUpdateScheduled = false; uint64 _myTagsHash = 0; mtpRequestId _tagsRequestId = 0;