feat(nsfw): 持久化账号偏好并接入生成控制
- digital.profile.ts: 未保存偏好时默认开启,原子合并 JSON 保留其他资料 - digital.controller.ts、digital.users.ts: 接入本人和管理员偏好读写、事务及审计 - digital.chat.ts、digital.prompt.ts、digital.suggestions.ts: 各生成入口读取服务端账号偏好,保留游客鉴权 - tests/digital-*.test.ts: 覆盖默认值、关闭保留、旧客户端兼容与越权拦截 - README.md: 同步模块职责和发布要求
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/** 独立 C 端账号、凭据和后台用户查询;不读写管理员或员工密码。 */
|
||||
import { z } from "zod";
|
||||
import { rows, write, transaction, audit } from "./digital.repository";
|
||||
import { mergeDigitalProfile, parseDigitalProfile } from "./digital.profile";
|
||||
import { hashPassword, verifyPassword } from "../auth/password";
|
||||
import type { AuthJwtPayload, AuthUser } from "../auth/auth.types";
|
||||
import {
|
||||
@@ -19,11 +20,13 @@ export const digitalUserInput = z.object({
|
||||
.regex(/^[a-zA-Z0-9_@.+-]+$/),
|
||||
displayName: z.string().trim().min(1).max(120),
|
||||
password: z.string().min(8).max(128),
|
||||
nsfwEnabled: z.boolean().default(true),
|
||||
});
|
||||
/** 修改账号基本资料,用户名作为登录标识保持稳定。 */
|
||||
export const digitalUserUpdate = z.object({
|
||||
displayName: z.string().trim().min(1).max(120),
|
||||
status: z.enum(["ACTIVE", "INACTIVE"]),
|
||||
nsfwEnabled: z.boolean().optional(),
|
||||
});
|
||||
type UserRow = {
|
||||
id: number;
|
||||
@@ -146,12 +149,17 @@ export async function digitalUsers(input: {
|
||||
`SELECT COUNT(*) total FROM dh_users u ${where}`,
|
||||
args,
|
||||
);
|
||||
const items = await rows(
|
||||
`SELECT ${publicColumns},(SELECT COUNT(*) FROM dh_conversations c WHERE c.account=CONCAT('user:',u.id)) conversations,(SELECT COUNT(*) FROM dh_messages m JOIN dh_conversations c ON c.id=m.conversation_id WHERE c.account=CONCAT('user:',u.id)) messages FROM dh_users u ${where} ORDER BY u.id DESC LIMIT ? OFFSET ?`,
|
||||
const records = await rows<
|
||||
Record<string, unknown> & { profileBody: unknown }
|
||||
>(
|
||||
`SELECT ${publicColumns},p.body AS profileBody,(SELECT COUNT(*) FROM dh_conversations c WHERE c.account=CONCAT('user:',u.id)) conversations,(SELECT COUNT(*) FROM dh_messages m JOIN dh_conversations c ON c.id=m.conversation_id WHERE c.account=CONCAT('user:',u.id)) messages FROM dh_users u LEFT JOIN dh_profiles p ON p.account=CONCAT('user:',u.id) ${where} ORDER BY u.id DESC LIMIT ? OFFSET ?`,
|
||||
[...args, input.size, (input.page - 1) * input.size],
|
||||
);
|
||||
return {
|
||||
items,
|
||||
items: records.map(({ profileBody, ...user }) => ({
|
||||
...user,
|
||||
nsfwEnabled: parseDigitalProfile(profileBody).nsfwEnabled,
|
||||
})),
|
||||
total: Number(count.total),
|
||||
page: input.page,
|
||||
size: input.size,
|
||||
@@ -160,7 +168,7 @@ export async function digitalUsers(input: {
|
||||
/** 仅后台创建 C 端用户,唯一键冲突转换为清晰业务错误。 */
|
||||
export async function createDigitalUser(
|
||||
actor: string,
|
||||
input: z.infer<typeof digitalUserInput>,
|
||||
input: z.input<typeof digitalUserInput>,
|
||||
) {
|
||||
const hash = await hashPassword(input.password);
|
||||
try {
|
||||
@@ -170,11 +178,20 @@ export async function createDigitalUser(
|
||||
[input.username, input.displayName, hash],
|
||||
c,
|
||||
);
|
||||
await mergeDigitalProfile(
|
||||
`user:${result.insertId}`,
|
||||
{ nsfwEnabled: input.nsfwEnabled ?? true },
|
||||
c,
|
||||
);
|
||||
await audit(
|
||||
actor,
|
||||
"user.create",
|
||||
String(result.insertId),
|
||||
{ username: input.username, displayName: input.displayName },
|
||||
{
|
||||
username: input.username,
|
||||
displayName: input.displayName,
|
||||
nsfwEnabled: input.nsfwEnabled ?? true,
|
||||
},
|
||||
c,
|
||||
);
|
||||
return { id: result.insertId };
|
||||
@@ -203,6 +220,12 @@ export async function updateDigitalUser(
|
||||
[input.displayName, input.status, id],
|
||||
c,
|
||||
);
|
||||
if (input.nsfwEnabled !== undefined)
|
||||
await mergeDigitalProfile(
|
||||
`user:${id}`,
|
||||
{ nsfwEnabled: input.nsfwEnabled },
|
||||
c,
|
||||
);
|
||||
await audit(actor, "user.update", String(id), input, c);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user