-
Notifications
You must be signed in to change notification settings - Fork 13k
fix: ui does not update after pruning only files #36099
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4645bca
fix: prune-files-ui-not-updating
abhinavkrin 5385725
added changeset
abhinavkrin f53a810
update modifyMessageOnFilesDelete (review)
gabriellsh 756036f
review
gabriellsh 0c6fa26
update changeset
gabriellsh 7b00bd6
resolve conflicts
abhinavkrin 3b645b2
Merge branch 'develop' into regression/prune-files-ui-not-updating
kodiakhq[bot] 729ba6c
Merge branch 'develop' into regression/prune-files-ui-not-updating
kodiakhq[bot] 95a52f9
Merge branch 'develop' into regression/prune-files-ui-not-updating
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@rocket.chat/core-services': patch | ||
| '@rocket.chat/ddp-client': patch | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Fixes missing UI updates after pruning messages with "Files only" enabled. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
apps/meteor/client/lib/utils/modifyMessageOnFilesDelete.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import type { IMessage, MessageAttachment, FileAttachmentProps, MessageQuoteAttachment } from '@rocket.chat/core-typings'; | ||
|
|
||
| import { modifyMessageOnFilesDelete } from './modifyMessageOnFilesDelete'; | ||
|
|
||
| global.structuredClone = (val: any) => JSON.parse(JSON.stringify(val)); | ||
|
|
||
| const fileAttachment: FileAttachmentProps = { title: 'Image', title_link: 'url', image_url: 'image.png', type: 'file' }; | ||
| const nonFileAttachment: MessageAttachment = { text: 'Non-file attachment', color: '#ff0000' }; | ||
| const quoteAttachment: MessageQuoteAttachment = { | ||
| text: 'Quoted message', | ||
| message_link: 'some-link', | ||
| attachments: [{ text: 'Quoted inner message' }], | ||
| author_icon: 'icon', | ||
| author_link: 'link', | ||
| author_name: 'name', | ||
| }; | ||
|
|
||
| const messageBase: IMessage = { | ||
| _id: 'msg1', | ||
| msg: 'Here is a file', | ||
| file: { | ||
| _id: 'file1', | ||
| name: 'image.png', | ||
| type: '', | ||
| format: '', | ||
| size: 0, | ||
| }, | ||
| files: [ | ||
| { | ||
| _id: 'file1', | ||
| name: 'image.png', | ||
| type: '', | ||
| format: '', | ||
| size: 0, | ||
| }, | ||
| ], | ||
| attachments: [fileAttachment, nonFileAttachment, quoteAttachment], | ||
| rid: '', | ||
| ts: new Date().toISOString() as any, | ||
| u: { username: 'username', _id: '12345' }, | ||
| _updatedAt: new Date().toISOString() as any, | ||
| }; | ||
|
|
||
| const createMessage = () => { | ||
| const msg = structuredClone(messageBase); | ||
| return msg as IMessage; | ||
| }; | ||
|
|
||
| describe('modifyMessageOnFilesDelete', () => { | ||
| it('should remove `file`, empty `files`, and remove file-type attachments', () => { | ||
| const message = createMessage(); | ||
|
|
||
| const result = modifyMessageOnFilesDelete(message); | ||
|
|
||
| expect(result).not.toHaveProperty('file'); | ||
| expect(Array.isArray(result.files)).toBe(true); | ||
| expect(result.files).toHaveLength(0); | ||
| expect(result.attachments).toHaveLength(2); | ||
| expect(result.attachments?.some((att) => att.text === 'Non-file attachment')).toBe(true); | ||
| }); | ||
|
|
||
| it('should replace file-type attachments if `replaceFileAttachmentsWith` is provided', () => { | ||
| const message = createMessage(); | ||
|
|
||
| const replacement: MessageAttachment = { text: 'File removed by prune' }; | ||
|
|
||
| const result = modifyMessageOnFilesDelete(message, replacement); | ||
|
|
||
| expect(result).not.toHaveProperty('file'); | ||
| expect(Array.isArray(result.files)).toBe(true); | ||
| expect(result.files).toHaveLength(0); | ||
| expect(result.attachments?.some((att) => att.text === 'File removed by prune')).toBe(true); | ||
| }); | ||
|
|
||
| it('should not mutate the original message', () => { | ||
| const message = createMessage(); | ||
|
|
||
| const original = JSON.parse(JSON.stringify(message)); | ||
| modifyMessageOnFilesDelete(message); | ||
|
|
||
| expect(message).toEqual(original); | ||
| }); | ||
|
|
||
| it('should not remove non-file attachments such as text and quote', () => { | ||
| const message = createMessage(); | ||
|
|
||
| const result = modifyMessageOnFilesDelete(message); | ||
|
|
||
| expect(result.attachments?.some((att) => att.text === 'Non-file attachment')).toBe(true); | ||
| expect(result.attachments?.some((att) => att.text === 'Quoted message')).toBe(true); | ||
| }); | ||
| }); |
30 changes: 30 additions & 0 deletions
30
apps/meteor/client/lib/utils/modifyMessageOnFilesDelete.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { isFileAttachment } from '@rocket.chat/core-typings'; | ||
| import type { IMessage, MessageAttachment } from '@rocket.chat/core-typings'; | ||
|
|
||
| /** | ||
| * Clone a message and clear or replace its file attachments. | ||
| * | ||
| * - Performs a deep clone via `structuredClone` to avoid mutating the source. | ||
| * - Removes the single-file `file` field and empties the `files` array. | ||
| * - If `replaceFileAttachmentsWith` is given, swaps out any file-type | ||
| * attachments; otherwise filters them out entirely. | ||
| * | ||
| * @param message The original `IMessage` to copy. | ||
| * @param replaceFileAttachmentsWith | ||
| * Optional attachment to substitute for each file attachment. | ||
| * @returns A new message object with `file`, `files`, and `attachments` updated. | ||
| */ | ||
|
|
||
| export const modifyMessageOnFilesDelete = <T extends IMessage = IMessage>(message: T, replaceFileAttachmentsWith?: MessageAttachment) => { | ||
| const { attachments, file: _, ...rest } = message; | ||
|
|
||
| if (!attachments?.length) { | ||
| return message; | ||
| } | ||
|
|
||
| if (replaceFileAttachmentsWith) { | ||
| return { ...rest, files: [], attachments: attachments?.map((att) => (isFileAttachment(att) ? replaceFileAttachmentsWith : att)) }; | ||
| } | ||
|
|
||
| return { ...rest, files: [], attachments: attachments?.filter((att) => !isFileAttachment(att)) }; | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.