""" Editor routes for BlockNote document editing. """ from datetime import UTC, datetime from typing import Any from fastapi import APIRouter, Depends, HTTPException from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.db import Document, SearchSpace, User, get_async_session from app.users import current_active_user router = APIRouter() @router.get("/documents/{document_id}/editor-content") async def get_editor_content( document_id: int, session: AsyncSession = Depends(get_async_session), user: User = Depends(current_active_user), ): """ Get document content for editing. Returns BlockNote JSON document. If blocknote_document is NULL, attempts to generate it from chunks (lazy migration). """ from sqlalchemy.orm import selectinload result = await session.execute( select(Document) .options(selectinload(Document.chunks)) .join(SearchSpace) .filter(Document.id == document_id, SearchSpace.user_id == user.id) ) document = result.scalars().first() if not document: raise HTTPException(status_code=404, detail="Document not found") # If blocknote_document exists, return it if document.blocknote_document: return { "document_id": document.id, "title": document.title, "blocknote_document": document.blocknote_document, "last_edited_at": document.last_edited_at.isoformat() if document.last_edited_at else None, } # Lazy migration: Try to generate blocknote_document from chunks from app.utils.blocknote_converter import convert_markdown_to_blocknote chunks = sorted(document.chunks, key=lambda c: c.id) if not chunks: raise HTTPException( status_code=400, detail="This document has no chunks and cannot be edited. Please re-upload to enable editing.", ) # Reconstruct markdown from chunks markdown_content = "\n\n".join(chunk.content for chunk in chunks) if not markdown_content.strip(): raise HTTPException( status_code=400, detail="This document has empty content and cannot be edited.", ) # Convert to BlockNote blocknote_json = await convert_markdown_to_blocknote(markdown_content) if not blocknote_json: raise HTTPException( status_code=500, detail="Failed to convert document to editable format. Please try again later.", ) # Save the generated blocknote_document (lazy migration) document.blocknote_document = blocknote_json document.content_needs_reindexing = False document.last_edited_at = None await session.commit() return { "document_id": document.id, "title": document.title, "blocknote_document": blocknote_json, "last_edited_at": None, } @router.post("/documents/{document_id}/save") async def save_document( document_id: int, data: dict[str, Any], session: AsyncSession = Depends(get_async_session), user: User = Depends(current_active_user), ): """ Save BlockNote document and trigger reindexing. Called when user clicks 'Save & Exit'. """ from app.tasks.celery_tasks.document_reindex_tasks import reindex_document_task # Verify ownership result = await session.execute( select(Document) .join(SearchSpace) .filter(Document.id == document_id, SearchSpace.user_id == user.id) ) document = result.scalars().first() if not document: raise HTTPException(status_code=404, detail="Document not found") blocknote_document = data.get("blocknote_document") if not blocknote_document: raise HTTPException(status_code=400, detail="blocknote_document is required") # Save BlockNote document document.blocknote_document = blocknote_document document.last_edited_at = datetime.now(UTC) document.content_needs_reindexing = True await session.commit() # Queue reindex task reindex_document_task.delay(document_id, str(user.id)) return { "status": "saved", "document_id": document_id, "message": "Document saved and will be reindexed in the background", "last_edited_at": document.last_edited_at.isoformat(), }