Skip to content

Keep topics data up-to-date by handling events #1508

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
Binary file modified assets/icons/ZulipIcons.ttf
Binary file not shown.
5 changes: 5 additions & 0 deletions assets/icons/chevron_down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/icons/topics.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions assets/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
"@actionSheetOptionMarkChannelAsRead": {
"description": "Label for marking a channel as read."
},
"actionSheetOptionTopicList": "Topic list",
"@actionSheetOptionTopicList": {
"description": "Label for navigating to a channel's topic-list page."
},
"actionSheetOptionMuteTopic": "Mute topic",
"@actionSheetOptionMuteTopic": {
"description": "Label for muting a topic on action sheet."
Expand Down Expand Up @@ -713,6 +717,10 @@
"@mainMenuMyProfile": {
"description": "Label for main-menu button leading to the user's own profile."
},
"topicsButtonLabel": "TOPICS",
"@topicsButtonLabel": {
"description": "Label for message list button leading to topic-list page. (Use ALL CAPS for cased alphabets: Latin, Greek, Cyrillic, etc.)"
},
"channelFeedButtonTooltip": "Channel feed",
"@channelFeedButtonTooltip": {
"description": "Tooltip for button to navigate to a given channel's feed"
Expand Down
12 changes: 12 additions & 0 deletions lib/generated/l10n/zulip_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ abstract class ZulipLocalizations {
/// **'Mark channel as read'**
String get actionSheetOptionMarkChannelAsRead;

/// Label for navigating to a channel's topic-list page.
///
/// In en, this message translates to:
/// **'Topic list'**
String get actionSheetOptionTopicList;

/// Label for muting a topic on action sheet.
///
/// In en, this message translates to:
Expand Down Expand Up @@ -1064,6 +1070,12 @@ abstract class ZulipLocalizations {
/// **'My profile'**
String get mainMenuMyProfile;

/// Label for message list button leading to topic-list page. (Use ALL CAPS for cased alphabets: Latin, Greek, Cyrillic, etc.)
///
/// In en, this message translates to:
/// **'TOPICS'**
String get topicsButtonLabel;

/// Tooltip for button to navigate to a given channel's feed
///
/// In en, this message translates to:
Expand Down
6 changes: 6 additions & 0 deletions lib/generated/l10n/zulip_localizations_ar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class ZulipLocalizationsAr extends ZulipLocalizations {
@override
String get actionSheetOptionMarkChannelAsRead => 'Mark channel as read';

@override
String get actionSheetOptionTopicList => 'Topic list';

@override
String get actionSheetOptionMuteTopic => 'Mute topic';

Expand Down Expand Up @@ -584,6 +587,9 @@ class ZulipLocalizationsAr extends ZulipLocalizations {
@override
String get mainMenuMyProfile => 'My profile';

@override
String get topicsButtonLabel => 'TOPICS';

@override
String get channelFeedButtonTooltip => 'Channel feed';

Expand Down
6 changes: 6 additions & 0 deletions lib/generated/l10n/zulip_localizations_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class ZulipLocalizationsEn extends ZulipLocalizations {
@override
String get actionSheetOptionMarkChannelAsRead => 'Mark channel as read';

@override
String get actionSheetOptionTopicList => 'Topic list';

@override
String get actionSheetOptionMuteTopic => 'Mute topic';

Expand Down Expand Up @@ -584,6 +587,9 @@ class ZulipLocalizationsEn extends ZulipLocalizations {
@override
String get mainMenuMyProfile => 'My profile';

@override
String get topicsButtonLabel => 'TOPICS';

@override
String get channelFeedButtonTooltip => 'Channel feed';

Expand Down
6 changes: 6 additions & 0 deletions lib/generated/l10n/zulip_localizations_ja.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class ZulipLocalizationsJa extends ZulipLocalizations {
@override
String get actionSheetOptionMarkChannelAsRead => 'Mark channel as read';

@override
String get actionSheetOptionTopicList => 'Topic list';

@override
String get actionSheetOptionMuteTopic => 'Mute topic';

Expand Down Expand Up @@ -584,6 +587,9 @@ class ZulipLocalizationsJa extends ZulipLocalizations {
@override
String get mainMenuMyProfile => 'My profile';

@override
String get topicsButtonLabel => 'TOPICS';

@override
String get channelFeedButtonTooltip => 'Channel feed';

Expand Down
6 changes: 6 additions & 0 deletions lib/generated/l10n/zulip_localizations_nb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class ZulipLocalizationsNb extends ZulipLocalizations {
@override
String get actionSheetOptionMarkChannelAsRead => 'Mark channel as read';

@override
String get actionSheetOptionTopicList => 'Topic list';

@override
String get actionSheetOptionMuteTopic => 'Mute topic';

Expand Down Expand Up @@ -584,6 +587,9 @@ class ZulipLocalizationsNb extends ZulipLocalizations {
@override
String get mainMenuMyProfile => 'My profile';

@override
String get topicsButtonLabel => 'TOPICS';

@override
String get channelFeedButtonTooltip => 'Channel feed';

Expand Down
6 changes: 6 additions & 0 deletions lib/generated/l10n/zulip_localizations_pl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class ZulipLocalizationsPl extends ZulipLocalizations {
String get actionSheetOptionMarkChannelAsRead =>
'Oznacz kanał jako przeczytany';

@override
String get actionSheetOptionTopicList => 'Topic list';

@override
String get actionSheetOptionMuteTopic => 'Wycisz wątek';

Expand Down Expand Up @@ -593,6 +596,9 @@ class ZulipLocalizationsPl extends ZulipLocalizations {
@override
String get mainMenuMyProfile => 'Mój profil';

@override
String get topicsButtonLabel => 'TOPICS';

@override
String get channelFeedButtonTooltip => 'Strumień kanału';

Expand Down
6 changes: 6 additions & 0 deletions lib/generated/l10n/zulip_localizations_ru.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class ZulipLocalizationsRu extends ZulipLocalizations {
String get actionSheetOptionMarkChannelAsRead =>
'Отметить канал как прочитанный';

@override
String get actionSheetOptionTopicList => 'Topic list';

@override
String get actionSheetOptionMuteTopic => 'Отключить тему';

Expand Down Expand Up @@ -597,6 +600,9 @@ class ZulipLocalizationsRu extends ZulipLocalizations {
@override
String get mainMenuMyProfile => 'Мой профиль';

@override
String get topicsButtonLabel => 'TOPICS';

@override
String get channelFeedButtonTooltip => 'Лента канала';

Expand Down
6 changes: 6 additions & 0 deletions lib/generated/l10n/zulip_localizations_sk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class ZulipLocalizationsSk extends ZulipLocalizations {
@override
String get actionSheetOptionMarkChannelAsRead => 'Mark channel as read';

@override
String get actionSheetOptionTopicList => 'Topic list';

@override
String get actionSheetOptionMuteTopic => 'Stlmiť tému';

Expand Down Expand Up @@ -586,6 +589,9 @@ class ZulipLocalizationsSk extends ZulipLocalizations {
@override
String get mainMenuMyProfile => 'Môj profil';

@override
String get topicsButtonLabel => 'TOPICS';

@override
String get channelFeedButtonTooltip => 'Channel feed';

Expand Down
6 changes: 6 additions & 0 deletions lib/generated/l10n/zulip_localizations_uk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class ZulipLocalizationsUk extends ZulipLocalizations {
String get actionSheetOptionMarkChannelAsRead =>
'Позначити канал як прочитаний';

@override
String get actionSheetOptionTopicList => 'Topic list';

@override
String get actionSheetOptionMuteTopic => 'Заглушити тему';

Expand Down Expand Up @@ -596,6 +599,9 @@ class ZulipLocalizationsUk extends ZulipLocalizations {
@override
String get mainMenuMyProfile => 'Мій профіль';

@override
String get topicsButtonLabel => 'TOPICS';

@override
String get channelFeedButtonTooltip => 'Стрічка каналу';

Expand Down
105 changes: 103 additions & 2 deletions lib/model/channel.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import 'dart:async';

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 'store.dart';

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

/// The portion of [PerAccountStore] for channels, topics, and stuff about them.
///
Expand Down Expand Up @@ -34,6 +41,26 @@ mixin ChannelStore {
/// and [streamsByName].
Map<int, Subscription> get subscriptions;

/// Fetch topics in a stream from the server.
///
/// The results from the last successful fetch
/// can be retrieved with [getStreamTopics].
Future<void> fetchTopics(int streamId);

/// Pairs of the known topics and its latest message ID, in the given stream.
///
/// Returns null if the data has never been fetched yet.
/// To fetch it from the server, use [fetchTopics].
///
/// The result is guaranteed to be sorted by [GetStreamTopicsEntry.maxId], and the
/// topics are guaranteed to be distinct.
///
/// In some cases, the same maxId affected by message moves can be present in
/// multiple [GetStreamTopicsEntry] entries. For this reason, the caller
/// should not rely on [getStreamTopics] to determine which topic the message
/// is in. Instead, refer to [PerAccountStore.messages].
List<GetStreamTopicsEntry>? getStreamTopics(int streamId);

/// 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 @@ -161,8 +188,11 @@ enum VisibilityEffect {
/// Generally the only code that should need this class is [PerAccountStore]
/// itself. Other code accesses this functionality through [PerAccountStore],
/// or through the mixin [ChannelStore] which describes its interface.
class ChannelStoreImpl with ChannelStore {
factory ChannelStoreImpl({required InitialSnapshot initialSnapshot}) {
class ChannelStoreImpl extends PerAccountStoreBase with ChannelStore {
factory ChannelStoreImpl({
required CorePerAccountStore core,
required InitialSnapshot initialSnapshot,
}) {
final subscriptions = Map.fromEntries(initialSnapshot.subscriptions.map(
(subscription) => MapEntry(subscription.streamId, subscription)));

Expand All @@ -182,6 +212,7 @@ class ChannelStoreImpl with ChannelStore {
}

return ChannelStoreImpl._(
core: core,
streams: streams,
streamsByName: streams.map((_, stream) => MapEntry(stream.name, stream)),
subscriptions: subscriptions,
Expand All @@ -190,6 +221,7 @@ class ChannelStoreImpl with ChannelStore {
}

ChannelStoreImpl._({
required super.core,
required this.streams,
required this.streamsByName,
required this.subscriptions,
Expand All @@ -203,6 +235,34 @@ class ChannelStoreImpl with ChannelStore {
@override
final Map<int, Subscription> subscriptions;

/// Maps indexed by stream IDs, of the known latest message IDs in each topic.
///
/// For example: `_latestMessageIdsByStreamTopic[stream.id][topic] = maxId`
///
/// In some cases, the same message IDs, when affected by message moves, can
/// be present for mutliple stream-topic keys.
final Map<int, Map<TopicName, int>> _latestMessageIdsByStreamTopic = {};

@override
Future<void> fetchTopics(int streamId) async {
final result = await _apiGetStreamTopics(connection, streamId: streamId,
allowEmptyTopicName: true);
_latestMessageIdsByStreamTopic[streamId] = {
for (final GetStreamTopicsEntry(:name, :maxId) in result.topics)
name: maxId,
};
}

@override
List<GetStreamTopicsEntry>? getStreamTopics(int streamId) {
final latestMessageIdsInStream = _latestMessageIdsByStreamTopic[streamId];
if (latestMessageIdsInStream == null) return null;
return [
for (final MapEntry(:key, :value) in latestMessageIdsInStream.entries)
GetStreamTopicsEntry(maxId: value, name: key),
].sortedBy((value) => -value.maxId);
}

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

Expand Down Expand Up @@ -369,4 +429,45 @@ class ChannelStoreImpl 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, :topic) = event.message as StreamMessage;

final latestMessageIdsByTopic = _latestMessageIdsByStreamTopic[streamId];
if (latestMessageIdsByTopic == null) return false;
assert(!latestMessageIdsByTopic.containsKey(topic)
|| latestMessageIdsByTopic[topic]! < event.message.id);
latestMessageIdsByTopic[topic] = event.message.id;
return true;
}

/// Handle a [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 = _latestMessageIdsByStreamTopic[origStreamId];
if (propagateMode == PropagateMode.changeAll
&& origLatestMessageIdsByTopics != null) {
shouldNotify = origLatestMessageIdsByTopics.remove(origTopic) != null;
}

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

return shouldNotify;
}
}
Loading