mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-03 23:40:52 +08:00
* feat(sessions): add recycle bin for deleted chats
Implements soft-delete for chat sessions so users can restore
accidentally deleted conversations from a recycle bin.
Backend:
- Schema migration adds deleted_at REAL column to sessions
- delete_session becomes a soft-delete (sets deleted_at timestamp)
- New store methods: restore_session, purge_session, list_deleted_sessions
- Session list queries exclude soft-deleted sessions
- API endpoints: GET /sessions/recycle-bin, POST /{id}/restore,
DELETE /{id}/purge (purge triggers attachment/mastery cleanup)
Frontend:
- RecycleBinSection: collapsible sidebar section listing deleted
sessions with Restore and Delete permanently actions
- Wired into SidebarShell via recycleBinSlot prop
- API client functions for list, restore, and purge
- en/zh translations for all recycle-bin UI strings
Tests: 6 backend tests covering the soft-delete lifecycle
* style: apply ruff format to sqlite_store
* fix(sessions): preserve recycled chat artifacts
* feat(sessions): fold #1444's backend into this recycle bin
#1444 built the same feature from the other end. This PR had the SQLite
column, the routes and the sidebar UI; #1444 had the protocol declarations,
the PocketBase implementation and the CLI wording. Neither shipped alone —
this one left multi-user deployments answering AttributeError on
/recycle-bin, and #1444 built a bin nobody could see. Closes #1444; its
backend is here.
Kept from here: one `deleted_at` column. #1444 carried `is_deleted` beside it
in both backends, and `deleted_at IS NOT NULL` already says everything the
boolean did — PocketBase now derives the outward `is_deleted` field from the
timestamp instead of storing it twice.
Taken from #1444: the naming, which turns out to fix the residual both PRs
shipped with. `delete_session` keeps meaning what every existing caller
already meant by it — remove the row — so `reading.py`'s two internal
cleanups need no patch: a deleted reading workspace still takes its sessions
with it instead of depositing them in a learner's bin, where restoring one
would resurrect a session pointing at a workspace that no longer exists. The
chat surface's delete is a separate, explicitly named `soft_delete_session`,
and `hard_delete_session` empties the bin. Both backends agree, and the
protocol declares all five so a backend cannot silently lack one again.
`test_entries_cascade_on_session_purge` becomes two tests, because there are
now two deletions worth pinning: one the bin can undo, and one that skips it
and cascades immediately.
Co-Authored-By: sjagadee <srini.jagadeesh@gmail.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: XZH <cto@xzh.ai>
Co-authored-by: Bingxi Zhao (Frank) <13051345080@163.com>
Co-authored-by: sjagadee <srini.jagadeesh@gmail.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
67 lines
2.4 KiB
Python
67 lines
2.4 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
|
|
|
|
def test_delete_session_cleans_only_current_user_artifacts(as_user, monkeypatch) -> None:
|
|
from deeptutor.api.routers import sessions as sessions_router
|
|
from deeptutor.learning.models import LearningProgress
|
|
from deeptutor.learning.storage import LearningStore
|
|
from deeptutor.services.storage.attachment_store import (
|
|
get_attachment_store,
|
|
reset_attachment_store,
|
|
)
|
|
|
|
session_id = "shared-session-id"
|
|
attachment_id = "upload-1"
|
|
|
|
class _SessionStore:
|
|
async def soft_delete_session(self, candidate: str) -> bool:
|
|
return candidate == session_id
|
|
|
|
async def hard_delete_session(self, candidate: str) -> bool:
|
|
return candidate == session_id
|
|
|
|
monkeypatch.setattr(sessions_router, "get_session_store", lambda: _SessionStore())
|
|
reset_attachment_store()
|
|
|
|
async def _seed(uid: str) -> None:
|
|
with as_user(uid):
|
|
LearningStore().save(LearningProgress(book_id=session_id))
|
|
await get_attachment_store().put(
|
|
session_id=session_id,
|
|
attachment_id=attachment_id,
|
|
filename="notes.txt",
|
|
data=uid.encode(),
|
|
)
|
|
|
|
async def _artifacts_exist(uid: str) -> tuple[bool, bool]:
|
|
with as_user(uid):
|
|
learning_exists = LearningStore().exists(session_id)
|
|
attachment_exists = (
|
|
get_attachment_store().resolve_path(
|
|
session_id=session_id,
|
|
attachment_id=attachment_id,
|
|
filename="notes.txt",
|
|
)
|
|
is not None
|
|
)
|
|
return learning_exists, attachment_exists
|
|
|
|
async def _exercise() -> None:
|
|
await _seed("u_alice")
|
|
await _seed("u_bob")
|
|
|
|
with as_user("u_alice"):
|
|
response = await sessions_router.delete_session(session_id)
|
|
|
|
assert response == {"deleted": True, "session_id": session_id, "recycled": True}
|
|
assert await _artifacts_exist("u_alice") == (True, True)
|
|
with as_user("u_alice"):
|
|
purge_response = await sessions_router.purge_session(session_id)
|
|
assert purge_response == {"purged": True, "session_id": session_id}
|
|
assert await _artifacts_exist("u_alice") == (False, False)
|
|
assert await _artifacts_exist("u_bob") == (True, True)
|
|
|
|
asyncio.run(_exercise())
|