2025-11-23 15:23:31 +05:30
|
|
|
"""
|
|
|
|
|
Editor routes for BlockNote document editing.
|
|
|
|
|
"""
|
2025-11-23 16:39:23 +05:30
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
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
|
2025-11-30 14:27:27 -08:00
|
|
|
from sqlalchemy.orm import selectinload
|
2025-11-23 15:23:31 +05:30
|
|
|
|
2025-12-16 12:28:30 +05:30
|
|
|
from app.db import Document, DocumentType, Permission, User, get_async_session
|
2025-11-23 15:23:31 +05:30
|
|
|
from app.users import current_active_user
|
2025-11-30 14:27:27 -08:00
|
|
|
from app.utils.rbac import check_permission
|
2025-11-23 16:47:09 +05:30
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
2025-11-30 15:06:48 -08:00
|
|
|
@router.get("/search-spaces/{search_space_id}/documents/{document_id}/editor-content")
|
2025-11-23 15:23:31 +05:30
|
|
|
async def get_editor_content(
|
2025-11-30 14:27:27 -08:00
|
|
|
search_space_id: int,
|
2025-11-23 15:23:31 +05:30
|
|
|
document_id: int,
|
|
|
|
|
session: AsyncSession = Depends(get_async_session),
|
|
|
|
|
user: User = Depends(current_active_user),
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Get document content for editing.
|
2025-11-23 16:39:23 +05:30
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
Returns BlockNote JSON document. If blocknote_document is NULL,
|
2025-11-30 03:49:43 +05:30
|
|
|
attempts to generate it from chunks (lazy migration).
|
2025-11-30 14:27:27 -08:00
|
|
|
|
|
|
|
|
Requires DOCUMENTS_READ permission.
|
2025-11-23 15:23:31 +05:30
|
|
|
"""
|
2025-11-30 14:27:27 -08:00
|
|
|
# Check RBAC permission
|
|
|
|
|
await check_permission(
|
|
|
|
|
session,
|
|
|
|
|
user,
|
|
|
|
|
search_space_id,
|
|
|
|
|
Permission.DOCUMENTS_READ.value,
|
|
|
|
|
"You don't have permission to read documents in this search space",
|
|
|
|
|
)
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
result = await session.execute(
|
|
|
|
|
select(Document)
|
2025-11-30 03:49:43 +05:30
|
|
|
.options(selectinload(Document.chunks))
|
2025-11-30 14:27:27 -08:00
|
|
|
.filter(
|
|
|
|
|
Document.id == document_id,
|
|
|
|
|
Document.search_space_id == search_space_id,
|
|
|
|
|
)
|
2025-11-23 15:23:31 +05:30
|
|
|
)
|
2025-11-23 16:24:11 +05:30
|
|
|
document = result.scalars().first()
|
2025-11-23 16:39:23 +05:30
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
if not document:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Document not found")
|
2025-11-23 16:39:23 +05:30
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
# If blocknote_document exists, return it
|
|
|
|
|
if document.blocknote_document:
|
|
|
|
|
return {
|
|
|
|
|
"document_id": document.id,
|
|
|
|
|
"title": document.title,
|
2025-12-16 13:57:06 +05:30
|
|
|
"document_type": document.document_type.value,
|
2025-11-23 15:23:31 +05:30
|
|
|
"blocknote_document": document.blocknote_document,
|
2025-12-12 01:32:14 -08:00
|
|
|
"updated_at": document.updated_at.isoformat()
|
|
|
|
|
if document.updated_at
|
2025-11-23 16:39:23 +05:30
|
|
|
else None,
|
2025-11-23 15:23:31 +05:30
|
|
|
}
|
2025-11-23 16:39:23 +05:30
|
|
|
|
2025-12-16 12:28:30 +05:30
|
|
|
# For NOTE type documents, return empty BlockNote structure if no content exists
|
|
|
|
|
if document.document_type == DocumentType.NOTE:
|
|
|
|
|
# Return empty BlockNote structure
|
|
|
|
|
empty_blocknote = [
|
|
|
|
|
{
|
|
|
|
|
"type": "paragraph",
|
|
|
|
|
"content": [],
|
|
|
|
|
"children": [],
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
# Save empty structure if not already saved
|
|
|
|
|
if not document.blocknote_document:
|
|
|
|
|
document.blocknote_document = empty_blocknote
|
|
|
|
|
await session.commit()
|
|
|
|
|
return {
|
|
|
|
|
"document_id": document.id,
|
|
|
|
|
"title": document.title,
|
2025-12-16 13:57:06 +05:30
|
|
|
"document_type": document.document_type.value,
|
2025-12-16 12:28:30 +05:30
|
|
|
"blocknote_document": empty_blocknote,
|
2025-12-16 20:25:50 +05:30
|
|
|
"updated_at": document.updated_at.isoformat()
|
|
|
|
|
if document.updated_at
|
|
|
|
|
else None,
|
2025-12-16 12:28:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Lazy migration: Try to generate blocknote_document from chunks (for other document types)
|
2025-11-30 03:49:43 +05:30
|
|
|
from app.utils.blocknote_converter import convert_markdown_to_blocknote
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-30 03:49:43 +05:30
|
|
|
chunks = sorted(document.chunks, key=lambda c: c.id)
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-30 03:49:43 +05:30
|
|
|
if not chunks:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=400,
|
|
|
|
|
detail="This document has no chunks and cannot be edited. Please re-upload to enable editing.",
|
|
|
|
|
)
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-30 03:49:43 +05:30
|
|
|
# Reconstruct markdown from chunks
|
|
|
|
|
markdown_content = "\n\n".join(chunk.content for chunk in chunks)
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-30 03:49:43 +05:30
|
|
|
if not markdown_content.strip():
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=400,
|
|
|
|
|
detail="This document has empty content and cannot be edited.",
|
|
|
|
|
)
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-30 03:49:43 +05:30
|
|
|
# Convert to BlockNote
|
|
|
|
|
blocknote_json = await convert_markdown_to_blocknote(markdown_content)
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-30 03:49:43 +05:30
|
|
|
if not blocknote_json:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=500,
|
|
|
|
|
detail="Failed to convert document to editable format. Please try again later.",
|
|
|
|
|
)
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-30 03:49:43 +05:30
|
|
|
# Save the generated blocknote_document (lazy migration)
|
|
|
|
|
document.blocknote_document = blocknote_json
|
|
|
|
|
document.content_needs_reindexing = False
|
|
|
|
|
await session.commit()
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-30 03:49:43 +05:30
|
|
|
return {
|
|
|
|
|
"document_id": document.id,
|
|
|
|
|
"title": document.title,
|
2025-12-16 13:57:06 +05:30
|
|
|
"document_type": document.document_type.value,
|
2025-11-30 03:49:43 +05:30
|
|
|
"blocknote_document": blocknote_json,
|
2025-12-12 01:32:14 -08:00
|
|
|
"updated_at": document.updated_at.isoformat() if document.updated_at else None,
|
2025-11-30 03:49:43 +05:30
|
|
|
}
|
2025-11-23 15:23:31 +05:30
|
|
|
|
|
|
|
|
|
2025-11-30 14:27:27 -08:00
|
|
|
@router.post("/search-spaces/{search_space_id}/documents/{document_id}/save")
|
2025-11-30 04:08:12 +05:30
|
|
|
async def save_document(
|
2025-11-30 14:27:27 -08:00
|
|
|
search_space_id: int,
|
2025-11-23 15:23:31 +05:30
|
|
|
document_id: int,
|
|
|
|
|
data: dict[str, Any],
|
|
|
|
|
session: AsyncSession = Depends(get_async_session),
|
|
|
|
|
user: User = Depends(current_active_user),
|
|
|
|
|
):
|
|
|
|
|
"""
|
2025-11-30 04:08:12 +05:30
|
|
|
Save BlockNote document and trigger reindexing.
|
|
|
|
|
Called when user clicks 'Save & Exit'.
|
2025-11-30 14:27:27 -08:00
|
|
|
|
|
|
|
|
Requires DOCUMENTS_UPDATE permission.
|
2025-11-23 15:23:31 +05:30
|
|
|
"""
|
2025-11-30 04:08:12 +05:30
|
|
|
from app.tasks.celery_tasks.document_reindex_tasks import reindex_document_task
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-30 14:27:27 -08:00
|
|
|
# Check RBAC permission
|
|
|
|
|
await check_permission(
|
|
|
|
|
session,
|
|
|
|
|
user,
|
|
|
|
|
search_space_id,
|
|
|
|
|
Permission.DOCUMENTS_UPDATE.value,
|
|
|
|
|
"You don't have permission to update documents in this search space",
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
result = await session.execute(
|
2025-11-30 14:27:27 -08:00
|
|
|
select(Document).filter(
|
|
|
|
|
Document.id == document_id,
|
|
|
|
|
Document.search_space_id == search_space_id,
|
|
|
|
|
)
|
2025-11-23 15:23:31 +05:30
|
|
|
)
|
2025-11-23 16:24:11 +05:30
|
|
|
document = result.scalars().first()
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
if not document:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Document not found")
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
blocknote_document = data.get("blocknote_document")
|
|
|
|
|
if not blocknote_document:
|
|
|
|
|
raise HTTPException(status_code=400, detail="blocknote_document is required")
|
2025-12-17 00:09:43 -08:00
|
|
|
|
2025-12-16 21:27:31 +05:30
|
|
|
# Add type validation
|
|
|
|
|
if not isinstance(blocknote_document, list):
|
|
|
|
|
raise HTTPException(status_code=400, detail="blocknote_document must be a list")
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-12-16 13:57:06 +05:30
|
|
|
# For NOTE type documents, extract title from first block (heading)
|
|
|
|
|
if (
|
|
|
|
|
document.document_type == DocumentType.NOTE
|
|
|
|
|
and blocknote_document
|
|
|
|
|
and len(blocknote_document) > 0
|
|
|
|
|
):
|
|
|
|
|
first_block = blocknote_document[0]
|
2025-12-16 20:25:50 +05:30
|
|
|
if (
|
|
|
|
|
first_block
|
|
|
|
|
and first_block.get("content")
|
|
|
|
|
and isinstance(first_block["content"], list)
|
|
|
|
|
):
|
2025-12-16 13:57:06 +05:30
|
|
|
# Extract text from first block content
|
2025-12-16 20:25:50 +05:30
|
|
|
# Match the frontend extractTitleFromBlockNote logic exactly
|
2025-12-16 13:57:06 +05:30
|
|
|
title_parts = []
|
|
|
|
|
for item in first_block["content"]:
|
|
|
|
|
if isinstance(item, str):
|
|
|
|
|
title_parts.append(item)
|
2025-12-16 20:25:50 +05:30
|
|
|
elif (
|
|
|
|
|
isinstance(item, dict)
|
|
|
|
|
and "text" in item
|
|
|
|
|
and isinstance(item["text"], str)
|
|
|
|
|
):
|
|
|
|
|
# BlockNote structure: {"type": "text", "text": "...", "styles": {}}
|
2025-12-16 13:57:06 +05:30
|
|
|
title_parts.append(item["text"])
|
2025-12-16 20:25:50 +05:30
|
|
|
|
2025-12-16 13:57:06 +05:30
|
|
|
new_title = "".join(title_parts).strip()
|
|
|
|
|
if new_title:
|
|
|
|
|
document.title = new_title
|
|
|
|
|
else:
|
2025-12-16 20:25:50 +05:30
|
|
|
# Only set to "Untitled" if content exists but is empty
|
2025-12-16 13:57:06 +05:30
|
|
|
document.title = "Untitled"
|
|
|
|
|
|
2025-11-30 04:08:12 +05:30
|
|
|
# Save BlockNote document
|
2025-11-23 15:23:31 +05:30
|
|
|
document.blocknote_document = blocknote_document
|
2025-12-12 01:32:14 -08:00
|
|
|
document.updated_at = datetime.now(UTC)
|
2025-11-30 04:08:12 +05:30
|
|
|
document.content_needs_reindexing = True
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-23 15:23:31 +05:30
|
|
|
await session.commit()
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-30 04:08:12 +05:30
|
|
|
# Queue reindex task
|
|
|
|
|
reindex_document_task.delay(document_id, str(user.id))
|
2025-11-30 04:15:38 +05:30
|
|
|
|
2025-11-30 04:08:12 +05:30
|
|
|
return {
|
|
|
|
|
"status": "saved",
|
|
|
|
|
"document_id": document_id,
|
|
|
|
|
"message": "Document saved and will be reindexed in the background",
|
2025-12-12 01:32:14 -08:00
|
|
|
"updated_at": document.updated_at.isoformat(),
|
2025-11-30 04:08:12 +05:30
|
|
|
}
|