feat(webui): show comments count badge in file card preview area (#403)

* feat(webui): show comments count badge in file card preview area

* perf(db): add index on asset_comments asset_id
This commit is contained in:
Yiling-J
2026-09-13 21:12:21 +08:00
committed by GitHub
parent 87e88396d1
commit 7d56b5bf90
8 changed files with 288 additions and 3 deletions
+190
View File
@@ -4420,4 +4420,194 @@ describe('AssetService — natural sort by name', () => {
expect(folderRes.data.length).toBe(1)
})
})
describe('commentsCount in toAssetInfos', () => {
it('returns commentsCount 0 when file has no comments', async () => {
const team = await prisma.team.create({ data: { name: 'Test Team' } })
const project = await prisma.project.create({
data: { name: 'Test Project', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'root',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
const user = await prisma.user.create({
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
})
const file = await prisma.asset.create({
data: {
name: 'file.mp4',
type: AssetType.file,
projectId: project.id,
parentId: rootFolder.id,
creatorId: user.id,
status: AssetStatus.uploaded,
},
})
const res = await assetService.listChildren({
assetId: rootFolder.id,
assetType: 'file',
})
const fileInfo = res.data.find((a) => a.id === file.id)
expect(fileInfo).toBeDefined()
expect(fileInfo?.commentsCount).toBe(0)
})
it('returns total comments count including replies for a file', async () => {
const team = await prisma.team.create({ data: { name: 'Test Team' } })
const project = await prisma.project.create({
data: { name: 'Test Project', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'root',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
const user = await prisma.user.create({
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
})
const file = await prisma.asset.create({
data: {
name: 'video.mp4',
type: AssetType.file,
projectId: project.id,
parentId: rootFolder.id,
creatorId: user.id,
status: AssetStatus.uploaded,
},
})
const rootComment = await prisma.assetComment.create({
data: {
assetId: file.id,
creatorId: user.id,
message: 'Root comment 1',
},
})
await prisma.assetComment.create({
data: {
assetId: file.id,
creatorId: user.id,
message: 'Reply to comment 1',
replyToId: rootComment.id,
},
})
await prisma.assetComment.create({
data: {
assetId: file.id,
creatorId: user.id,
message: 'Root comment 2',
},
})
const res = await assetService.listChildren({
assetId: rootFolder.id,
assetType: 'file',
})
const fileInfo = res.data.find((a) => a.id === file.id)
expect(fileInfo).toBeDefined()
expect(fileInfo?.commentsCount).toBe(3)
})
it('returns comments count of the active version for version stack', async () => {
const team = await prisma.team.create({ data: { name: 'Test Team' } })
const project = await prisma.project.create({
data: { name: 'Test Project', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'root',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
const user = await prisma.user.create({
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
})
const stack = await prisma.asset.create({
data: {
name: 'video_stack',
type: AssetType.version_stack,
projectId: project.id,
parentId: rootFolder.id,
creatorId: user.id,
status: AssetStatus.uploaded,
},
})
// Older version (v1): larger sortIndex
const fileV1 = await prisma.asset.create({
data: {
name: 'video_v1.mp4',
type: AssetType.file,
projectId: project.id,
parentId: stack.id,
creatorId: user.id,
status: AssetStatus.uploaded,
sortIndex: 'b',
},
})
// Active / latest version (v2): smaller sortIndex
const fileV2 = await prisma.asset.create({
data: {
name: 'video_v2.mp4',
type: AssetType.file,
projectId: project.id,
parentId: stack.id,
creatorId: user.id,
status: AssetStatus.uploaded,
sortIndex: 'a',
},
})
// Add 2 comments to v1
await prisma.assetComment.create({
data: {
assetId: fileV1.id,
creatorId: user.id,
message: 'Comment on v1',
},
})
await prisma.assetComment.create({
data: {
assetId: fileV1.id,
creatorId: user.id,
message: 'Another comment on v1',
},
})
// Add 1 comment to v2 (active version)
await prisma.assetComment.create({
data: {
assetId: fileV2.id,
creatorId: user.id,
message: 'Comment on v2',
},
})
const res = await assetService.listChildren({
assetId: rootFolder.id,
assetType: 'file',
})
const stackInfo = res.data.find((a) => a.id === stack.id)
expect(stackInfo).toBeDefined()
// Should reflect active version (v2) comments count
expect(stackInfo?.commentsCount).toBe(1)
})
})
})
+33
View File
@@ -2026,6 +2026,38 @@ export class AssetService {
: []
const hasAgentMdSet = new Set(agentMdRecords.map((r) => r.assetId))
const commentTargetIds = new Set<string>()
for (const a of assets) {
if (
a.type === AssetType.version_stack ||
(a.type === AssetType.symlink && a.target?.type === AssetType.version_stack)
) {
const stackId = a.type === AssetType.symlink ? a.targetId! : a.id
const versions = versionsMap.get(stackId) || []
if (versions.length > 0) {
commentTargetIds.add(versions[0].id)
} else {
commentTargetIds.add(stackId)
}
} else if (a.type === AssetType.symlink && a.target) {
commentTargetIds.add(a.target.id)
} else {
commentTargetIds.add(a.id)
}
}
const commentCounts =
commentTargetIds.size > 0
? await this.prismaClient.assetComment.groupBy({
by: ['assetId'],
where: {
assetId: { in: Array.from(commentTargetIds) },
},
_count: { _all: true },
})
: []
const commentCountMap = new Map(commentCounts.map((c) => [c.assetId, c._count._all]))
const result: AssetInfo[] = []
for (const a of assets) {
let latestVersion:
@@ -2207,6 +2239,7 @@ export class AssetService {
fieldValues,
sortIndex: a.sortIndex,
hasAgentsMd: hasAgentMdSet.has(a.id),
commentsCount: commentCountMap.get(latestVersion.id) ?? 0,
media: media as unknown as AssetInfo['media'],
versionStack,
})