From 655da0ba33855a667108391fdd89a8f6d2ccfd6e Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Tue, 1 Mar 2022 14:19:20 +0530 Subject: [PATCH] fix(llc): Remove regular message in case `showInChannel` is true. (#3) * fix(llc): also remove regular message in case `showInChannel` is true. * fix(llc): use `hard` param while removing message in channel state. --- .../stream_chat/lib/src/client/channel.dart | 48 ++++++++++++------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/packages/stream_chat/lib/src/client/channel.dart b/packages/stream_chat/lib/src/client/channel.dart index 072bb150..b0f1bdd0 100644 --- a/packages/stream_chat/lib/src/client/channel.dart +++ b/packages/stream_chat/lib/src/client/channel.dart @@ -643,13 +643,18 @@ class Channel { /// Deletes the [message] from the channel. Future deleteMessage(Message message, {bool? hard}) async { + final hardDelete = hard ?? false; + // Directly deleting the local messages which are not yet sent to server if (message.status == MessageSendingStatus.sending || message.status == MessageSendingStatus.failed) { - state!.updateMessage(message.copyWith( - type: 'deleted', - status: MessageSendingStatus.sent, - )); + state!.deleteMessage( + message.copyWith( + type: 'deleted', + status: MessageSendingStatus.sent, + ), + hardDelete: hardDelete, + ); // Removing the attachments upload completer to stop the `sendMessage` // waiting for attachments to complete. @@ -667,11 +672,14 @@ class Channel { deletedAt: message.deletedAt ?? DateTime.now(), ); - state?.updateMessage(message); + state?.deleteMessage(message, hardDelete: hardDelete); final response = await _client.deleteMessage(message.id, hard: hard); - state?.updateMessage(message.copyWith(status: MessageSendingStatus.sent)); + state?.deleteMessage( + message.copyWith(status: MessageSendingStatus.sent), + hardDelete: hardDelete, + ); return response; } catch (e) { @@ -1826,27 +1834,33 @@ class ChannelClientState { final parentId = message.parentId; // i.e. it's a thread message, Remove it if (parentId != null) { - if (!threads.containsKey(parentId)) { - return; - } + final newThreads = {...threads}; + // Early return in case the thread is not available + if (!newThreads.containsKey(parentId)) return; - final newThreads = Map>.from(threads); + _threads = newThreads + ..update( + parentId, + (messages) => messages..removeWhere((e) => e.id == message.id), + ); - newThreads[parentId] = [ - ...newThreads[parentId]!..removeWhere((e) => e.id == message.id), - ]; - - _threads = newThreads; - return; + // Early return if the thread message is not shown in channel. + if (message.showInChannel == false) return; } - // Remove regular message + // Remove regular message, thread message shown in channel final allMessages = [...messages]; _channelState = _channelState.copyWith( messages: allMessages..removeWhere((e) => e.id == message.id), ); } + /// Removes/Updates the [message] based on the [hardDelete] value. + void deleteMessage(Message message, {bool hardDelete = false}) { + if (hardDelete) return removeMessage(message); + return updateMessage(message); + } + void _listenReadEvents() { if (_channelState.channel?.config.readEvents == false) { return;