mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 11:24:43 +00:00
feat(notification): improve notification system with team settings and project-level toggles
This commit is contained in:
@@ -3,6 +3,7 @@ import { prisma } from '@/db'
|
||||
import { setupTestDbHooks } from '@/db-test-hooks'
|
||||
import { NotificationType } from '@/generated/prisma/client'
|
||||
import { notificationService } from './notification'
|
||||
import { userMetadataService } from '@/services/user-metadata/user-metadata'
|
||||
|
||||
describe('NotificationService', () => {
|
||||
setupTestDbHooks()
|
||||
@@ -247,4 +248,66 @@ describe('NotificationService', () => {
|
||||
const p2Notification = res.data.find((n) => n.id === '002')
|
||||
expect(p2Notification).toBeUndefined()
|
||||
})
|
||||
|
||||
it('Respects user metadata notification settings', async () => {
|
||||
const tm = await prisma.team.create({ data: { name: 'Team 1' } })
|
||||
const u1 = await prisma.user.create({
|
||||
data: { name: 'u1', email: `u1-${Date.now()}@example.com`, password: 'p' },
|
||||
})
|
||||
const u2 = await prisma.user.create({
|
||||
data: { name: 'u2', email: `u2-${Date.now()}@example.com`, password: 'p' },
|
||||
})
|
||||
await prisma.teamMember.create({
|
||||
data: { teamId: tm.id, userId: u1.id, role: 'owner' },
|
||||
})
|
||||
|
||||
// Create comments, replies, mentions, uploads, etc.
|
||||
const commentNotif = await prisma.notification.create({
|
||||
data: { id: '001', teamId: tm.id, type: NotificationType.comment_created },
|
||||
})
|
||||
const replyNotif = await prisma.notification.create({
|
||||
data: { id: '002', teamId: tm.id, type: NotificationType.reply_created, userId: u1.id },
|
||||
})
|
||||
const uploadNotifOther = await prisma.notification.create({
|
||||
data: {
|
||||
id: '003',
|
||||
teamId: tm.id,
|
||||
type: NotificationType.successful_file_uploaded,
|
||||
creatorId: u2.id,
|
||||
},
|
||||
})
|
||||
const uploadNotifSelf = await prisma.notification.create({
|
||||
data: {
|
||||
id: '004',
|
||||
teamId: tm.id,
|
||||
type: NotificationType.successful_file_uploaded,
|
||||
creatorId: u1.id,
|
||||
},
|
||||
})
|
||||
|
||||
const svc = notificationService
|
||||
|
||||
// Default settings: comments=true, replies=true, mentions=true, yourUploads=false, otherUploads=true
|
||||
const resDefault = await svc.list(tm.id, u1.id, {})
|
||||
expect(resDefault.data.some((n) => n.id === commentNotif.id)).toBe(true)
|
||||
expect(resDefault.data.some((n) => n.id === replyNotif.id)).toBe(true)
|
||||
expect(resDefault.data.some((n) => n.id === uploadNotifOther.id)).toBe(true)
|
||||
expect(resDefault.data.some((n) => n.id === uploadNotifSelf.id)).toBe(false) // yourUploads is false by default
|
||||
|
||||
// Disable comments and replies, but enable yourUploads
|
||||
await userMetadataService.upsertMetadata(u1.id, tm.id, 'notification_settings', {
|
||||
comments: false,
|
||||
replies: false,
|
||||
mentions: true,
|
||||
yourUploads: true,
|
||||
otherUploads: false,
|
||||
statusUpdates: true,
|
||||
})
|
||||
|
||||
const resCustom = await svc.list(tm.id, u1.id, {})
|
||||
expect(resCustom.data.some((n) => n.id === commentNotif.id)).toBe(false)
|
||||
expect(resCustom.data.some((n) => n.id === replyNotif.id)).toBe(false)
|
||||
expect(resCustom.data.some((n) => n.id === uploadNotifOther.id)).toBe(false) // otherUploads is now false
|
||||
expect(resCustom.data.some((n) => n.id === uploadNotifSelf.id)).toBe(true) // yourUploads is now true
|
||||
})
|
||||
})
|
||||
|
||||
@@ -5,10 +5,12 @@ import {
|
||||
NotificationInfo,
|
||||
CreateNotificationRequest,
|
||||
ListNotificationParams,
|
||||
NotificationSettings,
|
||||
} from '@/dtos/notification'
|
||||
import { paginateQuery, PaginatedData } from '@/services/pagination'
|
||||
import '@/prisma-json-types'
|
||||
import { s3Service } from '@/services/s3/s3'
|
||||
import { userMetadataService } from '@/services/user-metadata/user-metadata'
|
||||
|
||||
const mentionRegex = /<@([^>]+)>/g
|
||||
|
||||
@@ -127,13 +129,76 @@ export class NotificationService {
|
||||
projectFilter = pms.filter((pm) => pm.project).map((pm) => pm.projectId)
|
||||
}
|
||||
|
||||
// Load notification settings
|
||||
const settingsMeta = await userMetadataService.getMetadata(
|
||||
userId,
|
||||
teamId,
|
||||
'notification_settings',
|
||||
)
|
||||
const settings: NotificationSettings = settingsMeta
|
||||
? (settingsMeta.value as NotificationSettings)
|
||||
: {
|
||||
comments: true,
|
||||
replies: true,
|
||||
mentions: true,
|
||||
yourUploads: false,
|
||||
otherUploads: true,
|
||||
statusUpdates: true,
|
||||
}
|
||||
|
||||
const typeConditions: Prisma.NotificationWhereInput[] = []
|
||||
|
||||
// 1. Comments
|
||||
if (settings.comments) {
|
||||
typeConditions.push({ type: NotificationType.comment_created, userId: null })
|
||||
}
|
||||
|
||||
// 2. Replies
|
||||
if (settings.replies) {
|
||||
typeConditions.push({ type: NotificationType.reply_created, userId: userId })
|
||||
typeConditions.push({ type: NotificationType.reply_created, userId: null })
|
||||
}
|
||||
|
||||
// 3. Mentions
|
||||
if (settings.mentions) {
|
||||
typeConditions.push({ type: NotificationType.mention, userId: userId })
|
||||
}
|
||||
|
||||
// 4. Uploads
|
||||
if (settings.yourUploads && settings.otherUploads) {
|
||||
typeConditions.push({ type: NotificationType.successful_file_uploaded })
|
||||
} else if (settings.yourUploads) {
|
||||
typeConditions.push({ type: NotificationType.successful_file_uploaded, creatorId: userId })
|
||||
} else if (settings.otherUploads) {
|
||||
typeConditions.push({
|
||||
type: NotificationType.successful_file_uploaded,
|
||||
creatorId: { not: userId },
|
||||
})
|
||||
}
|
||||
|
||||
// 5. Status Updates
|
||||
if (settings.statusUpdates) {
|
||||
typeConditions.push({ type: NotificationType.metadata_field_updated_status })
|
||||
}
|
||||
|
||||
// 6. Always include team/project join notifications
|
||||
typeConditions.push({ type: NotificationType.new_user_join_team })
|
||||
typeConditions.push({ type: NotificationType.new_user_join_project })
|
||||
|
||||
const where: Prisma.NotificationWhereInput = {
|
||||
teamId,
|
||||
OR: [
|
||||
{ type: { not: NotificationType.mention } },
|
||||
{ type: NotificationType.mention, userId: userId },
|
||||
OR: typeConditions,
|
||||
AND: [
|
||||
{
|
||||
OR: [
|
||||
...(settings.yourUploads
|
||||
? [{ type: NotificationType.successful_file_uploaded, creatorId: userId }]
|
||||
: []),
|
||||
{ creatorId: null },
|
||||
{ creatorId: { not: userId } },
|
||||
],
|
||||
},
|
||||
],
|
||||
AND: [{ OR: [{ creatorId: null }, { creatorId: { not: userId } }] }],
|
||||
}
|
||||
|
||||
if (teamMember.scope === 'project') {
|
||||
|
||||
@@ -32,7 +32,7 @@ export class ProjectService {
|
||||
data: {
|
||||
name: req.name,
|
||||
teamId: req.teamId,
|
||||
enableNotification: req.enableNotification || false,
|
||||
enableNotification: req.enableNotification ?? true,
|
||||
coverImageKey: req.coverImageKey,
|
||||
},
|
||||
})
|
||||
@@ -304,7 +304,7 @@ export class ProjectService {
|
||||
const pi: ProjectInfo = {
|
||||
id: p.id,
|
||||
name: p.name,
|
||||
enableNotification: p.enableNotification || false,
|
||||
enableNotification: p.enableNotification ?? true,
|
||||
coverImageKey: p.coverImageKey || undefined,
|
||||
updatedAt: p.updatedAt,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user