mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 11:24:43 +00:00
feat(comment): add emoji reaction support for comments (#405)
* feat(comment): add emoji reaction support for comments * fix(webui): adjust emoji picker trigger icon size to match mark-done icon * fix(webui): prevent search icon and placeholder overlap in emoji picker * fix(webui): use normal text color for reaction count * feat(db): limit comment reaction code column to VarChar(32)
This commit is contained in:
@@ -1117,6 +1117,104 @@ describe('AssetService', () => {
|
||||
expect(incompleted.completionLastChangedBy?.id).toBe(user.id)
|
||||
})
|
||||
|
||||
describe('Comment Reactions', () => {
|
||||
it('adds, removes, and lists reactions chronologically with creatorNames', async () => {
|
||||
const { user, assets } = await setupBasicAssets()
|
||||
|
||||
const user2 = await prisma.user.create({
|
||||
data: {
|
||||
name: 'Bob',
|
||||
type: 'human',
|
||||
email: `bob-${Date.now()}@example.com`,
|
||||
},
|
||||
})
|
||||
|
||||
const user3 = await prisma.user.create({
|
||||
data: {
|
||||
name: 'Charlie',
|
||||
type: 'human',
|
||||
email: `charlie-${Date.now()}@example.com`,
|
||||
},
|
||||
})
|
||||
|
||||
const comment = await assetService.createComment({
|
||||
assetId: assets.fileA1.id,
|
||||
userId: user.id,
|
||||
message: 'A wonderful comment',
|
||||
attachmentIds: [],
|
||||
})
|
||||
|
||||
// Add reaction 1: Bob adds 👍
|
||||
await assetService.addCommentReaction(comment.id, user2.id, '👍')
|
||||
// Idempotency: Bob adds 👍 again
|
||||
await assetService.addCommentReaction(comment.id, user2.id, '👍')
|
||||
|
||||
// Add reaction 2: Charlie adds ❤️
|
||||
await assetService.addCommentReaction(comment.id, user3.id, '❤️')
|
||||
|
||||
// Add reaction 3: Alice (user) adds 👍
|
||||
await assetService.addCommentReaction(comment.id, user.id, '👍')
|
||||
|
||||
// Fetch as user (Alice)
|
||||
const commentAsAlice = await assetService.getComment(comment.id, user.id)
|
||||
expect(commentAsAlice.reactionCounts).toHaveLength(2)
|
||||
|
||||
// First reaction added was 👍, then ❤️
|
||||
expect(commentAsAlice.reactionCounts[0].code).toBe('👍')
|
||||
expect(commentAsAlice.reactionCounts[0].count).toBe(2)
|
||||
expect(commentAsAlice.reactionCounts[0].requestingUserReacted).toBe(true)
|
||||
expect(commentAsAlice.reactionCounts[0].creatorNames).toEqual(['Bob'])
|
||||
|
||||
expect(commentAsAlice.reactionCounts[1].code).toBe('❤️')
|
||||
expect(commentAsAlice.reactionCounts[1].count).toBe(1)
|
||||
expect(commentAsAlice.reactionCounts[1].requestingUserReacted).toBe(false)
|
||||
expect(commentAsAlice.reactionCounts[1].creatorNames).toEqual(['Charlie'])
|
||||
|
||||
// Fetch as Bob
|
||||
const commentAsBob = await assetService.getComment(comment.id, user2.id)
|
||||
expect(commentAsBob.reactionCounts[0].code).toBe('👍')
|
||||
expect(commentAsBob.reactionCounts[0].requestingUserReacted).toBe(true)
|
||||
expect(commentAsBob.reactionCounts[0].creatorNames).toEqual([user.name])
|
||||
|
||||
// Remove Alice's 👍
|
||||
await assetService.removeCommentReaction(comment.id, user.id, '👍')
|
||||
// Idempotency
|
||||
await assetService.removeCommentReaction(comment.id, user.id, '👍')
|
||||
|
||||
const updatedAlice = await assetService.getComment(comment.id, user.id)
|
||||
expect(updatedAlice.reactionCounts[0].code).toBe('👍')
|
||||
expect(updatedAlice.reactionCounts[0].count).toBe(1)
|
||||
expect(updatedAlice.reactionCounts[0].requestingUserReacted).toBe(false)
|
||||
expect(updatedAlice.reactionCounts[0].creatorNames).toEqual(['Bob'])
|
||||
|
||||
// Verify listComments returns reactions for comments and replies
|
||||
const reply = await assetService.createComment({
|
||||
assetId: assets.fileA1.id,
|
||||
userId: user2.id,
|
||||
message: 'A nice reply',
|
||||
replyToId: comment.id,
|
||||
attachmentIds: [],
|
||||
})
|
||||
await assetService.addCommentReaction(reply.id, user.id, '🎉')
|
||||
|
||||
const listRes = await assetService.listComments(assets.fileA1.id, { first: 10 }, user.id)
|
||||
const listedComment = listRes.data.find((c) => c.id === comment.id)
|
||||
expect(listedComment).toBeDefined()
|
||||
expect(listedComment?.reactionCounts[0].code).toBe('👍')
|
||||
expect(listedComment?.replies).toHaveLength(1)
|
||||
expect(listedComment?.replies[0].reactionCounts).toHaveLength(1)
|
||||
expect(listedComment?.replies[0].reactionCounts[0].code).toBe('🎉')
|
||||
expect(listedComment?.replies[0].reactionCounts[0].requestingUserReacted).toBe(true)
|
||||
})
|
||||
|
||||
it('throws error when adding reaction to nonexistent comment', async () => {
|
||||
const { user } = await setupBasicAssets()
|
||||
await expect(
|
||||
assetService.addCommentReaction('nonexistent-id', user.id, '👍'),
|
||||
).rejects.toThrow('Comment not found')
|
||||
})
|
||||
})
|
||||
|
||||
it('can export comments in all supported NLE formats', async () => {
|
||||
const { user, assets } = await setupBasicAssets()
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
AttachmentInfo,
|
||||
ChildPreview,
|
||||
CommentInfo,
|
||||
CommentReactionCount,
|
||||
CopyAssetsRequest,
|
||||
CreateAssetRequest,
|
||||
CreateCommentRequest,
|
||||
@@ -86,11 +87,21 @@ type CommentWithIncludes = Prisma.AssetCommentGetPayload<{
|
||||
creator: true
|
||||
completionLastChangedBy: true
|
||||
attachments: { include: { asset: { include: { storageKey: true } } } }
|
||||
reactions: {
|
||||
include: {
|
||||
user: { select: { id: true; name: true } }
|
||||
}
|
||||
}
|
||||
replies: {
|
||||
include: {
|
||||
creator: true
|
||||
completionLastChangedBy: true
|
||||
attachments: { include: { asset: { include: { storageKey: true } } } }
|
||||
reactions: {
|
||||
include: {
|
||||
user: { select: { id: true; name: true } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1707,10 +1718,10 @@ export class AssetService {
|
||||
}
|
||||
})
|
||||
|
||||
return this.getComment(commentId)
|
||||
return this.getComment(commentId, req.userId)
|
||||
}
|
||||
|
||||
async getComment(commentId: string): Promise<CommentInfo> {
|
||||
async getComment(commentId: string, requestingUserId?: string): Promise<CommentInfo> {
|
||||
const c = await this.prismaClient.assetComment.findUnique({
|
||||
where: { id: commentId },
|
||||
include: {
|
||||
@@ -1718,18 +1729,65 @@ export class AssetService {
|
||||
completionLastChangedBy: true,
|
||||
asset: { include: { storageKey: true } },
|
||||
attachments: { include: { asset: { include: { storageKey: true } } } },
|
||||
reactions: {
|
||||
include: {
|
||||
user: { select: { id: true, name: true } },
|
||||
},
|
||||
orderBy: { createdAt: 'asc' },
|
||||
},
|
||||
replies: {
|
||||
include: {
|
||||
creator: true,
|
||||
completionLastChangedBy: true,
|
||||
attachments: { include: { asset: { include: { storageKey: true } } } },
|
||||
reactions: {
|
||||
include: {
|
||||
user: { select: { id: true, name: true } },
|
||||
},
|
||||
orderBy: { createdAt: 'asc' },
|
||||
},
|
||||
},
|
||||
orderBy: { id: 'asc' },
|
||||
},
|
||||
},
|
||||
})
|
||||
if (!c) throw new Error('Comment not found')
|
||||
return this.toCommentInfo(c)
|
||||
return this.toCommentInfo(c, requestingUserId)
|
||||
}
|
||||
|
||||
async addCommentReaction(commentId: string, userId: string, code: string): Promise<void> {
|
||||
const comment = await this.prismaClient.assetComment.findUnique({
|
||||
where: { id: commentId },
|
||||
select: { id: true },
|
||||
})
|
||||
if (!comment) throw new Error('Comment not found')
|
||||
|
||||
await this.prismaClient.assetCommentReaction.upsert({
|
||||
where: {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
commentId_userId_code: {
|
||||
commentId,
|
||||
userId,
|
||||
code,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
commentId,
|
||||
userId,
|
||||
code,
|
||||
},
|
||||
update: {},
|
||||
})
|
||||
}
|
||||
|
||||
async removeCommentReaction(commentId: string, userId: string, code: string): Promise<void> {
|
||||
await this.prismaClient.assetCommentReaction.deleteMany({
|
||||
where: {
|
||||
commentId,
|
||||
userId,
|
||||
code,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async completeComment(
|
||||
@@ -1744,7 +1802,7 @@ export class AssetService {
|
||||
completionLastChangedById: userId,
|
||||
},
|
||||
})
|
||||
return this.getComment(commentId)
|
||||
return this.getComment(commentId, userId)
|
||||
}
|
||||
|
||||
async deleteComment({ commentId, userId }: { commentId: string; userId: string }): Promise<void> {
|
||||
@@ -1835,6 +1893,7 @@ export class AssetService {
|
||||
async listComments(
|
||||
assetId: string,
|
||||
params: PaginationParams,
|
||||
requestingUserId?: string,
|
||||
): Promise<PaginatedData<CommentInfo[]>> {
|
||||
const resolvedAssetId = await this.resolveLatestVersionId(assetId)
|
||||
|
||||
@@ -1847,11 +1906,23 @@ export class AssetService {
|
||||
completionLastChangedBy: true,
|
||||
asset: { include: { storageKey: true } },
|
||||
attachments: { include: { asset: { include: { storageKey: true } } } },
|
||||
reactions: {
|
||||
include: {
|
||||
user: { select: { id: true, name: true } },
|
||||
},
|
||||
orderBy: { createdAt: 'asc' },
|
||||
},
|
||||
replies: {
|
||||
include: {
|
||||
creator: true,
|
||||
completionLastChangedBy: true,
|
||||
attachments: { include: { asset: { include: { storageKey: true } } } },
|
||||
reactions: {
|
||||
include: {
|
||||
user: { select: { id: true, name: true } },
|
||||
},
|
||||
orderBy: { createdAt: 'asc' },
|
||||
},
|
||||
},
|
||||
orderBy: { id: 'asc' },
|
||||
},
|
||||
@@ -1865,7 +1936,7 @@ export class AssetService {
|
||||
params,
|
||||
)
|
||||
|
||||
const infos = await Promise.all(comments.map((c) => this.toCommentInfo(c)))
|
||||
const infos = await Promise.all(comments.map((c) => this.toCommentInfo(c, requestingUserId)))
|
||||
|
||||
return { data: infos, pageInfo }
|
||||
}
|
||||
@@ -2575,11 +2646,12 @@ export class AssetService {
|
||||
|
||||
private async toCommentInfo(
|
||||
c: CommentWithIncludes | CommentWithIncludes['replies'][0],
|
||||
requestingUserId?: string,
|
||||
): Promise<CommentInfo> {
|
||||
const replies: CommentInfo[] = []
|
||||
if ('replies' in c && c.replies) {
|
||||
for (const r of c.replies) {
|
||||
replies.push(await this.toCommentInfo(r))
|
||||
replies.push(await this.toCommentInfo(r, requestingUserId))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2643,6 +2715,49 @@ export class AssetService {
|
||||
}
|
||||
: null
|
||||
|
||||
const reactionsMap = new Map<
|
||||
string,
|
||||
{
|
||||
count: number
|
||||
requestingUserReacted: boolean
|
||||
creatorNames: string[]
|
||||
firstCreatedAt: Date
|
||||
}
|
||||
>()
|
||||
|
||||
if ('reactions' in c && c.reactions) {
|
||||
for (const reaction of c.reactions) {
|
||||
let entry = reactionsMap.get(reaction.code)
|
||||
if (!entry) {
|
||||
entry = {
|
||||
count: 0,
|
||||
requestingUserReacted: false,
|
||||
creatorNames: [],
|
||||
firstCreatedAt: reaction.createdAt,
|
||||
}
|
||||
reactionsMap.set(reaction.code, entry)
|
||||
}
|
||||
entry.count++
|
||||
if (requestingUserId && reaction.userId === requestingUserId) {
|
||||
entry.requestingUserReacted = true
|
||||
} else {
|
||||
const userName = reaction.user?.name || 'User'
|
||||
if (!entry.creatorNames.includes(userName)) {
|
||||
entry.creatorNames.push(userName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const reactionCounts: CommentReactionCount[] = Array.from(reactionsMap.entries())
|
||||
.sort((a, b) => a[1].firstCreatedAt.getTime() - b[1].firstCreatedAt.getTime())
|
||||
.map(([code, data]) => ({
|
||||
code,
|
||||
count: data.count,
|
||||
requestingUserReacted: data.requestingUserReacted,
|
||||
creatorNames: data.creatorNames,
|
||||
}))
|
||||
|
||||
return {
|
||||
id: c.id,
|
||||
assetId: c.assetId,
|
||||
@@ -2658,6 +2773,7 @@ export class AssetService {
|
||||
sessionId: c.sessionId,
|
||||
isCompleted: c.isCompleted,
|
||||
completionLastChangedBy,
|
||||
reactionCounts,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user