feat(users): 拆分 C 端身份并迁移历史聊天
- digital.users: 独立开户登录与凭据版本校验,后台查看用户及原始会话 - 017迁移: 保留聊天消息、实验分组和人工指定期限,迁移历史账号归属 - tests: 覆盖身份隔离、凭据失效、会话归属和长账号登录,同步 README
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
/** 后台 C 端用户与会话查看;聊天内容直接来自持久化消息表,不复制提示词或凭据。 */
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { rows, audit } from "./digital.repository";
|
||||
import {
|
||||
digitalUsers,
|
||||
createDigitalUser,
|
||||
updateDigitalUser,
|
||||
resetDigitalPassword,
|
||||
digitalUserInput,
|
||||
digitalUserUpdate,
|
||||
} from "./digital.users";
|
||||
import { authGuard } from "../auth/auth.guard";
|
||||
import { authService } from "../auth/auth.service";
|
||||
import { forbidden, notFound } from "../../shared/http-error";
|
||||
import { ok } from "../../shared/response";
|
||||
const userId = z.object({ id: z.coerce.number().int().positive() });
|
||||
const paging = z.object({
|
||||
page: z.coerce.number().int().min(1).default(1),
|
||||
size: z.coerce.number().int().min(1).max(100).default(30),
|
||||
});
|
||||
/** 新增用户管理页面与只读会话记录,管理接口始终要求后台管理员身份。 */
|
||||
export async function digitalUserAdminRoutes(app: FastifyInstance) {
|
||||
app.addHook("preHandler", async (r) => {
|
||||
await authGuard(r);
|
||||
const u = await authService.getCurrentUser(r.user);
|
||||
if (u.accountType !== "SUPER_ADMIN")
|
||||
throw forbidden("仅后台管理员可以查看 C 端用户与会话");
|
||||
});
|
||||
app.get("/users", async (r) =>
|
||||
ok(
|
||||
await digitalUsers(
|
||||
z
|
||||
.object({
|
||||
q: z.string().max(120).default(""),
|
||||
status: z.enum(["ACTIVE", "INACTIVE"]).optional(),
|
||||
})
|
||||
.merge(paging)
|
||||
.parse(r.query),
|
||||
),
|
||||
),
|
||||
);
|
||||
app.post("/users", async (r) =>
|
||||
ok(await createDigitalUser(r.user.sub, digitalUserInput.parse(r.body))),
|
||||
);
|
||||
app.patch("/users/:id", async (r) => {
|
||||
await updateDigitalUser(
|
||||
r.user.sub,
|
||||
userId.parse(r.params).id,
|
||||
digitalUserUpdate.parse(r.body),
|
||||
);
|
||||
return ok({ saved: true });
|
||||
});
|
||||
app.post("/users/:id/password", async (r) => {
|
||||
const body = z
|
||||
.object({ password: z.string().min(8).max(128) })
|
||||
.parse(r.body);
|
||||
await resetDigitalPassword(
|
||||
r.user.sub,
|
||||
userId.parse(r.params).id,
|
||||
body.password,
|
||||
);
|
||||
return ok({ saved: true });
|
||||
});
|
||||
app.get("/users/:id/conversations", async (r) => {
|
||||
const { id } = userId.parse(r.params),
|
||||
{ page, size } = paging.parse(r.query);
|
||||
const [user] = await rows(
|
||||
"SELECT id,username,display_name AS displayName,status,created_at AS createdAt FROM dh_users WHERE id=?",
|
||||
[id],
|
||||
);
|
||||
if (!user) throw notFound("C 端用户不存在");
|
||||
const account = `user:${id}`;
|
||||
const [count] = await rows<{ total: number }>(
|
||||
"SELECT COUNT(*) total FROM dh_conversations WHERE account=?",
|
||||
[account],
|
||||
);
|
||||
const items = await rows(
|
||||
"SELECT c.id,c.title,c.character_id AS characterId,COALESCE(ch.name,'已下架数字人') characterName,c.created_at AS createdAt,c.updated_at AS updatedAt,(SELECT COUNT(*) FROM dh_messages m WHERE m.conversation_id=c.id) messages FROM dh_conversations c LEFT JOIN dh_content ch ON ch.id=c.character_id WHERE c.account=? ORDER BY c.updated_at DESC,c.id DESC LIMIT ? OFFSET ?",
|
||||
[account, size, (page - 1) * size],
|
||||
);
|
||||
return ok({ user, items, total: Number(count.total), page, size });
|
||||
});
|
||||
app.get("/users/:id/conversations/:conversation/messages", async (r) => {
|
||||
const { id, conversation } = userId
|
||||
.extend({ conversation: z.string().regex(/^[a-zA-Z0-9_-]{1,64}$/) })
|
||||
.parse(r.params),
|
||||
{ page, size } = paging.parse(r.query);
|
||||
const [owned] = await rows(
|
||||
"SELECT c.id,c.title,COALESCE(ch.name,'已下架数字人') characterName FROM dh_conversations c LEFT JOIN dh_content ch ON ch.id=c.character_id WHERE c.id=? AND c.account=?",
|
||||
[conversation, `user:${id}`],
|
||||
);
|
||||
if (!owned) throw notFound("该用户没有此会话");
|
||||
const [count] = await rows<{ total: number }>(
|
||||
"SELECT COUNT(*) total FROM dh_messages WHERE conversation_id=?",
|
||||
[conversation],
|
||||
);
|
||||
const items = await rows(
|
||||
"SELECT id,role,content,status,pinned,sequence_id AS sequenceId,created_at AS createdAt FROM dh_messages WHERE conversation_id=? ORDER BY sequence_id DESC LIMIT ? OFFSET ?",
|
||||
[conversation, size, (page - 1) * size],
|
||||
);
|
||||
await audit(r.user.sub, "conversation.read", conversation, {
|
||||
userId: id,
|
||||
page,
|
||||
});
|
||||
return ok({
|
||||
conversation: owned,
|
||||
items: items.reverse(),
|
||||
total: Number(count.total),
|
||||
page,
|
||||
size,
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user