Merge pull request #891 from squaddy/bugfix/deleting_thread_messages

This commit is contained in:
Sahil Kumar
2022-03-01 15:47:40 +05:30
committed by GitHub
3 changed files with 74 additions and 57 deletions
+2
View File
@@ -10,6 +10,8 @@
- [[#890]](https://github.com/GetStream/stream-chat-flutter/pull/890) Fixed Reactions not updating on thread messages.
Thanks [bstolinski](https://github.com/bstolinski).
- [[#897]](https://github.com/GetStream/stream-chat-flutter/issues/897) Fixed error type mis-match in `AuthInterceptor`.
- [[#891]](https://github.com/GetStream/stream-chat-flutter/pull/891) Fixed reply counter for parent message not
updating correctly after deleting thread message.
- Fix `channelState.copyWith` with respect to pinnedMessages.
## 3.4.0
@@ -407,7 +407,7 @@ class Channel {
if (index != -1) {
final newAttachments = [...message!.attachments]..[index] = attachment;
final updatedMessage = message!.copyWith(attachments: newAttachments);
state?.addMessage(updatedMessage);
state?.updateMessage(updatedMessage);
// updating original message for next iteration
message = message!.merge(updatedMessage);
}
@@ -512,7 +512,7 @@ class Channel {
).toList(),
);
state!.addMessage(message);
state!.updateMessage(message);
try {
if (message.attachments.any((it) => !it.uploadState.isSuccess)) {
@@ -535,7 +535,7 @@ class Channel {
type,
skipPush: skipPush,
);
state!.addMessage(response.message);
state!.updateMessage(response.message);
if (cooldown > 0) cooldownStartedAt = DateTime.now();
return response;
} catch (e) {
@@ -571,7 +571,7 @@ class Channel {
).toList(),
);
state?.addMessage(message);
state?.updateMessage(message);
try {
if (message.attachments.any((it) => !it.uploadState.isSuccess)) {
@@ -594,7 +594,7 @@ class Channel {
ownReactions: message.ownReactions,
);
state?.addMessage(m);
state?.updateMessage(m);
return response;
} catch (e) {
@@ -602,7 +602,7 @@ class Channel {
if (e.isRetriable) {
state!._retryQueue.add([message]);
} else {
state?.addMessage(originalMessage);
state?.updateMessage(originalMessage);
}
}
rethrow;
@@ -630,7 +630,7 @@ class Channel {
ownReactions: message.ownReactions,
);
state?.addMessage(updatedMessage);
state?.updateMessage(updatedMessage);
return response;
} catch (e) {
@@ -643,13 +643,18 @@ class Channel {
/// Deletes the [message] from the channel.
Future<EmptyResponse> 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!.addMessage(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?.addMessage(message);
state?.deleteMessage(message, hardDelete: hardDelete);
final response = await _client.deleteMessage(message.id, hard: hard);
state?.addMessage(message.copyWith(status: MessageSendingStatus.sent));
state?.deleteMessage(
message.copyWith(status: MessageSendingStatus.sent),
hardDelete: hardDelete,
);
return response;
} catch (e) {
@@ -861,7 +869,7 @@ class Channel {
ownReactions: ownReactions,
);
state?.addMessage(newMessage);
state?.updateMessage(newMessage);
try {
final reactionResp = await _client.sendReaction(
@@ -874,7 +882,7 @@ class Channel {
return reactionResp;
} catch (_) {
// Reset the message if the update fails
state?.addMessage(message);
state?.updateMessage(message);
rethrow;
}
}
@@ -914,7 +922,7 @@ class Channel {
ownReactions: ownReactions,
);
state?.addMessage(newMessage);
state?.updateMessage(newMessage);
try {
final deleteResponse = await _client.deleteReaction(
@@ -924,7 +932,7 @@ class Channel {
return deleteResponse;
} catch (_) {
// Reset the message if the update fails
state?.addMessage(message);
state?.updateMessage(message);
rethrow;
}
}
@@ -1081,7 +1089,7 @@ class Channel {
// update the passed message with response message
if (res.message != null) {
state!.addMessage(res.message!);
state!.updateMessage(res.message!);
} else {
// remove the passed message if response does
// not contain message
@@ -1712,7 +1720,7 @@ class ChannelClientState {
final message = event.message!.copyWith(
ownReactions: ownReactions,
);
addMessage(message);
updateMessage(message);
}));
}
@@ -1725,7 +1733,7 @@ class ChannelClientState {
final message = event.message!.copyWith(
ownReactions: oldMessage?.ownReactions,
);
addMessage(message);
updateMessage(message);
}));
}
@@ -1743,7 +1751,16 @@ class ChannelClientState {
final message = event.message!.copyWith(
ownReactions: oldMessage?.ownReactions,
);
addMessage(message);
updateMessage(message);
if (message.pinned) {
_channelState = _channelState.copyWith(
pinnedMessages: [
..._channelState.pinnedMessages,
message,
],
);
}
}));
}
@@ -1751,9 +1768,9 @@ class ChannelClientState {
_subscriptions.add(_channel.on(EventType.messageDeleted).listen((event) {
final message = event.message!;
if (event.hardDelete == true) {
removeMessage(message, hardDelete: true);
removeMessage(message);
} else {
addMessage(message);
updateMessage(message);
}
}));
}
@@ -1768,7 +1785,7 @@ class ChannelClientState {
final message = event.message!;
if (isUpToDate ||
(message.parentId != null && message.showInChannel != true)) {
addMessage(message);
updateMessage(message);
}
if (_countMessageAsUnread(message)) {
@@ -1778,9 +1795,13 @@ class ChannelClientState {
}
/// Add a [message] to this [channelState].
void addMessage(Message message) {
@Deprecated('Use updateMessage instead')
void addMessage(Message message) => updateMessage(message);
/// Updates the [message] in the state if it exists. Adds it otherwise.
void updateMessage(Message message) {
if (message.parentId == null || message.showInChannel == true) {
final newMessages = List<Message>.from(_channelState.messages);
final newMessages = [...messages];
final oldIndex = newMessages.indexWhere((m) => m.id == message.id);
if (oldIndex != -1) {
Message? m;
@@ -1825,41 +1846,35 @@ class ChannelClientState {
}
/// Remove a [message] from this [channelState].
void removeMessage(Message message, {bool hardDelete = false}) {
void removeMessage(Message message) {
final parentId = message.parentId;
// i.e. it's a thread message
// 1. Remove the thread message
// 2. Reduce total reply count of parent message
// i.e. it's a thread message, Remove it
if (parentId != null) {
final allMessages = [...messages];
final parentMessage = allMessages.firstWhereOrNull(
(it) => it.id == parentId,
);
final newThreads = {...threads};
// Early return in case the thread is not available
if (!newThreads.containsKey(parentId)) 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;
_threads = newThreads
..update(
parentId,
(messages) => messages..removeWhere((e) => e.id == message.id),
);
addMessage(parentMessage.copyWith(replyCount: replyCount - 1));
updateThreadInfo(
parentId,
threads[parentId]!
..removeWhere(
(e) => e.id == message.id,
),
);
} else {
// Remove regular message
final allMessages = [...messages];
if (hardDelete) {
allMessages.removeWhere((e) => e.id == message.id);
_channelState = _channelState.copyWith(messages: allMessages);
} else if (allMessages.remove(message)) {
_channelState = _channelState.copyWith(messages: allMessages);
}
// Early return if the thread message is not shown in channel.
if (message.showInChannel == false) return;
}
// 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() {
@@ -158,7 +158,7 @@ class RetryQueue {
: message.status == MessageSendingStatus.updating
? MessageSendingStatus.failed_update
: MessageSendingStatus.failed_delete;
channel.state?.addMessage(message.copyWith(status: newStatus));
channel.state?.updateMessage(message.copyWith(status: newStatus));
}
Future<void> _retryMessage(Message message) async {