Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/model/autocomplete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:unorm_dart/unorm_dart.dart' as unorm;

import '../api/model/events.dart';
import '../api/model/model.dart';
import '../api/route/channels.dart';
import '../generated/l10n/zulip_localizations.dart';
import '../widgets/compose_box.dart';
import 'algorithms.dart';
Expand Down Expand Up @@ -1175,10 +1174,8 @@ class TopicAutocompleteView extends AutocompleteView<TopicAutocompleteQuery, Top
Future<void> _fetch() async {
assert(!_isFetching);
_isFetching = true;
final result = await getStreamTopics(store.connection, streamId: streamId,
allowEmptyTopicName: true,
);
_topics = result.topics.map((e) => e.name);
await store.fetchTopics(streamId);
_topics = store.getChannelTopics(streamId)!.map((e) => e.name);
_isFetching = false;
return _startSearch();
}
Expand Down
128 changes: 128 additions & 0 deletions lib/model/channel.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import 'dart:async';
import 'dart:collection';

import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';

import '../api/model/events.dart';
import '../api/model/initial_snapshot.dart';
import '../api/model/model.dart';
import '../api/route/channels.dart';
import 'realm.dart';
import 'store.dart';
import 'user.dart';

// similar to _apiSendMessage in lib/model/message.dart
final _apiGetChannelTopics = getStreamTopics;

/// The portion of [PerAccountStore] for channels, topics, and stuff about them.
///
/// This type is useful for expressing the needs of other parts of the
Expand Down Expand Up @@ -78,6 +84,29 @@ mixin ChannelStore on UserStore {
};
}

/// Fetch topics in a channel from the server, only if they're not fetched yet.
///
/// The results from the last successful fetch
/// can be retrieved with [getChannelTopics].
Future<void> fetchTopics(int channelId);

/// The topics in the given channel, along with their latest message ID.
///
/// Returns null if the data has not been fetched yet.
/// To fetch it from the server, use [fetchTopics].
///
/// The result is sorted by [GetStreamTopicsEntry.maxId] descending,
/// and the topics are distinct.
///
/// Occasionally, [GetStreamTopicsEntry.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 [GetStreamTopicsEntry.maxId] accurately.
/// (We don't keep a snapshot of all messages.)
/// Use [PerAccountStore.messages] to check a message's topic accurately.
List<GetStreamTopicsEntry>? getChannelTopics(int channelId);

