From 9f548b523e6cb4e52b1bc937bb0fa305a58d048c Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 26 May 2023 18:48:33 +0400 Subject: [PATCH] Apply updates correctly. --- Telegram/SourceFiles/data/data_stories.cpp | 47 +++++++++++++++++-- Telegram/SourceFiles/data/data_stories.h | 3 +- Telegram/SourceFiles/history/history_item.cpp | 12 +++++ Telegram/SourceFiles/history/history_item.h | 2 + .../history/history_item_components.cpp | 20 +++++++- .../history/history_item_components.h | 7 ++- 6 files changed, 82 insertions(+), 9 deletions(-) diff --git a/Telegram/SourceFiles/data/data_stories.cpp b/Telegram/SourceFiles/data/data_stories.cpp index 2ee3be031..3f12fd6fe 100644 --- a/Telegram/SourceFiles/data/data_stories.cpp +++ b/Telegram/SourceFiles/data/data_stories.cpp @@ -166,7 +166,7 @@ Main::Session &Stories::session() const { } void Stories::apply(const MTPDupdateStories &data) { - pushToFront(parse(data.vstories())); + applyChanges(parse(data.vstories())); _allChanged.fire({}); } @@ -402,14 +402,42 @@ void Stories::applyDeleted(FullStoryId id) { session().changes().storyUpdated( story.get(), UpdateFlag::Destroyed); + removeDependencyStory(story.get()); if (i->second.empty()) { _stories.erase(i); } } } + const auto j = ranges::find(_all, id.peer, [](const StoriesList &list) { + return list.user->id; + }); + if (j != end(_all)) { + const auto till = ranges::remove(j->ids, id.story); + const auto removed = int(std::distance(till, end(j->ids))); + if (till != end(j->ids)) { + j->ids.erase(till, end(j->ids)); + j->total = std::max(j->total - removed, 0); + if (j->ids.empty()) { + _all.erase(j); + } + _allChanged.fire({}); + } + } _deleted.emplace(id); } +void Stories::removeDependencyStory(not_null story) { + const auto i = _dependentMessages.find(story); + if (i != end(_dependentMessages)) { + const auto items = std::move(i->second); + _dependentMessages.erase(i); + + for (const auto &dependent : items) { + dependent->dependencyStoryRemoved(story); + } + } +} + const std::vector &Stories::all() { return _all; } @@ -465,12 +493,21 @@ void Stories::pushToBack(StoriesList &&list) { } } -void Stories::pushToFront(StoriesList &&list) { +void Stories::applyChanges(StoriesList &&list) { const auto i = ranges::find(_all, list.user, &StoriesList::user); if (i != end(_all)) { - *i = std::move(list); - ranges::rotate(begin(_all), i, i + 1); - } else { + auto added = false; + for (const auto id : list.ids) { + if (!ranges::contains(i->ids, id)) { + i->ids.insert(begin(i->ids), id); + ++i->total; + added = true; + } + } + if (added) { + ranges::rotate(begin(_all), i, i + 1); + } + } else if (!list.ids.empty()) { _all.insert(begin(_all), std::move(list)); } } diff --git a/Telegram/SourceFiles/data/data_stories.h b/Telegram/SourceFiles/data/data_stories.h index 641e26edf..979cba19b 100644 --- a/Telegram/SourceFiles/data/data_stories.h +++ b/Telegram/SourceFiles/data/data_stories.h @@ -123,8 +123,9 @@ private: void finalizeResolve(FullStoryId id); void pushToBack(StoriesList &&list); - void pushToFront(StoriesList &&list); + void applyChanges(StoriesList &&list); void applyDeleted(FullStoryId id); + void removeDependencyStory(not_null story); const not_null _owner; base::flat_map< diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index c0e60d38b..1a99b7024 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -725,6 +725,18 @@ void HistoryItem::dependencyItemRemoved(not_null dependency) { } } +void HistoryItem::dependencyStoryRemoved( + not_null dependency) { + if (const auto reply = Get()) { + const auto documentId = reply->replyToDocumentId; + reply->storyRemoved(this, dependency); + if (documentId != reply->replyToDocumentId + && generateLocalEntitiesByReply()) { + _history->owner().requestItemTextRefresh(this); + } + } +} + void HistoryItem::updateDependencyItem() { if (const auto reply = Get()) { const auto documentId = reply->replyToDocumentId; diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index 7ef66d7d6..bbc074879 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -57,6 +57,7 @@ class MessageReactions; class ForumTopic; class Thread; struct SponsoredFrom; +class Story; } // namespace Data namespace Main { @@ -185,6 +186,7 @@ public: }; void dependencyItemRemoved(not_null dependency); + void dependencyStoryRemoved(not_null dependency); void updateDependencyItem(); [[nodiscard]] MsgId dependencyMsgId() const; [[nodiscard]] bool notificationReady() const; diff --git a/Telegram/SourceFiles/history/history_item_components.cpp b/Telegram/SourceFiles/history/history_item_components.cpp index 4b5284555..ce9a8edf2 100644 --- a/Telegram/SourceFiles/history/history_item_components.cpp +++ b/Telegram/SourceFiles/history/history_item_components.cpp @@ -369,7 +369,14 @@ void HistoryMessageReply::clearData(not_null holder) { replyToMsg.get()); replyToMsg = nullptr; } + if (replyToStory) { + holder->history()->owner().stories().unregisterDependentMessage( + holder, + replyToStory.get()); + replyToStory = nullptr; + } replyToMsgId = 0; + replyToStoryId = 0; refreshReplyToMedia(); } @@ -466,14 +473,23 @@ void HistoryMessageReply::resize(int width) const { } void HistoryMessageReply::itemRemoved( - HistoryItem *holder, - HistoryItem *removed) { + not_null holder, + not_null removed) { if (replyToMsg.get() == removed) { clearData(holder); holder->history()->owner().requestItemResize(holder); } } +void HistoryMessageReply::storyRemoved( + not_null holder, + not_null removed) { + if (replyToStory.get() == removed) { + clearData(holder); + holder->history()->owner().requestItemResize(holder); + } +} + void HistoryMessageReply::paint( Painter &p, not_null holder, diff --git a/Telegram/SourceFiles/history/history_item_components.h b/Telegram/SourceFiles/history/history_item_components.h index 32d1d43f9..99100f6b0 100644 --- a/Telegram/SourceFiles/history/history_item_components.h +++ b/Telegram/SourceFiles/history/history_item_components.h @@ -249,7 +249,12 @@ struct HistoryMessageReply [[nodiscard]] bool isNameUpdated(not_null holder) const; void updateName(not_null holder) const; void resize(int width) const; - void itemRemoved(HistoryItem *holder, HistoryItem *removed); + void itemRemoved( + not_null holder, + not_null removed); + void storyRemoved( + not_null holder, + not_null removed); void paint( Painter &p,