feat(chat): 增加会话管理与可审计恢复

- 迁移019:增加置顶和分支来源字段及列表索引
- 会话接口:支持内容搜索、命名置顶、分支来源及请求回执
- 恢复服务:校验30秒撤销时间窗和账号归属,后台恢复写入审计
- 测试与文档:覆盖恢复边界和真实路由调用
This commit is contained in:
湛兮
2026-10-28 19:19:38 +08:00
parent c46ca1f5f8
commit b34c996684
7 changed files with 307 additions and 14 deletions
@@ -0,0 +1,33 @@
/** 会话恢复的账号隔离与撤销时间窗;管理员恢复沿用现有审计事务。 */
import { rows, write, transaction, audit } from "./digital.repository";
import { conflict, notFound } from "../../shared/http-error";
/** 用户只能撤销最近 30 秒内的删除;后台可恢复历史删除但不能跨用户操作。 */
export async function restoreConversation(
id: string,
account: string,
actor?: string,
) {
return transaction(async (c) => {
const [found] = await rows<{
id: string;
deleted_at: Date | null;
canUndo: number;
}>(
"SELECT id,deleted_at,(deleted_at>=DATE_SUB(NOW(3),INTERVAL 30 SECOND)) AS canUndo FROM dh_conversations WHERE id=? AND account=? FOR UPDATE",
[id, account],
c,
);
if (!found) throw notFound("会话不存在");
if (!found.deleted_at) return { restored: true };
if (!actor && !found.canUndo)
throw conflict("撤销时间已过,可联系管理员恢复这段对话");
await write(
"UPDATE dh_conversations SET deleted_at=NULL WHERE id=? AND account=?",
[id, account],
c,
);
if (actor) await audit(actor, "conversation.restore", id, { account }, c);
return { restored: true };
});
}