/// The visibility policy that the self-user has for the given topic.
///
/// This does not incorporate the user's channel-level policy,
Expand Down Expand Up @@ -288,6 +317,13 @@ mixin ProxyChannelStore on ChannelStore {
@override
Map<int, ChannelFolder> get channelFolders => channelStore.channelFolders;

@override
Future<void> fetchTopics(int channelId) => channelStore.fetchTopics(channelId);

@override
List<GetStreamTopicsEntry>? getChannelTopics(int channelId) =>
channelStore.getChannelTopics(channelId);

@override
UserTopicVisibilityPolicy topicVisibilityPolicy(int streamId, TopicName topic) =>
channelStore.topicVisibilityPolicy(streamId, topic);
Expand Down Expand Up @@ -368,6 +404,39 @@ class ChannelStoreImpl extends HasUserStore with ChannelStore {
@override
final Map<int, ChannelFolder> channelFolders;

/// 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.)
final Map<int, TopicKeyedMap<int>> _latestMessageIdsByChannelTopic = {};

@override
Future<void> fetchTopics(int channelId) async {
if (_latestMessageIdsByChannelTopic[channelId] != null) return;

final result = await _apiGetChannelTopics(connection, streamId: channelId,
allowEmptyTopicName: true);
(_latestMessageIdsByChannelTopic[channelId] = makeTopicKeyedMap()).addAll({
for (final GetStreamTopicsEntry(:name, :maxId) in result.topics)
name: maxId,
});
}

@override
List<GetStreamTopicsEntry>? getChannelTopics(int channelId) {
final latestMessageIdsByTopic = _latestMessageIdsByChannelTopic[channelId];
if (latestMessageIdsByTopic == null) return null;
return [
for (final MapEntry(:key, :value) in latestMessageIdsByTopic.entries)
GetStreamTopicsEntry(maxId: value, name: key),
].sortedBy((value) => -value.maxId);
}
Comment on lines +431 to +438
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a case where we want to maintain a sorted list in the data structure, rather than sorting on demand. @gnprice, do you think so?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think a sorted list would be better, as there could be thousands of topics in a channel. Curious what Greg's thoughts are!


@override
Map<int, TopicKeyedMap<UserTopicVisibilityPolicy>> get debugTopicVisibility => topicVisibility;

Expand Down Expand Up @@ -572,6 +641,65 @@ class ChannelStoreImpl extends HasUserStore with ChannelStore {
forStream[event.topicName] = visibilityPolicy;
}
}

/// Handle a [MessageEvent], returning whether listeners should be notified.
bool handleMessageEvent(MessageEvent event) {
if (event.message is! StreamMessage) return false;
final StreamMessage(streamId: channelId, :topic) = event.message as StreamMessage;

final latestMessageIdsByTopic = _latestMessageIdsByChannelTopic[channelId];
if (latestMessageIdsByTopic == null) {
// We're not tracking this channel's topics yet.
// We start doing that when [fetchTopics] is called,
// and we fill in all the topics at that time.
return false;
}

final currentLatestMessageId = latestMessageIdsByTopic[topic];
if (currentLatestMessageId != null && currentLatestMessageId >= event.message.id) {
// The event raced with a message fetch.
return false;
}
latestMessageIdsByTopic[topic] = event.message.id;
return true;
}

/// Handle an [UpdateMessageEvent], returning whether listeners should be
/// notified.
bool handleUpdateMessageEvent(UpdateMessageEvent event) {
if (event.moveData == null) return false;
final UpdateMessageMoveData(
:origStreamId, :origTopic, :newStreamId, :newTopic, :propagateMode,
) = event.moveData!;
bool shouldNotify = false;

final origLatestMessageIdsByTopics = _latestMessageIdsByChannelTopic[origStreamId];
switch (propagateMode) {
case PropagateMode.changeOne:
case PropagateMode.changeLater:
// 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 PropagateMode.changeAll:
if (origLatestMessageIdsByTopics != null) {
origLatestMessageIdsByTopics.remove(origTopic);
shouldNotify = true;
}
}

final newLatestMessageIdsByTopics = _latestMessageIdsByChannelTopic[newStreamId];
if (newLatestMessageIdsByTopics != null) {
final movedMaxId = event.messageIds.max;
final currentMaxId = newLatestMessageIdsByTopics[newTopic];
if (currentMaxId == null || currentMaxId < movedMaxId) {
newLatestMessageIdsByTopics[newTopic] = movedMaxId;
shouldNotify = true;
}
}

return shouldNotify;
}
}

/// A [Map] with [TopicName] keys and [V] values.
Expand Down
2 changes: 2 additions & 0 deletions lib/model/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -862,13 +862,15 @@ class PerAccountStore extends PerAccountStoreBase with
unreads.handleMessageEvent(event);
recentDmConversationsView.handleMessageEvent(event);
recentSenders.handleMessage(event.message); // TODO(#824)
if (_channels.handleMessageEvent(event)) notifyListeners();
// When adding anything here (to handle [MessageEvent]),
// it probably belongs in [reconcileMessages] too.

case UpdateMessageEvent():
assert(debugLog("server event: update_message ${event.messageId}"));
_messages.handleUpdateMessageEvent(event);
unreads.handleUpdateMessageEvent(event);
if (_channels.handleUpdateMessageEvent(event)) notifyListeners();

case DeleteMessageEvent():
assert(debugLog("server event: delete_message ${event.messageIds}"));
Expand Down
39 changes: 18 additions & 21 deletions lib/widgets/topic_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import '../api/model/model.dart';
import '../api/route/channels.dart';
import '../generated/l10n/zulip_localizations.dart';
import '../model/narrow.dart';
import '../model/store.dart';
import '../model/unreads.dart';
import 'action_sheet.dart';
import 'app_bar.dart';
Expand Down Expand Up @@ -124,16 +125,13 @@ class _TopicList extends StatefulWidget {

class _TopicListState extends State<_TopicList> with PerAccountStoreAwareStateMixin {
Unreads? unreadsModel;
// TODO(#1499): store the results on [ChannelStore], and keep them
// up-to-date by handling events
List<GetStreamTopicsEntry>? lastFetchedTopics;

@override
void onNewStore() {
unreadsModel?.removeListener(_modelChanged);
final store = PerAccountStoreWidget.of(context);
unreadsModel = store.unreads..addListener(_modelChanged);
_fetchTopics();
_fetchTopics(store);
}

@override
Expand All @@ -148,31 +146,30 @@ class _TopicListState extends State<_TopicList> with PerAccountStoreAwareStateMi
});
}

