Skip to content

Commit

Permalink
unreads: Add helpers to provide unread counts for narrows
Browse files Browse the repository at this point in the history
These helpers do not account for muted topics and streams,
those are left for #346.
  • Loading branch information
sirpengi authored and gnprice committed Nov 9, 2023
1 parent 2b4019d commit 1eebcb6
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
46 changes: 46 additions & 0 deletions lib/model/unreads.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,54 @@ class Unreads extends ChangeNotifier {

final int selfUserId;

// TODO(#346): account for muted topics and streams
// TODO(#370): maintain this count incrementally, rather than recomputing from scratch
int countInAllMessagesNarrow() {
int c = 0;
for (final messageIds in dms.values) {
c = c + messageIds.length;
}
for (final topics in streams.values) {
for (final messageIds in topics.values) {
c = c + messageIds.length;
}
}
return c;
}

// TODO(#346): account for muted topics and streams
// TODO(#370): maintain this count incrementally, rather than recomputing from scratch
int countInStreamNarrow(int streamId) {
final topics = streams[streamId];
if (topics == null) return 0;
int c = 0;
for (final messageIds in topics.values) {
c = c + messageIds.length;
}
return c;
}

// TODO(#346): account for muted topics and streams
int countInTopicNarrow(int streamId, String topic) {
final topics = streams[streamId];
return topics?[topic]?.length ?? 0;
}

int countInDmNarrow(DmNarrow narrow) => dms[narrow]?.length ?? 0;

int countInNarrow(Narrow narrow) {
switch (narrow) {
case AllMessagesNarrow():
return countInAllMessagesNarrow();
case StreamNarrow():
return countInStreamNarrow(narrow.streamId);
case TopicNarrow():
return countInTopicNarrow(narrow.streamId, narrow.topic);
case DmNarrow():
return countInDmNarrow(narrow);
}
}

void handleMessageEvent(MessageEvent event) {
final message = event.message;
if (message.flags.contains(MessageFlag.read)) {
Expand Down
47 changes: 47 additions & 0 deletions test/model/unreads_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,53 @@ void main() {
});
});

group('count helpers', () {
test('countInAllMessagesNarrow', () {
final stream1 = eg.stream();
final stream2 = eg.stream();
prepare();
fillWithMessages([
eg.streamMessage(stream: stream1, topic: 'a', flags: []),
eg.streamMessage(stream: stream1, topic: 'b', flags: []),
eg.streamMessage(stream: stream1, topic: 'b', flags: []),
eg.streamMessage(stream: stream2, topic: 'c', flags: []),
eg.dmMessage(from: eg.otherUser, to: [eg.selfUser], flags: []),
eg.dmMessage(from: eg.thirdUser, to: [eg.selfUser], flags: []),
]);
check(model.countInAllMessagesNarrow()).equals(6);
});

test('countInStreamNarrow', () {
final stream = eg.stream();
prepare();
fillWithMessages([
eg.streamMessage(stream: stream, topic: 'a', flags: []),
eg.streamMessage(stream: stream, topic: 'a', flags: []),
eg.streamMessage(stream: stream, topic: 'b', flags: []),
eg.streamMessage(stream: stream, topic: 'b', flags: []),
eg.streamMessage(stream: stream, topic: 'b', flags: []),
]);
check(model.countInStreamNarrow(stream.streamId)).equals(5);
});

test('countInTopicNarrow', () {
final stream = eg.stream();
prepare();
fillWithMessages(List.generate(7, (i) => eg.streamMessage(
stream: stream, topic: 'a', flags: [])));
check(model.countInTopicNarrow(stream.streamId, 'a')).equals(7);
});

test('countInDmNarrow', () {
prepare();
fillWithMessages(List.generate(5, (i) => eg.dmMessage(
from: eg.otherUser, to: [eg.selfUser], flags: [])));
final narrow = DmNarrow.withUser(
eg.otherUser.userId, selfUserId: eg.selfUser.userId);
check(model.countInDmNarrow(narrow)).equals(5);
});
});

group('handleMessageEvent', () {
for (final (isUnread, isStream, isDirectMentioned, isWildcardMentioned) in [
(true, true, true, true ),
Expand Down

0 comments on commit 1eebcb6

Please sign in to comment.