From 0f5526e1b8ce52936d6bd87587e020c03505a5c7 Mon Sep 17 00:00:00 2001 From: Krille Date: Wed, 26 Jun 2024 14:43:18 +0200 Subject: [PATCH] feat: Automatically cancel typing indicators after 30 seconds --- lib/src/client.dart | 7 +++++-- lib/src/room.dart | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index ea1d6ce4..2522e553 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -118,6 +118,9 @@ class Client extends MatrixApi { final Duration sendTimelineEventTimeout; + /// The timeout until a typing indicator gets removed automatically. + final Duration typingIndicatorTimeout; + Future Function( MatrixImageFileResizeArguments)? customImageResizer; @@ -205,6 +208,7 @@ class Client extends MatrixApi { /// lifetime to the server which overrides the default one. Needs server /// support. this.customRefreshTokenLifetime, + this.typingIndicatorTimeout = const Duration(seconds: 30), }) : syncFilter = syncFilter ?? Filter( room: RoomFilter( @@ -2474,8 +2478,7 @@ class Client extends MatrixApi { BasicRoomEvent.fromJson(eventUpdate.content); break; case EventUpdateType.ephemeral: - room.ephemerals[eventUpdate.content['type']] = - BasicRoomEvent.fromJson(eventUpdate.content); + room.setEphemeral(BasicRoomEvent.fromJson(eventUpdate.content)); break; case EventUpdateType.history: case EventUpdateType.decryptedTimelineQueue: diff --git a/lib/src/room.dart b/lib/src/room.dart index e0eb11c6..b76e39db 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -74,6 +74,8 @@ class Room { final _sendingQueue = []; + Timer? _clearTypingIndicatorTimer; + Map toJson() => { 'id': id, 'membership': membership.toString().split('.').last, @@ -367,6 +369,16 @@ class Room { Event? lastEvent; + void setEphemeral(BasicRoomEvent ephemeral) { + ephemerals[ephemeral.type] = ephemeral; + if (ephemeral.type == 'm.typing') { + _clearTypingIndicatorTimer?.cancel(); + _clearTypingIndicatorTimer = Timer(client.typingIndicatorTimeout, () { + ephemerals.remove('m.typing'); + }); + } + } + /// Returns a list of all current typing users. List get typingUsers { final typingMxid = ephemerals['m.typing']?.content['user_ids'];