43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { desc, eq } from 'drizzle-orm'
|
|
import { db } from '~/drizzle/db'
|
|
import { songComments, systemSettings, users } from '~/drizzle/schema'
|
|
import { maskStudentName } from '~~/server/utils/siteUtils'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const user = event.context.user
|
|
if (!user) {
|
|
throw createError({ statusCode: 401, message: '未授权访问' })
|
|
}
|
|
|
|
const songId = Number(getRouterParam(event, 'id'))
|
|
if (!Number.isFinite(songId) || songId <= 0) {
|
|
throw createError({ statusCode: 400, message: '无效的 songId' })
|
|
}
|
|
|
|
const settingsResult = await db.select().from(systemSettings).limit(1)
|
|
const settings = settingsResult[0] || null
|
|
const shouldMask = !!settings?.hideStudentInfo && !['ADMIN', 'SUPER_ADMIN', 'SONG_ADMIN'].includes(user.role)
|
|
|
|
const rows = await db
|
|
.select({
|
|
id: songComments.id,
|
|
content: songComments.content,
|
|
createdAt: songComments.createdAt,
|
|
userId: songComments.userId,
|
|
userName: users.name,
|
|
username: users.username
|
|
})
|
|
.from(songComments)
|
|
.leftJoin(users, eq(songComments.userId, users.id))
|
|
.where(eq(songComments.songId, songId))
|
|
.orderBy(desc(songComments.createdAt))
|
|
|
|
return rows.map((r) => ({
|
|
id: r.id,
|
|
content: r.content,
|
|
createdAt: r.createdAt,
|
|
userId: r.userId,
|
|
authorName: shouldMask ? maskStudentName(String(r.userName || r.username || '')) : (r.userName || r.username || '')
|
|
}))
|
|
})
|