From ba57c39556534e69350ba208aff5d3dcd425130d Mon Sep 17 00:00:00 2001 From: CREDO23 Date: Mon, 19 Jan 2026 14:36:29 +0200 Subject: [PATCH] feat(backend): add has_comments field to thread API --- surfsense_backend/app/routes/new_chat_routes.py | 17 +++++++++++++++-- surfsense_backend/app/schemas/new_chat.py | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/surfsense_backend/app/routes/new_chat_routes.py b/surfsense_backend/app/routes/new_chat_routes.py index e4dc5714a..7a5224ba6 100644 --- a/surfsense_backend/app/routes/new_chat_routes.py +++ b/surfsense_backend/app/routes/new_chat_routes.py @@ -19,13 +19,14 @@ from datetime import UTC, datetime from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile from fastapi.responses import StreamingResponse -from sqlalchemy import or_ +from sqlalchemy import func, or_ from sqlalchemy.exc import IntegrityError, OperationalError from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.future import select from sqlalchemy.orm import selectinload from app.db import ( + ChatComment, ChatVisibility, NewChatMessage, NewChatMessageRole, @@ -508,7 +509,19 @@ async def get_thread_full( # Check thread-level access based on visibility 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: raise diff --git a/surfsense_backend/app/schemas/new_chat.py b/surfsense_backend/app/schemas/new_chat.py index 3734b0470..24e779b50 100644 --- a/surfsense_backend/app/schemas/new_chat.py +++ b/surfsense_backend/app/schemas/new_chat.py @@ -105,6 +105,7 @@ class NewChatThreadWithMessages(NewChatThreadRead): """Schema for reading a thread with its messages.""" messages: list[NewChatMessageRead] = [] + has_comments: bool = False # =============================================================================