feat: implement batch unread counts for notifications to reduce API calls and improve performance

This commit is contained in:
DESKTOP-RTLN3BA\$punk 2026-03-10 01:26:37 -07:00
parent 7362da52d3
commit 403097646d
18 changed files with 450 additions and 51 deletions

View file

@ -1056,6 +1056,9 @@ async def delete_document(
Delete a document.
Requires DOCUMENTS_DELETE permission for the search space.
Documents in "processing" state cannot be deleted.
Heavy cascade deletion runs asynchronously via Celery so the API
response is fast and the deletion remains durable across API restarts.
"""
try:
result = await session.execute(
@ -1068,13 +1071,17 @@ async def delete_document(
status_code=404, detail=f"Document with id {document_id} not found"
)
# Check if document is pending or currently being processed
doc_state = document.status.get("state") if document.status else None
if doc_state in ("pending", "processing"):
raise HTTPException(
status_code=409, # Conflict
status_code=409,
detail="Cannot delete document while it is pending or being processed. Please wait for processing to complete.",
)
if doc_state == "deleting":
raise HTTPException(
status_code=409,
detail="Document is already being deleted.",
)
# Check permission for the search space
await check_permission(
@ -1085,8 +1092,25 @@ async def delete_document(
"You don't have permission to delete documents in this search space",
)
await session.delete(document)
# Mark the document as "deleting" so it's excluded from searches,
# then commit immediately so the user gets a fast response.
document.status = {"state": "deleting"}
await session.commit()
# Dispatch durable background deletion via Celery.
# If queue dispatch fails, revert status to avoid a stuck "deleting" document.
try:
from app.tasks.celery_tasks.document_tasks import delete_document_task
delete_document_task.delay(document_id)
except Exception as dispatch_error:
document.status = {"state": "ready"}
await session.commit()
raise HTTPException(
status_code=503,
detail="Failed to queue background deletion. Please try again.",
) from dispatch_error
return {"message": "Document deleted successfully"}
except HTTPException:
raise