Check file id instead of metadata for file removal (#8549) (#8551)

(cherry picked from commit 46f1cf3969)

Co-authored-by: Daniel Espino García <larkox@gmail.com>
This commit is contained in:
Mattermost Build 2025-02-03 16:08:16 +02:00 committed by GitHub
parent 7df4619de3
commit 7d968f6dfe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 49 additions and 1 deletions

View file

@ -44,6 +44,7 @@ describe('*** Operator: Post Handlers tests ***', () => {
reply_count: 4,
last_reply_at: 0,
participants: null,
file_ids: ['f1oxe5rtepfs7n3zifb4sso7po'],
metadata: {
images: {
'https://community-release.mattermost.com/api/v4/image?url=https%3A%2F%2Favatars1.githubusercontent.com%2Fu%2F6913320%3Fs%3D400%26v%3D4': {
@ -381,6 +382,7 @@ describe('*** Operator: Post Handlers tests ***', () => {
...postWithMetadata.metadata,
files: [],
},
file_ids: [],
},
];
@ -443,6 +445,7 @@ describe('*** Operator: Post Handlers tests ***', () => {
},
],
},
file_ids: [...postWithMetadata.file_ids!, 'another-file-id'],
},
];
@ -505,6 +508,7 @@ describe('*** Operator: Post Handlers tests ***', () => {
},
],
},
file_ids: ['another-file-id'],
},
];
@ -537,6 +541,48 @@ describe('*** Operator: Post Handlers tests ***', () => {
expect(files).toHaveLength(1);
expect(files[0].id).toBe('another-file-id');
});
it('=> HandlePosts: should not remove files if file ids are present but metadata is missing', async () => {
const postWithMetadata = posts[0];
const uploadedFiles = postWithMetadata.metadata.files!;
const updatedPosts: Post[] = [
{
...postWithMetadata,
update_at: Date.now(),
metadata: {}, // No metadata
file_ids: postWithMetadata.file_ids, // File IDs are still present
},
];
const order = [
'8swgtrrdiff89jnsiwiip3y1eoe',
'8fcnk3p1jt8mmkaprgajoxz115a',
'3y3w3a6gkbg73bnj3xund9o5ic',
];
const actionType = ActionType.POSTS.RECEIVED_IN_CHANNEL;
await operator.handlePosts({
actionType,
order,
posts,
prepareRecordsOnly: false,
});
let files = await operator.database.get('File').query(Q.where('post_id', postWithMetadata.id)).fetch();
expect(files).toHaveLength(1);
expect(files[0].id).toBe(uploadedFiles[0].id);
await operator.handlePosts({
actionType,
order: [uploadedFiles[0].id!],
posts: updatedPosts,
});
files = await operator.database.get('File').query(Q.where('post_id', postWithMetadata.id)).fetch();
expect(files).toHaveLength(1);
expect(files[0].id).toBe(uploadedFiles[0].id);
});
});
describe('*** Operator: merge chunks ***', () => {

View file

@ -157,6 +157,7 @@ const PostHandler = <TBase extends Constructor<ServerDataOperatorBase>>(supercla
const postsReactions: ReactionsPerPost[] = [];
const pendingPostsToDelete: Post[] = [];
const postsInThread: Record<string, Post[]> = {};
const receivedFilesSet = new Set<string>();
// Let's process the post data
for (const post of posts) {
@ -201,6 +202,8 @@ const PostHandler = <TBase extends Constructor<ServerDataOperatorBase>>(supercla
post.metadata = data;
}
post.file_ids?.forEach((fileId) => receivedFilesSet.add(fileId));
}
// Get unique posts in case they are duplicated
@ -261,7 +264,6 @@ const PostHandler = <TBase extends Constructor<ServerDataOperatorBase>>(supercla
}
const allFiles = await database.get<FileModel>(MM_TABLES.SERVER.FILE).query(Q.where('post_id', Q.oneOf(uniquePosts.map((p) => p.id)))).fetch();
const receivedFilesSet = new Set(files.map((f) => f.id));
allFiles.forEach((f) => {
if (!receivedFilesSet.has(f.id)) {
batch.push(f.prepareDestroyPermanently());