Skip to content

Commit

Permalink
unreads: Use case i9e lookups, preserving case.
Browse files Browse the repository at this point in the history
This commit makes the topics having different
casings for the same topic be stored under
the same QueueList by making the lookups
lowercase while preserving one of the
variants of the used topic case.

Fixes #980.
  • Loading branch information
apoorvapendse committed Dec 27, 2024
1 parent 24215f6 commit 475f35b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
14 changes: 11 additions & 3 deletions lib/model/unreads.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ class Unreads extends ChangeNotifier {
required ChannelStore channelStore,
}) {
final streams = <int, Map<String, QueueList<int>>>{};
final topicMapper = <String, String>{};
final dms = <DmNarrow, QueueList<int>>{};
final mentions = Set.of(initial.mentions);

for (final unreadChannelSnapshot in initial.channels) {
final streamId = unreadChannelSnapshot.streamId;
final topic = unreadChannelSnapshot.topic;
(streams[streamId] ??= {})[topic] = QueueList.from(unreadChannelSnapshot.unreadMessageIds);
topicMapper[topic.toLowerCase()] = topic;
(streams[streamId] ??= {})[topic.toLowerCase()] = QueueList.from(unreadChannelSnapshot.unreadMessageIds);
}

for (final unreadDmSnapshot in initial.dms) {
Expand All @@ -64,6 +66,7 @@ class Unreads extends ChangeNotifier {
return Unreads._(
channelStore: channelStore,
streams: streams,
topicMapper: topicMapper,
dms: dms,
mentions: mentions,
oldUnreadsMissing: initial.oldUnreadsMissing,
Expand All @@ -74,6 +77,7 @@ class Unreads extends ChangeNotifier {
Unreads._({
required this.channelStore,
required this.streams,
required this.topicMapper,
required this.dms,
required this.mentions,
required this.oldUnreadsMissing,
Expand All @@ -88,6 +92,9 @@ class Unreads extends ChangeNotifier {
/// Unread stream messages, as: stream ID → topic → message IDs (sorted).
final Map<int, Map<String, QueueList<int>>> streams;

// Maps lowercase topic names for lookup to one of the variants of the topic (case preserving).
final Map<String, String> topicMapper;

/// Unread DM messages, as: DM narrow → message IDs (sorted).
final Map<DmNarrow, QueueList<int>> dms;

Expand Down Expand Up @@ -368,7 +375,7 @@ class Unreads extends ChangeNotifier {
switch (detail.type) {
case MessageType.stream:
final topics = (newlyUnreadInStreams[detail.streamId!] ??= {});
final messageIds = (topics[detail.topic!] ??= QueueList());
final messageIds = (topics[detail.topic!.toLowerCase()] ??= QueueList());
messageIds.add(messageId);
case MessageType.direct:
final narrow = DmNarrow.ofUpdateMessageFlagsMessageDetail(selfUserId: selfUserId,
Expand Down Expand Up @@ -437,7 +444,8 @@ class Unreads extends ChangeNotifier {
}

void _addLastInStreamTopic(int messageId, int streamId, String topic) {
((streams[streamId] ??= {})[topic] ??= QueueList()).addLast(messageId);
topicMapper[topic.toLowerCase()] = topic;
((streams[streamId] ??= {})[topic.toLowerCase()] ??= QueueList()).addLast(messageId);
}

// [messageIds] must be sorted ascending and without duplicates.
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/inbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ class _InboxPageState extends State<InboxPageBody> with PerAccountStoreAwareStat
int countInStream = 0;
bool streamHasMention = false;
for (final MapEntry(key: topic, value: messageIds) in topics.entries) {
if (!store.isTopicVisible(streamId, topic)) continue;
if (!store.isTopicVisible(streamId, unreadsModel != null? unreadsModel!.topicMapper[topic]! : topic)) continue;
final countInTopic = messageIds.length;
final hasMention = messageIds.any((messageId) => unreadsModel!.mentions.contains(messageId));
if (hasMention) streamHasMention = true;
topicItems.add((topic, countInTopic, hasMention, messageIds.last));
topicItems.add((unreadsModel!.topicMapper[topic]!, countInTopic, hasMention, messageIds.last));
countInStream += countInTopic;
}
if (countInStream == 0) {
Expand Down
3 changes: 2 additions & 1 deletion test/model/unreads_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void main() {
switch (message) {
case StreamMessage():
final perTopic = expectedStreams[message.streamId] ??= {};
final messageIds = perTopic[message.topic] ??= QueueList();
final messageIds = perTopic[message.topic.toLowerCase()] ??= QueueList();
messageIds.add(message.id);
case DmMessage():
final narrow = DmNarrow.ofMessage(message, selfUserId: eg.selfUser.userId);
Expand Down Expand Up @@ -289,6 +289,7 @@ void main() {
final stream2 = eg.stream(streamId: 2);
for (final (oldStream, newStream, oldTopic, newTopic) in [
(stream1, stream1, 'a', 'a'),
(stream1, stream1, 'a', 'A'),
(stream1, stream1, 'a', 'b'),
(stream1, stream2, 'a', 'a'),
(stream1, stream2, 'a', 'b'),
Expand Down

0 comments on commit 475f35b

Please sign in to comment.