From f828caf0d9858ce9c18bd212de77ab7d3bcb931d Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 12 Jun 2023 18:04:10 +0400 Subject: [PATCH] Simplify saved stories list. --- Telegram/SourceFiles/data/data_stories.cpp | 43 ---------- .../SourceFiles/data/data_stories_ids.cpp | 83 ++++--------------- 2 files changed, 17 insertions(+), 109 deletions(-) diff --git a/Telegram/SourceFiles/data/data_stories.cpp b/Telegram/SourceFiles/data/data_stories.cpp index c0f70b925..e737e347e 100644 --- a/Telegram/SourceFiles/data/data_stories.cpp +++ b/Telegram/SourceFiles/data/data_stories.cpp @@ -362,49 +362,6 @@ void Stories::apply(not_null peer, const MTPUserStories *data) { if (!data) { applyDeletedFromSources(peer->id, StorySourcesList::All); _all.erase(peer->id); - const auto i = _stories.find(peer->id); - if (i != end(_stories)) { - auto stories = base::take(i->second); - _stories.erase(i); - - auto archiveChanged = false; - auto savedChanged = false; - if (peer->isSelf()) { - for (const auto &[id, story] : stories) { - if (_archive.list.remove(id)) { - archiveChanged = true; - if (_archiveTotal > 0) { - --_archiveTotal; - } - } - } - } - const auto j = _saved.find(peer->id); - const auto saved = (j != end(_saved)) ? &j->second : nullptr; - for (const auto &[id, story] : stories) { - // Duplicated in Stories::applyDeleted. - _deleted.emplace(FullStoryId{ peer->id, id }); - _expiring.remove(story->expires(), story->fullId()); - if (story->pinned() && saved) { - if (saved->ids.list.remove(id)) { - savedChanged = true; - if (saved->total > 0) { - --saved->total; - } - } - } - session().changes().storyUpdated( - story.get(), - UpdateFlag::Destroyed); - removeDependencyStory(story.get()); - } - if (archiveChanged) { - _archiveChanged.fire({}); - } - if (savedChanged) { - _savedChanged.fire_copy(peer->id); - } - } _sourceChanged.fire_copy(peer->id); } else { parseAndApply(*data); diff --git a/Telegram/SourceFiles/data/data_stories_ids.cpp b/Telegram/SourceFiles/data/data_stories_ids.cpp index 21d0878a6..7506abea5 100644 --- a/Telegram/SourceFiles/data/data_stories_ids.cpp +++ b/Telegram/SourceFiles/data/data_stories_ids.cpp @@ -38,70 +38,26 @@ rpl::producer SavedStoriesIds( } const auto saved = stories->saved(peer->id); + Assert(saved != nullptr); const auto count = stories->savedCount(peer->id); const auto around = saved->list.lower_bound(aroundId); - Assert(saved != nullptr); - const auto source = stories->source(peer->id); - if (!source || source->ids.empty()) { - const auto hasBefore = int(around - begin(saved->list)); - const auto hasAfter = int(end(saved->list) - around); - if (hasAfter < limit) { - stories->savedLoadMore(peer->id); - } - const auto takeBefore = std::min(hasBefore, limit); - const auto takeAfter = std::min(hasAfter, limit); - auto ids = base::flat_set{ - std::make_reverse_iterator(around + takeAfter), - std::make_reverse_iterator(around - takeBefore) - }; - const auto added = int(ids.size()); - state->slice = StoriesIdsSlice( - std::move(ids), - count, - (hasBefore - takeBefore), - count - hasBefore - added); - } else { - auto ids = base::flat_set(); - auto added = 0; - auto skipped = 0; - auto skippedBefore = (around - begin(saved->list)); - auto skippedAfter = (end(saved->list) - around); - const auto &active = source->ids; - const auto process = [&](StoryId id) { - const auto i = active.lower_bound(StoryIdDates{ id }); - if (i == end(active) || i->id != id) { - ids.emplace(id); - ++added; - } else { - ++skipped; - } - return (added < limit); - }; - ids.reserve(2 * limit + 1); - for (auto i = around, b = begin(saved->list); i != b;) { - --skippedBefore; - if (!process(*--i)) { - break; - } - } - if (ids.size() < limit) { - ids.emplace(active.back().id); // #TODO stories fake max story id - } else { - ++skippedBefore; - } - added = 0; - for (auto i = around, e = end(saved->list); i != e; ++i) { - --skippedAfter; - if (!process(*i)) { - break; - } - } - state->slice = StoriesIdsSlice( - std::move(ids), - count - skipped + 1, - skippedBefore, - skippedAfter); + const auto hasBefore = int(around - begin(saved->list)); + const auto hasAfter = int(end(saved->list) - around); + if (hasAfter < limit) { + stories->savedLoadMore(peer->id); } + const auto takeBefore = std::min(hasBefore, limit); + const auto takeAfter = std::min(hasAfter, limit); + auto ids = base::flat_set{ + std::make_reverse_iterator(around + takeAfter), + std::make_reverse_iterator(around - takeBefore) + }; + const auto added = int(ids.size()); + state->slice = StoriesIdsSlice( + std::move(ids), + count, + (hasBefore - takeBefore), + count - hasBefore - added); consumer.put_next_copy(state->slice); }; const auto schedule = [=] { @@ -117,11 +73,6 @@ rpl::producer SavedStoriesIds( }; const auto stories = &peer->owner().stories(); - stories->sourceChanged( - ) | rpl::filter( - rpl::mappers::_1 == peer->id - ) | rpl::start_with_next(schedule, lifetime); - stories->savedChanged( ) | rpl::filter( rpl::mappers::_1 == peer->id