mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 20:47:44 +00:00
* feat(notification): add email notification support and dedicated workflow * feat(webui): add team email notification settings and test email dialog * refactor(webui): remove redundant card header from email notification settings * fix(notification): prevent sending notification emails to AI agents * feat(notification): display project, avatar, upload time and resolve mention names in notification emails * refactor(notification): remove uploadDebounceSeconds and naive workflow sleep * refactor(notification): remove temporal notification workflow and keep email infrastructure * fix(team): prevent leaking email notification settings and credentials in team settings * feat(notification): add periodic background job for batched email notification delivery * fix(notification): embed avatars and thumbnails as inline cid attachments
168 lines
5.1 KiB
TypeScript
168 lines
5.1 KiB
TypeScript
import { Hono } from 'hono'
|
|
import { zValidator } from '@hono/zod-validator'
|
|
import { uploadService } from '@shumai/core/src/upload/upload'
|
|
import {
|
|
abortUploadRequestSchema,
|
|
confirmFileUploadRequestSchema,
|
|
createUploadTaskRequestSchema,
|
|
localUploadQuerySchema,
|
|
s3SignRequestSchema,
|
|
} from '@shumai/dtos'
|
|
import { paginationParamsSchema, AuditAction } from '@shumai/dtos'
|
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
|
import { notificationService } from '@shumai/core/src/notification/notification'
|
|
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
|
import { assetService } from '@shumai/core/src/asset/asset'
|
|
import { NotificationType } from '@shumai/db'
|
|
import { s3Service, verifyLocalUrlSignature } from '@shumai/core/src/s3/s3'
|
|
import type { Prisma } from '@shumai/db'
|
|
|
|
type User = Prisma.UserGetPayload<Record<string, never>>
|
|
|
|
export const localUploadRoute = new Hono().put(
|
|
'/upload/local',
|
|
zValidator('query', localUploadQuerySchema),
|
|
async (c) => {
|
|
const { bucket, key, Signature } = c.req.valid('query')
|
|
|
|
if (!verifyLocalUrlSignature(bucket, key, Signature)) {
|
|
return c.text('Invalid signature', 403)
|
|
}
|
|
|
|
const contentType = c.req.header('Content-Type')
|
|
let finalContentType = contentType
|
|
const contentLength = parseInt(c.req.header('Content-Length') || '0', 10)
|
|
|
|
if (contentType?.includes('multipart/form-data')) {
|
|
const body = await c.req.parseBody()
|
|
const file = body['file'] as File
|
|
if (!file) {
|
|
return c.text('No file uploaded', 400)
|
|
}
|
|
finalContentType = file.type || 'application/octet-stream'
|
|
const payload =
|
|
typeof file.stream === 'function' ? file.stream() : Buffer.from(await file.arrayBuffer())
|
|
await s3Service.putObject(bucket, key, payload, file.size, finalContentType)
|
|
} else {
|
|
const body = c.req.raw.body ?? (await c.req.arrayBuffer())
|
|
await s3Service.putObject(bucket, key, body, contentLength, finalContentType)
|
|
}
|
|
|
|
return c.json({ success: true })
|
|
},
|
|
)
|
|
|
|
const route = new Hono<{ Variables: { user: User } }>()
|
|
.get('/teams/:teamId/upload/tasks', zValidator('query', paginationParamsSchema), async (c) => {
|
|
const teamId = c.req.param('teamId')
|
|
const user = c.get('user')
|
|
const params = c.req.valid('query')
|
|
|
|
await authzService.hasPermission({
|
|
user,
|
|
permission: Permission.Read,
|
|
type: ResourceType.Team,
|
|
id: teamId,
|
|
})
|
|
|
|
const tasks = await uploadService.listUploadTasks(user.id, params)
|
|
return c.json(tasks)
|
|
})
|
|
.post(
|
|
'/teams/:teamId/upload/tasks',
|
|
zValidator('json', createUploadTaskRequestSchema),
|
|
async (c) => {
|
|
const user = c.get('user')
|
|
const req = c.req.valid('json')
|
|
|
|
await authzService.hasPermission({
|
|
user,
|
|
permission: Permission.Edit,
|
|
type: ResourceType.Asset,
|
|
id: req.parentId,
|
|
})
|
|
|
|
const resp = await uploadService.createUploadTask(user.id, req)
|
|
return c.json(resp)
|
|
},
|
|
)
|
|
.patch(
|
|
'/teams/:teamId/upload/tasks/:taskId',
|
|
zValidator('json', confirmFileUploadRequestSchema),
|
|
async (c) => {
|
|
const teamId = c.req.param('teamId')
|
|
const taskId = c.req.param('taskId')
|
|
const user = c.get('user')
|
|
const req = c.req.valid('json')
|
|
|
|
await authzService.hasPermission({
|
|
user,
|
|
permission: Permission.Edit,
|
|
type: ResourceType.Asset,
|
|
id: req.fileId,
|
|
})
|
|
|
|
await uploadService.confirmFileUpload(user.id, taskId, req)
|
|
|
|
if (!req.errorMessage) {
|
|
const context = await assetService.getAssetContext(req.fileId)
|
|
|
|
await notificationService.create({
|
|
type: NotificationType.successful_file_uploaded,
|
|
teamId: teamId,
|
|
projectId: context.projectId,
|
|
creatorId: user.id,
|
|
assetId: req.fileId,
|
|
taskId: taskId,
|
|
})
|
|
|
|
await auditLogService.logAction({
|
|
action: AuditAction.file_create,
|
|
teamId: teamId,
|
|
userId: user.id,
|
|
projectId: context.projectId,
|
|
itemId: req.fileId,
|
|
})
|
|
}
|
|
|
|
return c.json({ success: true })
|
|
},
|
|
)
|
|
.post('/teams/:teamId/upload/sign', zValidator('json', s3SignRequestSchema), 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.Edit,
|
|
type: ResourceType.Asset,
|
|
id: req.fileId,
|
|
})
|
|
|
|
const resp = await uploadService.signS3Upload(teamId, user.id, req)
|
|
return c.json(resp)
|
|
})
|
|
.post(
|
|
'/teams/:teamId/upload/tasks/:taskId/abort',
|
|
zValidator('json', abortUploadRequestSchema),
|
|
async (c) => {
|
|
const teamId = c.req.param('teamId')
|
|
const taskId = c.req.param('taskId')
|
|
const user = c.get('user')
|
|
const req = c.req.valid('json')
|
|
|
|
await authzService.hasPermission({
|
|
user,
|
|
permission: Permission.Edit,
|
|
type: ResourceType.Asset,
|
|
id: req.fileId,
|
|
})
|
|
|
|
const resp = await uploadService.abortUpload(teamId, user.id, taskId, req)
|
|
return c.json(resp)
|
|
},
|
|
)
|
|
|
|
export default route
|