-
Notifications
You must be signed in to change notification settings - Fork 371
Keep track of the known topics in store #1951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sm-sayedi
wants to merge
11
commits into
zulip:main
Choose a base branch
from
sm-sayedi:1499-track-topics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3d151ed
narrow: Use TopicName.isSameAs in TopicNarrow.containsMessage
sm-sayedi 093e866
api [nfc]: Rename GetStreamTopicsEntry to GetChannelTopicsEntry
sm-sayedi a3ff1fd
example data [nfc]: Rename getStreamTopicsEntry to getChannelTopicsEntry
sm-sayedi 81efe80
api [nfc]: Rename GetStreamTopicsResult to GetChannelTopicsResult
sm-sayedi 6c5727a
api [nfc]: Rename getStreamTopics to getChannelTopics
sm-sayedi dfa349f
topics: Introduce `Topics` model for tracking known channel topics
sm-sayedi ad8e5d0
autocomplete test [nfc]: s/streams/topics in a topic test name
sm-sayedi d7ec65a
autocomplete test [nfc]: Add and use isTopic condition
sm-sayedi b7407ff
autocomplete: Use topic data from `Topics` model in topic autocomplete
sm-sayedi 9730b1f
topics: Pass `maxId` to topic sheet if the related message is still i…
sm-sayedi 400b8cf
topics: Keep topic-list page updated, by using data from `Topics` model
sm-sayedi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:collection/collection.dart'; | ||
| import 'package:flutter/foundation.dart'; | ||
|
|
||
| import '../api/model/events.dart'; | ||
| import '../api/model/model.dart'; | ||
| import '../api/route/channels.dart'; | ||
| import 'channel.dart'; | ||
| import 'store.dart'; | ||
|
|
||
| // similar to _apiSendMessage in lib/model/message.dart | ||
| final _apiGetChannelTopics = getChannelTopics; | ||
|
|
||
| /// The view-model for tracking channel topics. | ||
| /// | ||
| /// Use [fetchChannelTopics] to first fetch the topics for a channel from the | ||
| /// server and then use [getChannelTopics] to get those topics, affected | ||
| /// by the relevant events. | ||
| class Topics extends PerAccountStoreBase with ChangeNotifier { | ||
| Topics({required super.core}); | ||
|
|
||
| /// Maps indexed by channel IDs, of the known latest message IDs in each topic. | ||
| /// | ||
| /// For example: `_latestMessageIdsByChannelTopic[channel.streamId][topic] = maxId` | ||
| /// | ||
| /// Occasionally, the latest message ID of a topic will refer to a message | ||
| /// that doesn't exist or is no longer in the topic. | ||
| /// This happens when the topic's latest message is deleted or moved | ||
| /// and we don't have enough information to replace it accurately. | ||
| /// (We don't keep a snapshot of all messages.) | ||
| // TODO(#2004): handle more cases where this can change | ||
| final Map<int, TopicKeyedMap<int>> _latestMessageIdsByChannelTopic = {}; | ||
|
|
||
| final Map<int, Future<GetChannelTopicsResult>> _channelTopicsFetching = {}; | ||
|
|
||
| /// Fetch topics in a channel from the server, only if they're not fetched yet. | ||
| /// | ||
| /// Once fetched, the data will be updated by events; | ||
| /// use [getChannelTopics] to consume the data. | ||
| Future<void> fetchChannelTopics(int channelId) async { | ||
| if (_latestMessageIdsByChannelTopic[channelId] != null) return; | ||
|
|
||
| Future<GetChannelTopicsResult>? future = _channelTopicsFetching[channelId]; | ||
| // If another call has already started fetching topics for this channel, | ||
| // ignore this call. | ||
| if (future != null) return; | ||
|
|
||
| future = _apiGetChannelTopics(connection, channelId: channelId, | ||
| allowEmptyTopicName: true); | ||
| _channelTopicsFetching[channelId] = future; | ||
|
|
||
| try { | ||
| final result = await future; | ||
| assert(_latestMessageIdsByChannelTopic[channelId] == null); | ||
| (_latestMessageIdsByChannelTopic[channelId] = makeTopicKeyedMap()) | ||
| .addEntries(result.topics.map((entry) => MapEntry(entry.name, entry.maxId))); | ||
| } finally { | ||
| unawaited(_channelTopicsFetching.remove(channelId)); | ||
| } | ||
| } | ||
|
|
||
| /// Map of topics per channel, sorted by latest message IDs descending. | ||
| /// | ||
| /// Derived from [_latestMessageIdsByChannelTopic]; | ||
| /// used for optimized topics sorting. | ||
| /// | ||
| /// A channel entry should be discarded when there is a change to | ||
| /// the ordering of its topics or the channel is no longer present | ||
| /// in [_latestMessageIdsByChannelTopic], allowing [getChannelTopics] | ||
| /// to recalculate the sorted topics on demand. | ||
| // TODO(#2004): handle more cases where this can change | ||
| final Map<int, List<GetChannelTopicsEntry>> _sortedTopicsByChannel = {}; | ||
|
|
||
| /// The topics the user can access, along with their latest message ID, | ||
| /// reflecting updates from events that arrived since the data was fetched. | ||
| /// | ||
| /// Returns null if the data has not been fetched yet. | ||
| /// To fetch it from the server, use [fetchChannelTopics]. | ||
| /// | ||
| /// The result is sorted by [GetChannelTopicsEntry.maxId] descending, | ||
| /// and the topics are distinct. | ||
| /// | ||
| /// Occasionally, [GetChannelTopicsEntry.maxId] will refer to a message | ||
| /// that doesn't exist or is no longer in the topic. | ||
| /// This happens when a topic's latest message is deleted or moved | ||
| /// and we don't have enough information | ||
| /// to replace [GetChannelTopicsEntry.maxId] accurately. | ||
| /// (We don't keep a snapshot of all messages.) | ||
| /// Use [PerAccountStore.messages] to check a message's topic accurately. | ||
| List<GetChannelTopicsEntry>? getChannelTopics(int channelId) { | ||
| final latestMessageIdsByTopic = _latestMessageIdsByChannelTopic[channelId]; | ||
| if (latestMessageIdsByTopic == null) return null; | ||
| return _sortedTopicsByChannel[channelId] ??= latestMessageIdsByTopic.entries | ||
| .map((e) => GetChannelTopicsEntry(maxId: e.value, name: e.key)) | ||
| .sortedBy((value) => -value.maxId); | ||
| } | ||
|
|
||
| void handleMessageEvent(MessageEvent event) { | ||
| if (event.message is! StreamMessage) return; | ||
| final StreamMessage(:id, streamId: channelId, :topic) = event.message as StreamMessage; | ||
|
|
||
| final latestMessageIdsByTopic = _latestMessageIdsByChannelTopic[channelId]; | ||
| if (latestMessageIdsByTopic == null) { | ||
| // We're not tracking this channel's topics yet. | ||
| // We'll start doing that when we get the full topic list; | ||
| // see [fetchChannelTopics]. | ||
| return; | ||
| } | ||
|
|
||
| final currentLatestMessageId = latestMessageIdsByTopic[topic]; | ||
| if (currentLatestMessageId != null && currentLatestMessageId >= id) { | ||
| // The event raced with a topic-list fetch. | ||
| return; | ||
| } | ||
| latestMessageIdsByTopic[topic] = id; | ||
| _sortedTopicsByChannel.remove(channelId); | ||
| notifyListeners(); | ||
| } | ||
|
|
||
| void handleUpdateMessageEvent(UpdateMessageEvent event) { | ||
| if (event.moveData == null) return; | ||
| final UpdateMessageMoveData( | ||
| :origStreamId, :origTopic, :newStreamId, :newTopic, :propagateMode, | ||
| ) = event.moveData!; | ||
| bool shouldNotify = false; | ||
|
|
||
| final origLatestMessageIdsByTopic = _latestMessageIdsByChannelTopic[origStreamId]; | ||
| if (origLatestMessageIdsByTopic != null) { | ||
| switch (propagateMode) { | ||
| case .changeOne: | ||
| case .changeLater: | ||
|
Comment on lines
+130
to
+132
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the other hand, the shorthand as it's used here looks great to me! |
||
| // We can't know the new `maxId` for the original topic. | ||
| // Shrug; leave it unchanged. (See dartdoc of [getChannelTopics], | ||
| // where we call out this possibility that `maxId` is incorrect.) | ||
| break; | ||
| case .changeAll: | ||
| origLatestMessageIdsByTopic.remove(origTopic); | ||
| _sortedTopicsByChannel.remove(origStreamId); | ||
| shouldNotify = true; | ||
| } | ||
| } | ||
|
|
||
| final newLatestMessageIdsByTopic = _latestMessageIdsByChannelTopic[newStreamId]; | ||
| if (newLatestMessageIdsByTopic != null) { | ||
| // TODO(server-11): rely on `event.messageIds` being sorted, to avoid this linear scan | ||
| final movedMaxId = event.messageIds.max; | ||
| final currentMaxId = newLatestMessageIdsByTopic[newTopic]; | ||
| if (currentMaxId == null || currentMaxId < movedMaxId) { | ||
| newLatestMessageIdsByTopic[newTopic] = movedMaxId; | ||
| _sortedTopicsByChannel.remove(newStreamId); | ||
| shouldNotify = true; | ||
| } | ||
| } | ||
|
|
||
| if (shouldNotify) notifyListeners(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, good catch.
Did this have a user-visible symptom? What were the symptoms?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, there is one: when looking at a topic narrow for a topic named "t", and then there's a new message event in the topic named "T", it will not be shown in the current view.
Will mention this in the commit message.