void _fetchTopics() async {
void _fetchTopics(PerAccountStore store) async {
// Do nothing when the fetch fails; the topic-list will stay on
// the loading screen, until the user navigates away and back.
// TODO(design) show a nice error message on screen when this fails
final store = PerAccountStoreWidget.of(context);
final result = await getStreamTopics(store.connection,
streamId: widget.streamId,
allowEmptyTopicName: true);
await store.fetchTopics(widget.streamId);
if (!mounted) return;
setState(() {
lastFetchedTopics = result.topics;
// The actual state lives in the PerAccountStore.
});
}

@override
Widget build(BuildContext context) {
if (lastFetchedTopics == null) {
final store = PerAccountStoreWidget.of(context);
final channelTopics = store.getChannelTopics(widget.streamId);
if (channelTopics == null) {
return const Center(child: CircularProgressIndicator());
}

// TODO(design) handle the rare case when `lastFetchedTopics` is empty

// This is adapted from parts of the build method on [_InboxPageState].
final topicItems = <_TopicItemData>[];
for (final GetStreamTopicsEntry(:maxId, name: topic) in lastFetchedTopics!) {
for (final GetStreamTopicsEntry(:maxId, name: topic) in channelTopics) {
final unreadMessageIds =
unreadsModel!.streams[widget.streamId]?[topic] ?? <int>[];
final countInTopic = unreadMessageIds.length;
Expand All @@ -182,17 +179,9 @@ class _TopicListState extends State<_TopicList> with PerAccountStoreAwareStateMi
topic: topic,
unreadCount: countInTopic,
hasMention: hasMention,
// `lastFetchedTopics.maxId` can become outdated when a new message
// arrives or when there are message moves, until we re-fetch.
// TODO(#1499): track changes to this
maxId: maxId,
));
}
topicItems.sort((a, b) {
final aMaxId = a.maxId;
final bMaxId = b.maxId;
return bMaxId.compareTo(aMaxId);
});

return SafeArea(
// Don't pad the bottom here; we want the list content to do that.
Expand Down Expand Up @@ -234,6 +223,14 @@ class _TopicItem extends StatelessWidget {

final store = PerAccountStoreWidget.of(context);
final designVariables = DesignVariables.of(context);
// The message with `maxId` might not remain in `topic` since we last fetch
// the list of topics. Make sure we check for that before passing `maxId`
// to the topic action sheet.
// See also: [ChannelStore.getChannelTopics]
final message = store.messages[maxId];
final isMaxIdInTopic = message is StreamMessage
&& message.streamId == streamId
&& message.topic.isSameAs(topic);

final visibilityPolicy = store.topicVisibilityPolicy(streamId, topic);
final double opacity;
Expand Down Expand Up @@ -262,7 +259,7 @@ class _TopicItem extends StatelessWidget {
onLongPress: () => showTopicActionSheet(context,
channelId: streamId,
topic: topic,
someMessageIdInTopic: maxId),
someMessageIdInTopic: isMaxIdInTopic ? maxId : null),
splashFactory: NoSplash.splashFactory,
child: Padding(padding: EdgeInsetsDirectional.fromSTEB(6, 8, 12, 8),
child: Row(
Expand Down
7 changes: 7 additions & 0 deletions test/api/route/route_checks.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:checks/checks.dart';
import 'package:zulip/api/model/model.dart';
import 'package:zulip/api/route/channels.dart';
import 'package:zulip/api/route/messages.dart';
import 'package:zulip/api/route/realm.dart';
import 'package:zulip/api/route/saved_snippets.dart';
Expand All @@ -15,4 +17,9 @@ extension GetServerSettingsResultChecks on Subject<GetServerSettingsResult> {
Subject<Uri> get realmUrl => has((e) => e.realmUrl, 'realmUrl');
}

extension GetStreamTopicEntryChecks on Subject<GetStreamTopicsEntry> {
Subject<int> get maxId => has((e) => e.maxId, 'maxId');
Subject<TopicName> get name => has((e) => e.name, 'name');
}

// TODO add similar extensions for other classes in api/route/*.dart
Loading