Skip to content

Commit e83a790

Browse files
authored
fix(llc): only include uploaded attachments in draft (#2388)
1 parent 1c9f078 commit e83a790

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

packages/stream_chat/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Upcoming
2+
3+
🐞 Fixed
4+
5+
- Fixed `toDraftMessage` to only include successfully uploaded attachments in draft messages.
6+
17
## 9.16.0
28

39
🐞 Fixed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,16 @@ extension MessageToDraftMessage on Message {
189189
/// This is useful when you want to convert a message to a draft message
190190
/// before sending it to the server.
191191
DraftMessage toDraftMessage() {
192+
// Only include attachments that have been successfully uploaded.
193+
final uploadedAttachments = attachments.where((it) {
194+
return it.uploadState.isSuccess;
195+
}).toList();
196+
192197
return DraftMessage(
193198
id: id,
194199
text: text,
195200
type: type,
196-
attachments: attachments,
201+
attachments: uploadedAttachments,
197202
parentId: parentId,
198203
showInChannel: showInChannel,
199204
mentionedUsers: mentionedUsers,

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// ignore_for_file: avoid_redundant_argument_values
22

33
import 'package:stream_chat/src/core/models/attachment.dart';
4+
import 'package:stream_chat/src/core/models/attachment_file.dart';
45
import 'package:stream_chat/src/core/models/draft_message.dart';
56
import 'package:stream_chat/src/core/models/message.dart';
67
import 'package:stream_chat/src/core/models/poll.dart';
@@ -303,8 +304,8 @@ void main() {
303304
group('Message and DraftMessage conversion', () {
304305
test('should convert Message to DraftMessage correctly', () {
305306
final attachments = [
306-
Attachment(type: 'image'),
307-
Attachment(type: 'file'),
307+
Attachment(type: 'image', uploadState: const UploadState.success()),
308+
Attachment(type: 'file', uploadState: const UploadState.success()),
308309
];
309310
final mentionedUsers = [
310311
User(id: 'user1'),
@@ -355,10 +356,51 @@ void main() {
355356
expect(draftMessage.extraData, equals(message.extraData));
356357
});
357358

359+
test('should only include successfully uploaded attachments', () {
360+
final successfulAttachment = Attachment(
361+
id: 'success-attachment',
362+
type: 'image',
363+
uploadState: const UploadState.success(),
364+
);
365+
final preparingAttachment = Attachment(
366+
id: 'preparing-attachment',
367+
type: 'file',
368+
uploadState: const UploadState.preparing(),
369+
);
370+
final inProgressAttachment = Attachment(
371+
id: 'progress-attachment',
372+
type: 'image',
373+
uploadState: const UploadState.inProgress(uploaded: 50, total: 100),
374+
);
375+
final failedAttachment = Attachment(
376+
id: 'failed-attachment',
377+
type: 'file',
378+
uploadState: const UploadState.failed(error: 'Upload failed'),
379+
);
380+
381+
final message = Message(
382+
id: 'test-message',
383+
text: 'Test message with mixed upload states',
384+
attachments: [
385+
successfulAttachment,
386+
preparingAttachment,
387+
inProgressAttachment,
388+
failedAttachment,
389+
],
390+
);
391+
392+
final draftMessage = message.toDraftMessage();
393+
394+
// Only the successfully uploaded attachment should be included
395+
expect(draftMessage.attachments.length, equals(1));
396+
expect(draftMessage.attachments.first.id, equals('success-attachment'));
397+
expect(draftMessage.attachments.first.uploadState.isSuccess, isTrue);
398+
});
399+
358400
test('should convert DraftMessage to Message correctly', () {
359401
final attachments = [
360-
Attachment(type: 'image'),
361-
Attachment(type: 'file'),
402+
Attachment(type: 'image', uploadState: const UploadState.success()),
403+
Attachment(type: 'file', uploadState: const UploadState.success()),
362404
];
363405
final mentionedUsers = [
364406
User(id: 'user1'),

0 commit comments

Comments
 (0)