From 346183b6eebc667e64a9b72382545beb357df0a7 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 4 Aug 2023 16:29:31 +0530 Subject: [PATCH] feat(llc): add support for `channel.countUnreadMentions()`. --- .../stream_chat/lib/src/client/channel.dart | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/stream_chat/lib/src/client/channel.dart b/packages/stream_chat/lib/src/client/channel.dart index 838f267c..38b24898 100644 --- a/packages/stream_chat/lib/src/client/channel.dart +++ b/packages/stream_chat/lib/src/client/channel.dart @@ -2334,6 +2334,26 @@ class ChannelClientState { !isThreadMessage; } + /// Counts the number of unread messages mentioning the current user. + /// + /// **NOTE**: The method relies on the [Channel.messages] list and doesn't do + /// any API call. Therefore, the count might be not reliable as it relies on + /// the local data. + int countUnreadMentions() { + final lastRead = currentUserRead?.lastRead; + final userId = _channel.client.state.currentUser?.id; + + var count = 0; + for (final message in messages) { + if (_countMessageAsUnread(message) && + (lastRead == null || message.createdAt.isAfter(lastRead)) && + message.mentionedUsers.any((user) => user.id == userId) == true) { + count++; + } + } + return count; + } + /// Update threads with updated information about messages. void updateThreadInfo(String parentId, List messages) { final newThreads = Map>.from(threads);