diff --git a/packages/stream_chat/lib/src/client/channel.dart b/packages/stream_chat/lib/src/client/channel.dart index c623039b..0c9dc049 100644 --- a/packages/stream_chat/lib/src/client/channel.dart +++ b/packages/stream_chat/lib/src/client/channel.dart @@ -1447,15 +1447,32 @@ class Channel { ); } + // Timer to keep track of mute expiration. This is used to update the channel + // state when the mute expires. + Timer? _muteExpirationTimer; + /// Mutes the channel. Future mute({Duration? expiration}) { _checkInitialized(); + + // If there is a expiration set, we will set a timer to automatically unmute + // the channel when the mute expires. + if (expiration != null) { + _muteExpirationTimer?.cancel(); + _muteExpirationTimer = Timer(expiration, unmute); + } + return _client.muteChannel(cid!, expiration: expiration); } /// Unmute the channel. Future unmute() { _checkInitialized(); + + // Cancel the mute expiration timer if it is set. + _muteExpirationTimer?.cancel(); + _muteExpirationTimer = null; + return _client.unmuteChannel(cid!); } diff --git a/packages/stream_chat/test/src/client/channel_test.dart b/packages/stream_chat/test/src/client/channel_test.dart index 12a35629..3b4a1b6a 100644 --- a/packages/stream_chat/test/src/client/channel_test.dart +++ b/packages/stream_chat/test/src/client/channel_test.dart @@ -2481,6 +2481,31 @@ void main() { )).called(1); }); + test('`.mute with expiration`', () async { + const expiration = Duration(seconds: 3); + + when(() => client.muteChannel( + channelCid, + expiration: expiration, + )).thenAnswer((_) async => EmptyResponse()); + + when(() => client.unmuteChannel(channelCid)) + .thenAnswer((_) async => EmptyResponse()); + + final res = await channel.mute(expiration: expiration); + + expect(res, isNotNull); + + verify(() => client.muteChannel( + channelCid, + expiration: expiration, + )).called(1); + + // wait for expiration + await Future.delayed(expiration); + verify(() => client.unmuteChannel(channelCid)).called(1); + }); + test('`.unmute`', () async { when( () => client.unmuteChannel(channelCid),