Skip to content

Commit 5d0e790

Browse files
committed
typing_status: Do not allow adding self as typist.
The server usually does not send events notifying the user themself typing. But having this check gives us a stronger guarantee that the maps do not contain the self user. Signed-off-by: Zixuan James Li <[email protected]>
1 parent be722d2 commit 5d0e790

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

lib/api/model/events.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ class TypingEvent extends Event {
901901
required this.recipientIds,
902902
required this.streamId,
903903
required this.topic,
904-
}) : assert(isSortedWithoutDuplicates(recipientIds ?? []));
904+
}) : assert(recipientIds == null || isSortedWithoutDuplicates(recipientIds));
905905

906906
static Object? _readSenderId(Map<Object?, Object?> json, String key) {
907907
return (json['sender'] as Map<String, dynamic>)['user_id'];

lib/model/typing_status.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:async';
33
import 'package:flutter/foundation.dart';
44

55
import '../api/model/events.dart';
6+
import '../log.dart';
67
import 'narrow.dart';
78

89
/// The model for tracking the typing status organized by narrows.
@@ -38,6 +39,10 @@ class TypingStatus extends ChangeNotifier {
3839
}
3940

4041
bool _addTypist(SendableNarrow narrow, int typistUserId) {
42+
if (typistUserId == selfUserId) {
43+
assert(debugLog('typing status: adding self as typist'));
44+
return false;
45+
}
4146
final narrowTimerMap = _timerMapsByNarrow[narrow] ??= {};
4247
final typistTimer = narrowTimerMap[typistUserId];
4348
final isNewTypist = typistTimer == null;

test/model/typing_status_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ void main() {
9797
checkTypists({groupNarrow: [eg.otherUser, eg.thirdUser]});
9898
checkNotifiedOnce();
9999
});
100+
101+
test('ignore adding self as typist', () {
102+
prepareModel();
103+
104+
model.handleTypingEvent(
105+
eg.typingEvent(groupNarrow, TypingOp.start, eg.selfUser.userId));
106+
checkTypists({});
107+
checkNotNotified();
108+
});
100109
});
101110

102111
group('handle typing stop events', () {

0 commit comments

Comments
 (0)