feat(notification): improve notification system with team settings and project-level toggles

This commit is contained in:
yiling
2026-05-29 20:40:13 +08:00
parent 748363d8f8
commit ff66d4af14
12 changed files with 627 additions and 9 deletions
+55
View File
@@ -1,5 +1,6 @@
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
import { Hono, type Context, type Next } from 'hono'
import { prisma } from '@/db'
import fileRoute from './file'
import { assetService } from '@/services/asset/asset'
import { metadataService } from '@/services/metadata/metadata'
@@ -202,6 +203,60 @@ describe('file api', () => {
})
})
it('POST /files/:fileId/comments - reply_created targets parent comment creator', async () => {
vi.mocked(assetService.createComment).mockResolvedValue({
id: 'comment-id',
assetId: 'file-id',
message: 'hello reply',
annotations: null,
second: null,
creator: { id: 'user-id', name: 'Test User' },
replies: [],
attachments: [],
mentions: [],
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
sessionId: null,
})
// Mock assetService.getAsset to return teamId for notification
vi.mocked(assetService.getAsset).mockResolvedValue({
id: 'test-id',
project: { teamId: 'test-team' },
} as any) // eslint-disable-line @typescript-eslint/no-explicit-any
const mockFindUnique = vi.spyOn(prisma.assetComment, 'findUnique').mockResolvedValue({
id: 'parent-comment-id',
creatorId: 'parent-comment-creator-id',
} as any) // eslint-disable-line @typescript-eslint/no-explicit-any
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/test-id/comments', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
message: 'hello reply',
replyToId: 'parent-comment-id',
attachmentIds: [],
}),
})
expect(res.status).toBe(201)
const json = await res.json()
expect(json.message).toBe('hello reply')
expect(notificationService.create).toHaveBeenCalledWith({
type: 'reply_created',
teamId: 'test-team',
creatorId: 'user1',
assetId: 'test-id',
userId: 'parent-comment-creator-id',
commentMessage: 'hello reply',
})
mockFindUnique.mockRestore()
})
it('GET /files/:fileId/comments', async () => {
vi.mocked(assetService.listComments).mockResolvedValue({
data: [
+12
View File
@@ -1,5 +1,6 @@
import { Hono } from 'hono'
import { zValidator } from '@hono/zod-validator'
import { prisma } from '@/db'
import { authzService, Permission, ResourceType } from '@/services/authz/authz'
import { assetService } from '@/services/asset/asset'
import { metadataService } from '@/services/metadata/metadata'
@@ -156,11 +157,22 @@ const route = new Hono<{ Variables: { user: User } }>()
const context = await assetService.getAssetContext(fileId)
if (context?.teamId) {
let targetUserId: string | undefined
if (req.replyToId) {
const parentComment = await prisma.assetComment.findUnique({
where: { id: req.replyToId },
})
if (parentComment?.creatorId) {
targetUserId = parentComment.creatorId
}
}
notificationService.create({
type: notifType,
teamId: context.teamId,
creatorId: user.id,
assetId: fileId,
userId: targetUserId,
commentMessage: req.message,
})
}
+76
View File
@@ -3,6 +3,7 @@ import { Hono, type Context, type Next } from 'hono'
import notificationRoute from './notification'
import { notificationService } from '@/services/notification/notification'
import { authzService, Permission, ResourceType } from '@/services/authz/authz'
import { userMetadataService } from '@/services/user-metadata/user-metadata'
vi.mock('@/api/middleware/auth', () => ({
authMiddleware: async (
@@ -16,6 +17,7 @@ vi.mock('@/api/middleware/auth', () => ({
vi.mock('@/services/authz/authz')
vi.mock('@/services/notification/notification')
vi.mock('@/services/user-metadata/user-metadata')
describe('notification api', () => {
const app = new Hono<{ Variables: { user: { id: string; name: string } } }>()
@@ -77,4 +79,78 @@ describe('notification api', () => {
id: 't1',
})
})
it('GET /teams/:teamId/notifications/settings - returns default settings', async () => {
vi.mocked(userMetadataService.getMetadata).mockResolvedValue(null)
const res = await app.request('/teams/t1/notifications/settings')
expect(res.status).toBe(200)
const json = await res.json()
expect(json.comments).toBe(true)
expect(json.yourUploads).toBe(false)
expect(userMetadataService.getMetadata).toHaveBeenCalledWith(
'user1',
't1',
'notification_settings',
)
})
it('GET /teams/:teamId/notifications/settings - returns saved settings', async () => {
vi.mocked(userMetadataService.getMetadata).mockResolvedValue({
key: 'notification_settings',
value: {
comments: false,
replies: false,
mentions: true,
yourUploads: true,
otherUploads: false,
statusUpdates: true,
},
})
const res = await app.request('/teams/t1/notifications/settings')
expect(res.status).toBe(200)
const json = await res.json()
expect(json.comments).toBe(false)
expect(json.yourUploads).toBe(true)
})
it('POST /teams/:teamId/notifications/settings - saves settings', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(userMetadataService.upsertMetadata).mockResolvedValue(undefined as any)
const res = await app.request('/teams/t1/notifications/settings', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
comments: false,
replies: false,
mentions: true,
yourUploads: true,
otherUploads: false,
statusUpdates: true,
}),
})
expect(res.status).toBe(200)
const json = await res.json()
expect(json.success).toBe(true)
expect(userMetadataService.upsertMetadata).toHaveBeenCalledWith(
'user1',
't1',
'notification_settings',
{
comments: false,
replies: false,
mentions: true,
yourUploads: true,
otherUploads: false,
statusUpdates: true,
},
)
})
})
+47
View File
@@ -5,7 +5,9 @@ import { authzService, Permission, ResourceType } from '@/services/authz/authz'
import {
listNotificationsRequestSchema,
markNotificationReadRequestSchema,
notificationSettingsSchema,
} from '@/dtos/notification'
import { userMetadataService } from '@/services/user-metadata/user-metadata'
import type { Prisma } from '@/generated/prisma/client'
type User = Prisma.UserGetPayload<Record<string, never>>
@@ -62,5 +64,50 @@ const route = new Hono<{ Variables: { user: User } }>()
return new Response(null, { status: 204 })
},
)
.get('/teams/:teamId/notifications/settings', async (c) => {
const teamId = c.req.param('teamId')
const user = c.get('user')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Team,
id: teamId,
})
const metadata = await userMetadataService.getMetadata(user.id, teamId, 'notification_settings')
const settings = metadata
? metadata.value
: {
comments: true,
replies: true,
mentions: true,
yourUploads: false,
otherUploads: true,
statusUpdates: true,
}
return c.json(settings)
})
.post(
'/teams/:teamId/notifications/settings',
zValidator('json', notificationSettingsSchema),
async (c) => {
const teamId = c.req.param('teamId')
const user = c.get('user')
const req = c.req.valid('json')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Team,
id: teamId,
})
await userMetadataService.upsertMetadata(user.id, teamId, 'notification_settings', req)
return c.json({ success: true })
},
)
export default route