Skip to content

Commit 61e418b

Browse files
authored
fixes for deleted messagees (#2581)
1 parent 94f7253 commit 61e418b

3 files changed

Lines changed: 102 additions & 2 deletions

File tree

packages/stream_chat/lib/src/core/models/message.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,11 +585,22 @@ class Message extends Equatable implements ComparableFieldProvider {
585585
Message syncWith(Message? other) {
586586
if (other == null) return this;
587587

588-
return copyWith(
588+
var synced = copyWith(
589589
localCreatedAt: other.localCreatedAt,
590590
localUpdatedAt: other.localUpdatedAt,
591591
localDeletedAt: other.localDeletedAt,
592592
);
593+
594+
// The backend does not always enrich this deletedForMe field
595+
if (other.deletedForMe == true && synced.deletedForMe != true) {
596+
synced = synced.copyWith(
597+
deletedForMe: true,
598+
type: MessageType.deleted,
599+
state: MessageState.deletedForMe,
600+
);
601+
}
602+
603+
return synced;
593604
}
594605

595606
@override

packages/stream_chat/test/src/core/models/message_test.dart

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import 'package:stream_chat/src/core/models/attachment.dart';
44
import 'package:stream_chat/src/core/models/message.dart';
5+
import 'package:stream_chat/src/core/models/message_state.dart';
56
import 'package:stream_chat/src/core/models/moderation.dart';
67
import 'package:stream_chat/src/core/models/reaction.dart';
78
import 'package:stream_chat/src/core/models/reaction_group.dart';
@@ -535,6 +536,94 @@ void main() {
535536
expect(message.isFlagged, isTrue);
536537
});
537538
});
539+
540+
group('syncWith', () {
541+
test('should preserve local timestamps from the other message', () {
542+
final localCreatedAt = DateTime(2024, 1, 1);
543+
final localUpdatedAt = DateTime(2024, 1, 2);
544+
final localDeletedAt = DateTime(2024, 1, 3);
545+
546+
final serverMessage = Message(id: 'msg-1', text: 'Hello');
547+
final localMessage = Message(
548+
id: 'msg-1',
549+
text: 'Hello',
550+
localCreatedAt: localCreatedAt,
551+
localUpdatedAt: localUpdatedAt,
552+
localDeletedAt: localDeletedAt,
553+
);
554+
555+
final synced = serverMessage.syncWith(localMessage);
556+
expect(synced.localCreatedAt, localCreatedAt);
557+
expect(synced.localUpdatedAt, localUpdatedAt);
558+
expect(synced.localDeletedAt, localDeletedAt);
559+
});
560+
561+
test('should return this if other is null', () {
562+
final message = Message(id: 'msg-1', text: 'Hello');
563+
final synced = message.syncWith(null);
564+
expect(identical(synced, message), isTrue);
565+
});
566+
567+
test(
568+
'should preserve deletedForMe from local when server does not have it',
569+
() {
570+
final serverMessage = Message(
571+
id: 'msg-1',
572+
text: 'Hello',
573+
);
574+
final localMessage = Message(
575+
id: 'msg-1',
576+
text: 'Hello',
577+
deletedForMe: true,
578+
type: MessageType.deleted,
579+
state: MessageState.deletedForMe,
580+
);
581+
582+
final synced = serverMessage.syncWith(localMessage);
583+
expect(synced.deletedForMe, isTrue);
584+
expect(synced.type, MessageType.deleted);
585+
expect(synced.state, MessageState.deletedForMe);
586+
expect(synced.isDeleted, isTrue);
587+
},
588+
);
589+
590+
test(
591+
'should keep server deletedForMe when both server and local have it',
592+
() {
593+
final serverMessage = Message(
594+
id: 'msg-1',
595+
text: 'Hello',
596+
deletedForMe: true,
597+
type: MessageType.deleted,
598+
state: MessageState.deletedForMe,
599+
);
600+
final localMessage = Message(
601+
id: 'msg-1',
602+
text: 'Hello',
603+
deletedForMe: true,
604+
type: MessageType.deleted,
605+
state: MessageState.deletedForMe,
606+
);
607+
608+
final synced = serverMessage.syncWith(localMessage);
609+
expect(synced.deletedForMe, isTrue);
610+
expect(synced.type, MessageType.deleted);
611+
expect(synced.state, MessageState.deletedForMe);
612+
},
613+
);
614+
615+
test(
616+
'should not set deletedForMe when neither server nor local have it',
617+
() {
618+
final serverMessage = Message(id: 'msg-1', text: 'Hello');
619+
final localMessage = Message(id: 'msg-1', text: 'Hello');
620+
621+
final synced = serverMessage.syncWith(localMessage);
622+
expect(synced.deletedForMe, isNull);
623+
expect(synced.type, isNot(MessageType.deleted));
624+
},
625+
);
626+
});
538627
}
539628

540629
/// Helper function to create a Message for testing

packages/stream_chat_flutter/lib/src/scroll_view/channel_scroll_view/stream_channel_list_item.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ class _ChannelLastMessageWithStatusState extends State<_ChannelLastMessageWithSt
669669

670670
return Row(
671671
children: [
672-
deliveryPrefix,
672+
if (!latestLastMessage.isDeleted) deliveryPrefix,
673673
Flexible(
674674
child: StreamMessagePreviewText(
675675
message: latestLastMessage,

0 commit comments

Comments
 (0)