From 9a4677a7b852c34344722455a64a20d5af462241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Stolin=CC=81ski?= Date: Tue, 8 Feb 2022 15:47:42 +0100 Subject: [PATCH] decrease parent message reply count only once --- .../stream_chat/lib/src/client/channel.dart | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/stream_chat/lib/src/client/channel.dart b/packages/stream_chat/lib/src/client/channel.dart index d3900842..84f0321e 100644 --- a/packages/stream_chat/lib/src/client/channel.dart +++ b/packages/stream_chat/lib/src/client/channel.dart @@ -1083,7 +1083,7 @@ class Channel { } else { // remove the passed message if response does // not contain message - state!.removeMessage(message); + state!.removeMessage(message, decreaseReplyCount: true); await _client.chatPersistenceClient?.deleteMessageById(messageId); } return res; @@ -1752,6 +1752,7 @@ class ChannelClientState { _subscriptions.add(_channel.on(EventType.messageDeleted).listen((event) { final message = event.message!; if (event.hardDelete == true) { + //do not decrease reply count here - it is done in _listenMessageUpdated removeMessage(message, hardDelete: true); } else { addMessage(message); @@ -1810,24 +1811,30 @@ class ChannelClientState { } /// Remove a [message] from this [channelState]. - void removeMessage(Message message, {bool hardDelete = false}) { + void removeMessage( + Message message, { + bool hardDelete = false, + bool decreaseReplyCount = false, + }) { final parentId = message.parentId; // i.e. it's a thread message // 1. Remove the thread message // 2. Reduce total reply count of parent message if (parentId != null) { - final allMessages = [...messages]; - final parentMessage = allMessages.firstWhereOrNull( - (it) => it.id == parentId, - ); + if (decreaseReplyCount) { + final allMessages = [...messages]; + final parentMessage = allMessages.firstWhereOrNull( + (it) => it.id == parentId, + ); - // return if message not available in the memory - if (parentMessage == null) return; - final replyCount = parentMessage.replyCount; - // return if reply count is null or zero - if (replyCount == null || replyCount == 0) return; + // return if message not available in the memory + if (parentMessage == null) return; + final replyCount = parentMessage.replyCount; + // return if reply count is null or zero + if (replyCount == null || replyCount == 0) return; - addMessage(parentMessage.copyWith(replyCount: replyCount - 1)); + addMessage(parentMessage.copyWith(replyCount: replyCount - 1)); + } updateThreadInfo( parentId, threads[parentId]!