Skip to content

fix: upsert attachmemts only when attachment exist and handle FileList for react native #1539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
63 changes: 42 additions & 21 deletions src/messageComposer/attachmentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,32 +182,53 @@ export class AttachmentManager {
attachment.localMetadata.id && localId === attachment.localMetadata?.id,
);

upsertAttachments = (attachmentsToUpsert: LocalAttachment[]) => {
if (!attachmentsToUpsert.length) return;
private prepareAttachmentUpdate = (attachmentToUpdate: LocalAttachment) => {
const stateAttachments = this.attachments;
const attachments = [...this.attachments];
attachmentsToUpsert.forEach((upsertedAttachment) => {
const attachmentIndex = this.getAttachmentIndex(
upsertedAttachment.localMetadata.id,
);
const attachmentIndex = this.getAttachmentIndex(attachmentToUpdate.localMetadata.id);
if (attachmentIndex === -1) return null;
const merged = mergeWithDiff<LocalAttachment>(
stateAttachments[attachmentIndex] ?? {},
attachmentToUpdate,
);
const updatesOnMerge = merged.diff && Object.keys(merged.diff.children).length;
if (updatesOnMerge) {
const localAttachment = ensureIsLocalAttachment(merged.result);
if (localAttachment) {
attachments.splice(attachmentIndex, 1, localAttachment);
return attachments;
}
}
return null;
};

if (attachmentIndex === -1) {
const localAttachment = ensureIsLocalAttachment(upsertedAttachment);
if (localAttachment) attachments.push(localAttachment);
updateAttachment = (attachmentToUpdate: LocalAttachment) => {
const updatedAttachments = this.prepareAttachmentUpdate(attachmentToUpdate);
if (updatedAttachments) {
this.state.partialNext({ attachments: updatedAttachments });
}
};

upsertAttachments = (attachmentsToUpsert: LocalAttachment[]) => {
if (!attachmentsToUpsert.length) return;
let attachments = [...this.attachments];
let hasUpdates = false;
attachmentsToUpsert.forEach((attachment) => {
const updatedAttachments = this.prepareAttachmentUpdate(attachment);
if (updatedAttachments) {
attachments = updatedAttachments;
hasUpdates = true;
} else {
const merged = mergeWithDiff<LocalAttachment>(
stateAttachments[attachmentIndex] ?? {},
upsertedAttachment,
);
const updatesOnMerge = merged.diff && Object.keys(merged.diff.children).length;
if (updatesOnMerge) {
const localAttachment = ensureIsLocalAttachment(merged.result);
if (localAttachment) attachments.splice(attachmentIndex, 1, localAttachment);
const localAttachment = ensureIsLocalAttachment(attachment);
if (localAttachment) {
attachments.push(localAttachment);
hasUpdates = true;
}
}
});

this.state.partialNext({ attachments });
if (hasUpdates) {
this.state.partialNext({ attachments });
}
};

removeAttachments = (localAttachmentIds: string[]) => {
Expand Down Expand Up @@ -448,7 +469,7 @@ export class AttachmentManager {
},
};

this.upsertAttachments([failedAttachment]);
this.updateAttachment(failedAttachment);
return failedAttachment;
}

Expand Down Expand Up @@ -482,7 +503,7 @@ export class AttachmentManager {
(uploadedAttachment as LocalNotImageAttachment).thumb_url = response.thumb_url;
}

this.upsertAttachments([uploadedAttachment]);
this.updateAttachment(uploadedAttachment);

return uploadedAttachment;
};
Expand Down
2 changes: 1 addition & 1 deletion src/messageComposer/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const isFileList = (obj: unknown): obj is FileList => {
if (typeof obj !== 'object') return false;

return (
(obj as object) instanceof FileList ||
(typeof FileList !== 'undefined' && (obj as object) instanceof FileList) ||
('item' in obj && 'length' in obj && !Array.isArray(obj))
);
};
Expand Down
51 changes: 51 additions & 0 deletions test/unit/MessageComposer/attachmentManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,57 @@ describe('AttachmentManager', () => {
});
});

describe('updateAttachment', () => {
it('should update an attachment by id', () => {
const {
messageComposer: { attachmentManager },
} = setup();

const newAttachments = [
{ localMetadata: { id: 'test-id-1' }, type: 'image' },
{ localMetadata: { id: 'test-id-2' }, type: 'video' },
];

attachmentManager.upsertAttachments(newAttachments);

const updatedAttachment = {
id: 'test-id-1',
localMetadata: { id: 'test-id-1' },
type: 'audio',
};

attachmentManager.updateAttachment(updatedAttachment);

expect(attachmentManager.attachments).toEqual([
updatedAttachment,
newAttachments[1],
]);
});

it('should not update an attachment if id is not found', () => {
const {
messageComposer: { attachmentManager },
} = setup();

const newAttachments = [
{ localMetadata: { id: 'test-id-1' }, type: 'image' },
{ localMetadata: { id: 'test-id-2' }, type: 'video' },
];

attachmentManager.upsertAttachments(newAttachments);

const updatedAttachment = {
id: 'non-existent-id',
localMetadata: { id: 'non-existent-id' },
type: 'audio',
};

attachmentManager.updateAttachment(updatedAttachment);

expect(attachmentManager.attachments).toEqual(newAttachments);
});
});

describe('removeAttachments', () => {
it('should remove attachments by id', () => {
const {
Expand Down
Loading