Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/stream_chat/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Upcoming

🐞 Fixed

- Fixed `toDraftMessage` to only include successfully uploaded attachments in draft messages.

## 9.16.0

🐞 Fixed
Expand Down
7 changes: 6 additions & 1 deletion packages/stream_chat/lib/src/core/models/draft_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,16 @@ extension MessageToDraftMessage on Message {
/// This is useful when you want to convert a message to a draft message
/// before sending it to the server.
DraftMessage toDraftMessage() {
// Only include attachments that have been successfully uploaded.
final uploadedAttachments = attachments.where((it) {
return it.uploadState.isSuccess;
}).toList();

return DraftMessage(
id: id,
text: text,
type: type,
attachments: attachments,
attachments: uploadedAttachments,
parentId: parentId,
showInChannel: showInChannel,
mentionedUsers: mentionedUsers,
Expand Down
50 changes: 46 additions & 4 deletions packages/stream_chat/test/src/core/models/draft_message_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore_for_file: avoid_redundant_argument_values

import 'package:stream_chat/src/core/models/attachment.dart';
import 'package:stream_chat/src/core/models/attachment_file.dart';
import 'package:stream_chat/src/core/models/draft_message.dart';
import 'package:stream_chat/src/core/models/message.dart';
import 'package:stream_chat/src/core/models/poll.dart';
Expand Down Expand Up @@ -303,8 +304,8 @@ void main() {
group('Message and DraftMessage conversion', () {
test('should convert Message to DraftMessage correctly', () {
final attachments = [
Attachment(type: 'image'),
Attachment(type: 'file'),
Attachment(type: 'image', uploadState: const UploadState.success()),
Attachment(type: 'file', uploadState: const UploadState.success()),
];
final mentionedUsers = [
User(id: 'user1'),
Expand Down Expand Up @@ -355,10 +356,51 @@ void main() {
expect(draftMessage.extraData, equals(message.extraData));
});

test('should only include successfully uploaded attachments', () {
final successfulAttachment = Attachment(
id: 'success-attachment',
type: 'image',
uploadState: const UploadState.success(),
);
final preparingAttachment = Attachment(
id: 'preparing-attachment',
type: 'file',
uploadState: const UploadState.preparing(),
);
final inProgressAttachment = Attachment(
id: 'progress-attachment',
type: 'image',
uploadState: const UploadState.inProgress(uploaded: 50, total: 100),
);
final failedAttachment = Attachment(
id: 'failed-attachment',
type: 'file',
uploadState: const UploadState.failed(error: 'Upload failed'),
);

final message = Message(
id: 'test-message',
text: 'Test message with mixed upload states',
attachments: [
successfulAttachment,
preparingAttachment,
inProgressAttachment,
failedAttachment,
],
);

final draftMessage = message.toDraftMessage();

// Only the successfully uploaded attachment should be included
expect(draftMessage.attachments.length, equals(1));
expect(draftMessage.attachments.first.id, equals('success-attachment'));
expect(draftMessage.attachments.first.uploadState.isSuccess, isTrue);
});

test('should convert DraftMessage to Message correctly', () {
final attachments = [
Attachment(type: 'image'),
Attachment(type: 'file'),
Attachment(type: 'image', uploadState: const UploadState.success()),
Attachment(type: 'file', uploadState: const UploadState.success()),
];
final mentionedUsers = [
User(id: 'user1'),
Expand Down