Skip to content

Commit

Permalink
unreads: Handle updates when there are moved messages
Browse files Browse the repository at this point in the history
Signed-off-by: Zixuan James Li <[email protected]>
  • Loading branch information
PIG208 committed Jan 28, 2025
1 parent 2433aa3 commit 72c791a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/model/unreads.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import '../api/model/model.dart';
import '../api/model/events.dart';
import '../log.dart';
import 'algorithms.dart';
import 'message.dart';
import 'narrow.dart';
import 'channel.dart';

Expand Down Expand Up @@ -296,13 +297,37 @@ class Unreads extends ChangeNotifier {
madeAnyUpdate |= mentions.add(messageId);
}

// TODO(#901) handle moved messages
madeAnyUpdate |= _handleMessageMove(event);

if (madeAnyUpdate) {
notifyListeners();
}
}

bool _handleMessageMove(UpdateMessageEvent event) {
// TODO(#901) handle moved messages
final messageMove = UpdateMessageMoveData.tryParseFromEvent(event);
if (messageMove == null) {
// No moved messages or malformed event.
return false;
}

final topics = streams[messageMove.origStreamId];
if (topics == null || topics[messageMove.origTopic] == null) {
// No known unreads affected by move; nothing to do.
return false;
}

final messageIdsToMove = _removeAllInStreamTopic(
event.messageIds.toSet(),
messageMove.origStreamId, messageMove.origTopic);
assert(isSortedWithoutDuplicates(messageIdsToMove));
_addAllInStreamTopic(
messageIdsToMove..sort(),
messageMove.newStreamId, messageMove.newTopic);
return true;
}

void handleDeleteMessageEvent(DeleteMessageEvent event) {
mentions.removeAll(event.messageIds);
final messageIdsSet = Set.of(event.messageIds);
Expand Down
26 changes: 26 additions & 0 deletions test/model/unreads_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,32 @@ void main() {
}
}
});

group('moves', () {
test('unknown message move', () {

});

test('tolerates unsorted event.messageIds', () {

});

test('move read message from known topic to unknown topic', () {

});

test('move unread message from known topic to known topic', () {

});

test('move unread from unknown to unknown', () {

});

test('move unread from unknown to known', () {

});
});
});


Expand Down

0 comments on commit 72c791a

Please sign in to comment.