Simplify saved stories list.

This commit is contained in:
John Preston 2023-06-12 18:04:10 +04:00
parent 0401364d71
commit f828caf0d9
2 changed files with 17 additions and 109 deletions

View File

@ -362,49 +362,6 @@ void Stories::apply(not_null<PeerData*> 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);

View File

@ -38,70 +38,26 @@ rpl::producer<StoriesIdsSlice> 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<StoryId>{
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<StoryId>();
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<StoryId>{
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<StoriesIdsSlice> 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