diff --git a/app/database/operator/server_data_operator/handlers/post.test.ts b/app/database/operator/server_data_operator/handlers/post.test.ts index 04d68cb6c..e6c1eaeb1 100644 --- a/app/database/operator/server_data_operator/handlers/post.test.ts +++ b/app/database/operator/server_data_operator/handlers/post.test.ts @@ -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 ***', () => { diff --git a/app/database/operator/server_data_operator/handlers/post.ts b/app/database/operator/server_data_operator/handlers/post.ts index 14793424f..a731a7ab1 100644 --- a/app/database/operator/server_data_operator/handlers/post.ts +++ b/app/database/operator/server_data_operator/handlers/post.ts @@ -157,6 +157,7 @@ const PostHandler = >(supercla const postsReactions: ReactionsPerPost[] = []; const pendingPostsToDelete: Post[] = []; const postsInThread: Record = {}; + const receivedFilesSet = new Set(); // Let's process the post data for (const post of posts) { @@ -201,6 +202,8 @@ const PostHandler = >(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 = >(supercla } const allFiles = await database.get(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());