Files
shumai/packages/core/src/notification/notification.ts
T
Yiling-J 1f2bdc8c9e feat(agentic kanban): phase 1 not-agentic kanban board (#330)
* feat(kanban): implement AI-native kanban database models, services, and REST APIs

* feat(webui): implement team kanban board and task management

* style(webui): increase kanban dialog widths and form container

* fix(webui): ensure kanban dialogs override sm:max-w-lg base class

* style(webui): set edit task dialog max width to 5xl

* chore: untrack kanban plan markdown files

* feat(webui): update kanban page layout to 2 columns and refine task activity UI

* fix(webui): constrain kanban column and board heights for vertical scrolling

* feat(kanban): unify task status transition into updateTask API and allow unconstrained human task transitions

* fix(webui): add optimistic updates to kanban drag-and-drop to eliminate flash

* refactor(webui): remove client-side search box from kanban header

* feat(kanban): redesign task dialog comments area and add attachment support

* refactor(webui): remove top status action bar from task info dialog

* fix(webui): narrow query cache predicate and add defensive guards for kanban drag optimistic update

* feat(kanban): add drag-and-drop task reordering with fractional indexing (#331)

* feat(kanban): add drag-and-drop task reordering with fractional indexing

* fix(kanban): improve card drop zones, snap indicators, and column highlight

* fix(kanban): enable card droppables only during active drag

* fix(kanban): refine hover styles and drop indicators for column boundaries

* fix(kanban): unify bottom drop indicator and fix top indicator clipping

* fix(kanban): ensure full column highlight and full coverage drop target

* minor change

* feat(kanban): allow agent tasks free drag-and-drop and enforce status change permissions (#332)

* refactor(kanban): rename task_type_agentic translation keys to task_type_agent

* feat(kanban): refactor task type enum to isAgentTask boolean (#333)

* refactor(kanban): remove phase 2 agent execution logic and streamline phase 1 kanban

* feat(kanban): add notifications and audit logging for kanban (#334)

* feat(kanban): redesign config area layout and add parent tasks multi-selector (#335)

* feat(webui): redesign kanban task dialog config area

* feat(kanban): simplify task dependencies with parent tasks multi-selector

* refactor(kanban): remove unused links routes and legacy dependency methods

* feat(kanban): connect assets and kanban tasks with many-to-many relationship (#336)

* feat(kanban): connect assets and kanban tasks with many-to-many relationship

* feat(kanban): improve asset picker layout, infinite scrolling, and breadcrumb icon styling

* fix(kanban): ensure asset picker scrollarea is scrollable with min-h-0 and shrink-0 flex styling

* fix(kanban): hide recently deleted in folder tree picker and adjust header padding

* fix(kanban): use lucide files icon instead of paperclip for linked assets

* feat(kanban): improve task deletion, goal sidebar interaction, and card styling (#337)

* feat(kanban): improve task deletion, goal sidebar interaction, and card styling

* feat(kanban): move agent task badge after priority and update to orange style

* feat(kanban): add status Compare-And-Swap (CAS) to prevent drag-and-drop race conditions (#338)

* feat(kanban): add status Compare-And-Swap (CAS) to prevent drag-and-drop race conditions

* style(kanban): rotate kanban icon 180 degrees in sidebar and top nav

* style(kanban): remove rotate-180 from SquareKanban icons

* feat(kanban): add unassigned goal record and fix all tasks count (#339)

* feat(kanban): add unassigned goal record and fix all tasks count

* feat(kanban): toggle task count with three-dot action button on goal card hover

* fix(ci): pin bun version to 1.3.14 across CI and Docker (#340)

* fix(webui): truncate goal name to max 2 lines in kanban sidebar (#341)
2026-08-23 10:28:56 +08:00

540 lines
15 KiB
TypeScript

import { prisma } from '@shumai/db'
import type { Prisma } from '@shumai/db'
import { NotificationType } from '@shumai/db'
import {
NotificationInfo,
CreateNotificationRequest,
ListNotificationParams,
NotificationSettings,
} from '@shumai/dtos'
import { paginateQuery, PaginatedData } from '@shumai/core/src/pagination'
import '@shumai/db/src/prisma-json-types'
import { s3Service } from '@shumai/core/src/s3/s3'
import { userMetadataService } from '@shumai/core/src/user-metadata/user-metadata'
const mentionRegex = /<@([^>]+)>/g
export class NotificationService {
async notifyKanbanTaskEvent(params: {
type: NotificationType
teamId: string
projectId?: string | null
creatorId: string
kanbanTaskId?: string
stakeholderIds?: (string | null | undefined)[]
targetUserId?: string
commentMessage?: string
}) {
const recipients = new Set<string>()
if (params.targetUserId) {
recipients.add(params.targetUserId)
}
if (params.stakeholderIds) {
for (const id of params.stakeholderIds) {
if (id) recipients.add(id)
}
}
recipients.delete(params.creatorId)
const promises: Promise<void>[] = []
for (const recipientId of recipients) {
promises.push(
this.createAsync({
type: params.type,
teamId: params.teamId,
projectId: params.projectId ?? undefined,
creatorId: params.creatorId,
kanbanTaskId: params.kanbanTaskId,
userId: recipientId,
}),
)
}
if (params.commentMessage) {
const matches = [...params.commentMessage.matchAll(mentionRegex)]
const mentionedIds = new Set<string>()
for (const match of matches) {
if (match.length > 1) {
const userId = match[1]
if (mentionedIds.has(userId)) {
continue
}
mentionedIds.add(userId)
if (params.creatorId === userId) {
continue
}
promises.push(
this.createAsync({
type: NotificationType.mention,
teamId: params.teamId,
projectId: params.projectId ?? undefined,
creatorId: params.creatorId,
kanbanTaskId: params.kanbanTaskId,
userId: userId,
}),
)
}
}
}
await Promise.all(promises)
}
async create(req: CreateNotificationRequest) {
// Note: Do not await this, we fire and forget similarly to Go
this.createAsync(req).catch((err) => {
console.error('Failed to create notification asynchronously:', err)
})
}
private async createAsync(req: CreateNotificationRequest) {
let projectId = req.projectId
// Resolve ProjectID from AssetID if missing
if (!projectId && req.assetId) {
const asset = await prisma.asset.findUnique({
where: { id: req.assetId },
include: { project: true },
})
if (asset?.projectId) {
projectId = asset.projectId
}
}
// Resolve ProjectID from KanbanTaskId if missing
if (!projectId && req.kanbanTaskId) {
const task = await prisma.kanbanTask.findUnique({
where: { id: req.kanbanTaskId },
})
if (task?.projectId) {
projectId = task.projectId
}
}
// Check project notification settings
if (projectId) {
const project = await prisma.project.findUnique({
where: { id: projectId },
})
if (project && !project.enableNotification) {
return
}
}
// Helper to create a notification
const create = async (t: NotificationType, targetUserId?: string) => {
try {
await prisma.notification.create({
data: {
type: t,
teamId: req.teamId,
projectId: projectId,
creatorId: req.creatorId,
assetId: req.assetId,
taskId: req.taskId,
kanbanTaskId: req.kanbanTaskId,
userId: targetUserId,
},
})
} catch (err) {
console.error('Failed to save notification', { type: t, err })
}
}
// 1. Create the primary notification
await create(req.type, req.userId)
// 2. Handle Mentions for Comment/Reply types
if (
(req.type === NotificationType.comment_created ||
req.type === NotificationType.reply_created ||
req.type === NotificationType.kanban_task_comment_created) &&
req.commentMessage
) {
const matches = [...req.commentMessage.matchAll(mentionRegex)]
const mentionedIds = new Set<string>()
for (const match of matches) {
if (match.length > 1) {
const userId = match[1]
if (mentionedIds.has(userId)) {
continue
}
mentionedIds.add(userId)
// Don't notify self
if (req.creatorId === userId) {
continue
}
// Create mention notification
await create(NotificationType.mention, userId)
}
}
}
}
private async getListWhere(
teamId: string,
userId: string,
teamMember: Prisma.TeamMemberGetPayload<{ include: { lastReadNotification: true } }>,
unreadOnly: boolean,
): Promise<Prisma.NotificationWhereInput> {
// Permission Filtering based on Scope
let projectFilter: string[] = []
if (teamMember.scope === 'project') {
const pms = await prisma.projectMember.findMany({
where: { teamMemberId: teamMember.id },
include: { project: true },
})
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,
kanbanTasks: true,
kanbanComments: true,
}
const typeConditions: Prisma.NotificationWhereInput[] = []
// 1. Comments
if (settings.comments ?? true) {
typeConditions.push({ type: NotificationType.comment_created, userId: null })
}
// 2. Replies
if (settings.replies ?? true) {
typeConditions.push({ type: NotificationType.reply_created, userId: userId })
typeConditions.push({ type: NotificationType.reply_created, userId: null })
}
// 3. Mentions
if (settings.mentions ?? true) {
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 ?? true) {
typeConditions.push({ type: NotificationType.metadata_field_updated_status })
}
// 6. Kanban Tasks
if (settings.kanbanTasks ?? true) {
typeConditions.push({
type: {
in: [
NotificationType.kanban_task_created,
NotificationType.kanban_task_assigned,
NotificationType.kanban_task_status_updated,
NotificationType.kanban_task_updated,
NotificationType.kanban_task_deleted,
],
},
userId: userId,
})
}
// 7. Kanban Comments
if (settings.kanbanComments ?? true) {
typeConditions.push({
type: NotificationType.kanban_task_comment_created,
userId: userId,
})
}
// 8. 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: typeConditions,
AND: [
{
OR: [
...(settings.yourUploads
? [{ type: NotificationType.successful_file_uploaded, creatorId: userId }]
: []),
{ creatorId: null },
{ creatorId: { not: userId } },
],
},
],
}
if (teamMember.scope === 'project') {
// Must be AND applied after the general rules
const currentAnd = Array.isArray(where.AND) ? where.AND : where.AND ? [where.AND] : []
where.AND = [
...currentAnd,
{
OR: [
{ type: NotificationType.mention }, // Always allow mentions (targeted)
{ userId: userId }, // Direct targeted notifications (e.g. assigned, stakeholder)
{ projectId: { in: projectFilter } }, // Only show notifications for projects user is member of
],
},
] as Prisma.NotificationWhereInput[]
}
if (unreadOnly && teamMember.lastReadNotification) {
// We want notifications NEWER than the last read one.
// String comparison works for ULID/KSUID.
const currentAnd = Array.isArray(where.AND) ? where.AND : where.AND ? [where.AND] : []
where.AND = [
...currentAnd,
{ id: { gt: teamMember.lastReadNotification.id } },
] as Prisma.NotificationWhereInput[]
}
return where
}
async getUnreadCount(teamId: string, userId: string): Promise<number> {
const teamMember = await prisma.teamMember.findUnique({
where: {
teamIdUserId: {
teamId,
userId,
},
},
include: {
lastReadNotification: true,
},
})
if (!teamMember) {
return 0
}
const where = await this.getListWhere(teamId, userId, teamMember, true)
return prisma.notification.count({ where })
}
async list(
teamId: string,
userId: string,
params: ListNotificationParams,
): Promise<PaginatedData<NotificationInfo[]>> {
// Get user's last read notification
const teamMember = await prisma.teamMember.findUnique({
where: {
teamIdUserId: {
teamId,
userId,
},
},
include: {
lastReadNotification: true,
},
})
if (!teamMember) {
throw new Error('failed to get team member')
}
const where = await this.getListWhere(teamId, userId, teamMember, !!params.unreadOnly)
return paginateQuery<NotificationInfo>(
async (skip, take) => {
const notifications = await prisma.notification.findMany({
where,
orderBy: { id: 'desc' },
skip,
take,
include: {
creator: true,
team: true,
project: true,
asset: true,
user: true,
kanbanTask: true,
},
})
return Promise.all(
notifications.map(async (n) => {
return this.toNotificationInfo(n, teamMember.lastReadNotification)
}),
)
},
null, // we don't need count
{
after: params.after,
first: params.pageSize,
},
)
}
async markRead(teamId: string, userId: string, notificationId: string) {
const teamMember = await prisma.teamMember.findUnique({
where: {
teamIdUserId: {
teamId,
userId,
},
},
include: {
lastReadNotification: true,
},
})
if (!teamMember) {
throw new Error('failed to get team member')
}
const notificationExists = await prisma.notification.findUnique({
where: { id: notificationId },
})
if (!notificationExists) {
throw new Error('notification not found')
}
let shouldUpdate = true
if (teamMember.lastReadNotification) {
if (notificationId <= teamMember.lastReadNotification.id) {
shouldUpdate = false
}
}
if (shouldUpdate) {
await prisma.teamMember.update({
where: { id: teamMember.id },
data: { lastReadNotificationId: notificationId },
})
}
}
private async toNotificationInfo(
n: Prisma.NotificationGetPayload<{
include: {
creator: true
team: true
project: true
asset: true
user: true
kanbanTask: true
}
}>,
lastRead: Prisma.NotificationGetPayload<Record<string, never>> | null,
): Promise<NotificationInfo> {
const info: NotificationInfo = {
id: n.id,
type: n.type || undefined,
createdAt: n.createdAt.toISOString(),
isRead: lastRead !== null && n.id <= lastRead.id,
}
if (n.creator) {
info.creator = {
id: n.creator.id,
name: n.creator.name,
}
}
if (n.team) {
info.team = {
id: n.team.id,
name: n.team.name,
}
}
if (n.project) {
info.project = {
id: n.project.id,
name: n.project.name,
}
}
if (n.user) {
info.user = {
id: n.user.id,
name: n.user.name,
}
}
if (n.kanbanTask) {
info.kanbanTask = {
id: n.kanbanTask.id,
title: n.kanbanTask.title,
status: n.kanbanTask.status,
}
}
if (n.asset) {
const a = n.asset
let previewUrl = ''
let thumbnailUrl = ''
let originalWidth = 0
let originalHeight = 0
const media = a.media as PrismaJson.MediaInfo | null
if (media) {
const bucket = process.env.S3_BUCKET || 'shumai'
try {
if (media.sprite) {
previewUrl = await s3Service.presign(bucket, media.sprite.key, 'GET')
if (media.poster) {
thumbnailUrl = await s3Service.presign(bucket, media.poster.key || '', 'GET')
}
if (media.metadata) {
originalWidth = media.metadata.originalWidth
originalHeight = media.metadata.originalHeight
}
} else if (media.thumbnail) {
previewUrl = await s3Service.presign(bucket, media.thumbnail.key || '', 'GET')
thumbnailUrl = previewUrl
}
} catch (e) {
console.error('Failed to generate presigned url for notification asset preview:', e)
}
}
info.asset = {
id: a.id,
name: a.name,
proxyType: (a.media as PrismaJson.MediaInfo | null)?.proxyType || undefined,
preview: previewUrl || undefined,
thumbnailUrl: thumbnailUrl || undefined,
originalWidth: originalWidth || undefined,
originalHeight: originalHeight || undefined,
}
}
return info
}
}
export const notificationService = new NotificationService()