feat(backend): add has_comments field to thread API

This commit is contained in:
CREDO23 2026-01-19 14:36:29 +02:00
parent 9d11446553
commit ba57c39556
2 changed files with 16 additions and 2 deletions

View file

@ -19,13 +19,14 @@ from datetime import UTC, datetime
from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from sqlalchemy import or_ from sqlalchemy import func, or_
from sqlalchemy.exc import IntegrityError, OperationalError from sqlalchemy.exc import IntegrityError, OperationalError
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select from sqlalchemy.future import select
from sqlalchemy.orm import selectinload from sqlalchemy.orm import selectinload
from app.db import ( from app.db import (
ChatComment,
ChatVisibility, ChatVisibility,
NewChatMessage, NewChatMessage,
NewChatMessageRole, NewChatMessageRole,
@ -508,7 +509,19 @@ async def get_thread_full(
# Check thread-level access based on visibility # Check thread-level access based on visibility
await check_thread_access(session, thread, user) await check_thread_access(session, thread, user)
return thread # Check if thread has any comments
comment_count = await session.scalar(
select(func.count())
.select_from(ChatComment)
.join(NewChatMessage, ChatComment.message_id == NewChatMessage.id)
.where(NewChatMessage.thread_id == thread.id)
)
return {
**thread.__dict__,
"messages": thread.messages,
"has_comments": (comment_count or 0) > 0,
}
except HTTPException: except HTTPException:
raise raise

View file

@ -105,6 +105,7 @@ class NewChatThreadWithMessages(NewChatThreadRead):
"""Schema for reading a thread with its messages.""" """Schema for reading a thread with its messages."""
messages: list[NewChatMessageRead] = [] messages: list[NewChatMessageRead] = []
has_comments: bool = False
# ============================================================================= # =============================================================================