/** 会话恢复的账号隔离与撤销时间窗;管理员恢复沿用现有审计事务。 */ 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 }; }); }