Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(files,comments): prevent duplicate file rows and handle undefined fileId

Jan Wiebec04adef2cc74
diff --git a/backend/src/applications/comments/services/comments-manager.service.spec.ts b/backend/src/applications/comments/services/comments-manager.service.spec.ts
index 8da0a0c8..91be7e1a 100644
--- a/backend/src/applications/comments/services/comments-manager.service.spec.ts
+++ b/backend/src/applications/comments/services/comments-manager.service.spec.ts
@@ -159,6 +159,21 @@ describe(CommentsManager.name, () => {
})
})
+ it('uses getOrCreate when fileId > 0 but file is not yet indexed', async () => {
+ filesQueries.getSpaceFileId.mockResolvedValue(undefined)
+ filesQueries.getOrCreateSpaceFile.mockResolvedValue(77)
+ commentQueries.createComment.mockResolvedValue(1)
+ commentQueries.getComments.mockResolvedValue([{ id: 1, fileId: 77, content: 'hi' }])
+ commentQueries.membersToNotify.mockResolvedValue([])
+
+ const res = await commentsManager.createComment(user, makeSpace(), { fileId: 42, content: 'hi' } as any)
+
+ expect(filesQueries.getSpaceFileId).toHaveBeenCalledTimes(1)
+ expect(filesQueries.getOrCreateSpaceFile).toHaveBeenCalledWith(42, expect.anything(), expect.anything())
+ expect(commentQueries.createComment).toHaveBeenCalledWith(42, 77, 'hi')
+ expect(res).toMatchObject({ id: 1, fileId: 77, content: 'hi' })
+ })
+
it('uses getOrCreate when fileId < 0', async () => {
filesQueries.getOrCreateSpaceFile.mockResolvedValue(555)
commentQueries.createComment.mockResolvedValue(777)
@@ -269,6 +284,15 @@ describe(CommentsManager.name, () => {
})
})
+ it('rejects BAD_REQUEST if file is not indexed (getSpaceFileId returns undefined)', async () => {
+ commentQueries.getComments.mockResolvedValue([{ id: 50, fileId: 5 }])
+ filesQueries.getSpaceFileId.mockResolvedValue(undefined)
+
+ await expect(commentsManager.updateComment(user, makeSpace(), { commentId: 50, fileId: 5, content: 'z' } as any)).rejects.toMatchObject({
+ status: HttpStatus.BAD_REQUEST
+ })
+ })
+
it('rejects INTERNAL_SERVER_ERROR if update fails', async () => {
commentQueries.getComments.mockResolvedValueOnce([{ id: 50, fileId: 5 }])
filesQueries.getSpaceFileId.mockResolvedValue(5)
@@ -303,6 +327,14 @@ describe(CommentsManager.name, () => {
})
})
+ it('rejects BAD_REQUEST if file is not indexed (getSpaceFileId returns undefined)', async () => {
+ filesQueries.getSpaceFileId.mockResolvedValue(undefined)
+
+ await expect(commentsManager.deleteComment(user, makeSpace(), { commentId: 1, fileId: 10 } as any)).rejects.toMatchObject({
+ status: HttpStatus.BAD_REQUEST
+ })
+ })
+
it('rejects FORBIDDEN if deletion is denied', async () => {
filesQueries.getSpaceFileId.mockResolvedValue(10)
commentQueries.deleteComment.mockResolvedValue(false)
diff --git a/backend/src/applications/comments/services/comments-manager.service.ts b/backend/src/applications/comments/services/comments-manager.service.ts
index ba1bb1d5..5520ad07 100644
--- a/backend/src/applications/comments/services/comments-manager.service.ts
+++ b/backend/src/applications/comments/services/comments-manager.service.ts
@@ -42,9 +42,13 @@ export class CommentsManager {
}
let fileId: number
if (createCommentDto.fileId > 0) {
- fileId = await this.getFileId(space)
- if (createCommentDto.fileId !== fileId) {
+ const dbFileId = await this.getFileId(space)
+ if (dbFileId === undefined) {
+ fileId = await this.getFileId(space, createCommentDto.fileId)
+ } else if (createCommentDto.fileId !== dbFileId) {
throw new HttpException('File id mismatch', HttpStatus.BAD_REQUEST)
+ } else {
+ fileId = dbFileId
}
} else {
fileId = await this.getFileId(space, createCommentDto.fileId)
@@ -59,8 +63,8 @@ export class CommentsManager {
if (!comment) {
throw new HttpException('Location not found', HttpStatus.NOT_FOUND)
}
- const fileId: number = await this.getFileId(space)
- if (updateCommentDto.fileId !== fileId || comment.fileId !== fileId) {
+ const fileId = await this.getFileId(space)
+ if (fileId === undefined || updateCommentDto.fileId !== fileId || comment.fileId !== fileId) {
throw new HttpException('File id mismatch', HttpStatus.BAD_REQUEST)
}
if (!(await this.commentQueries.updateComment(user.id, updateCommentDto.commentId, updateCommentDto.fileId, updateCommentDto.content))) {
@@ -70,8 +74,8 @@ export class CommentsManager {
}
async deleteComment(user: UserModel, space: SpaceEnv, deleteCommentDto: DeleteCommentDto): Promise<void> {
- const fileId: number = await this.getFileId(space)
- if (deleteCommentDto.fileId !== fileId) {
+ const fileId = await this.getFileId(space)
+ if (fileId === undefined || deleteCommentDto.fileId !== fileId) {
throw new HttpException('File id mismatch', HttpStatus.BAD_REQUEST)
}
if (!(await this.commentQueries.deleteComment(user.id, deleteCommentDto.commentId, fileId, space.dbFile?.ownerId === user.id))) {
@@ -79,7 +83,7 @@ export class CommentsManager {
}
}
- private async getFileId(space: SpaceEnv, fileId?: number): Promise<number> {
+ private async getFileId(space: SpaceEnv, fileId?: number): Promise<number | undefined> {
if (!(await isPathExists(space.realPath))) {
throw new HttpException('Location not found', HttpStatus.NOT_FOUND)
}
diff --git a/backend/src/applications/files/services/files-queries.service.ts b/backend/src/applications/files/services/files-queries.service.ts
index 2ba73891..7ba88387 100644
--- a/backend/src/applications/files/services/files-queries.service.ts
+++ b/backend/src/applications/files/services/files-queries.service.ts
@@ -135,6 +135,10 @@ export class FilesQueries {
})
}
}
+ // Before inserting, check whether a record already exists at this path to avoid
+ // creating duplicate rows when concurrent or repeated calls race to index the same file.
+ const existingId = await this.getSpaceFileId(file, dbFile)
+ if (existingId !== undefined) return existingId
// order is important, path is replaced by the FileProps.path
return dbGetInsertedId(await this.db.insert(files).values({ ...dbFile, ...file }))
}