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.
This commit is contained in:
Sahil Kumar
2022-03-01 09:49:20 +01:00
committed by GitHub
parent 52b035cb5f
commit 655da0ba33
@@ -643,13 +643,18 @@ class Channel {
/// Deletes the [message] from the channel. /// Deletes the [message] from the channel.
Future<EmptyResponse> deleteMessage(Message message, {bool? hard}) async { Future<EmptyResponse> deleteMessage(Message message, {bool? hard}) async {
final hardDelete = hard ?? false;
// Directly deleting the local messages which are not yet sent to server // Directly deleting the local messages which are not yet sent to server
if (message.status == MessageSendingStatus.sending || if (message.status == MessageSendingStatus.sending ||
message.status == MessageSendingStatus.failed) { message.status == MessageSendingStatus.failed) {
state!.updateMessage(message.copyWith( state!.deleteMessage(
type: 'deleted', message.copyWith(
status: MessageSendingStatus.sent, type: 'deleted',
)); status: MessageSendingStatus.sent,
),
hardDelete: hardDelete,
);
// Removing the attachments upload completer to stop the `sendMessage` // Removing the attachments upload completer to stop the `sendMessage`
// waiting for attachments to complete. // waiting for attachments to complete.
@@ -667,11 +672,14 @@ class Channel {
deletedAt: message.deletedAt ?? DateTime.now(), deletedAt: message.deletedAt ?? DateTime.now(),
); );
state?.updateMessage(message); state?.deleteMessage(message, hardDelete: hardDelete);
final response = await _client.deleteMessage(message.id, hard: hard); 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; return response;
} catch (e) { } catch (e) {
@@ -1826,27 +1834,33 @@ class ChannelClientState {
final parentId = message.parentId; final parentId = message.parentId;
// i.e. it's a thread message, Remove it // i.e. it's a thread message, Remove it
if (parentId != null) { if (parentId != null) {
if (!threads.containsKey(parentId)) { final newThreads = {...threads};
return; // Early return in case the thread is not available
} if (!newThreads.containsKey(parentId)) return;
final newThreads = Map<String, List<Message>>.from(threads); _threads = newThreads
..update(
parentId,
(messages) => messages..removeWhere((e) => e.id == message.id),
);
newThreads[parentId] = [ // Early return if the thread message is not shown in channel.
...newThreads[parentId]!..removeWhere((e) => e.id == message.id), if (message.showInChannel == false) return;
];
_threads = newThreads;
return;
} }
// Remove regular message // Remove regular message, thread message shown in channel
final allMessages = [...messages]; final allMessages = [...messages];
_channelState = _channelState.copyWith( _channelState = _channelState.copyWith(
messages: allMessages..removeWhere((e) => e.id == message.id), 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() { void _listenReadEvents() {
if (_channelState.channel?.config.readEvents == false) { if (_channelState.channel?.config.readEvents == false) {
return; return;