feat: 智能助手支持软删会话并级联隐藏消息

This commit is contained in:
2026-09-26 16:47:01 +08:00
parent 13a2bd9274
commit 85bac4f710
9 changed files with 203 additions and 13 deletions
@@ -222,7 +222,17 @@ class MongoMessageRepository(MessageRepository):
) -> list[ChatMessage]:
cursor = (
self._col.find(
{"company_id": company_id, "user_id": user_id, "thread_id": thread_id}
{
"company_id": company_id,
"user_id": user_id,
"thread_id": thread_id,
# 兼容旧文档无 status;软删后 status=deleted
"$or": [
{"status": {"$exists": False}},
{"status": None},
{"status": {"$ne": "deleted"}},
],
}
)
.sort("seq", -1)
.limit(limit)
@@ -231,6 +241,24 @@ class MongoMessageRepository(MessageRepository):
rows.reverse()
return rows
async def soft_delete_by_thread(
self, company_id: int, user_id: str, thread_id: str
) -> int:
result = await self._col.update_many(
{
"company_id": company_id,
"user_id": user_id,
"thread_id": thread_id,
"$or": [
{"status": {"$exists": False}},
{"status": None},
{"status": {"$ne": "deleted"}},
],
},
{"$set": {"status": "deleted", "deleted_at": datetime.utcnow()}},
)
return int(result.modified_count or 0)
@staticmethod
def _to_msg(doc: dict[str, Any]) -> ChatMessage:
return ChatMessage(