This commit is contained in:
Timothy Jaeryang Baek
2026-06-17 00:25:35 +02:00
parent 6b2d962cd6
commit 232421f40b
3 changed files with 36 additions and 1 deletions
+15
View File
@@ -1571,6 +1571,21 @@ class ChatTable:
log.info(f"Count of chats for folder '{folder_id}': {count}")
return count
async def count_chats_by_folder_ids_and_user_id(
self, folder_ids: list[str], user_id: str, db: AsyncSession | None = None
) -> int:
if not folder_ids:
return 0
async with get_async_db_context(db) as session:
result = await session.execute(
select(func.count(Chat.id)).filter(Chat.user_id == user_id, Chat.folder_id.in_(folder_ids))
)
count = result.scalar()
log.info(f"Count of chats for folders '{folder_ids}': {count}")
return count
async def delete_tag_by_id_and_user_id_and_tag_name(
self, id: str, user_id: str, tag_name: str, db: AsyncSession | None = None
) -> bool:
+19
View File
@@ -252,6 +252,25 @@ class FolderTable:
result = await db.execute(select(Folder).filter_by(parent_id=parent_id, user_id=user_id))
return [FolderModel.model_validate(folder) for folder in result.scalars().all()]
async def get_folder_ids_by_id_and_user_id_in_subtree(
self, id: str, user_id: str, db: Optional[AsyncSession] = None
) -> list[str]:
async with get_async_db_context(db) as db:
result = await db.execute(select(Folder).filter_by(id=id, user_id=user_id))
folder = result.scalars().first()
if not folder:
return []
folder_ids = [folder.id]
folders = [FolderModel.model_validate(folder)]
while folders:
current_folder = folders.pop()
children = await self.get_folders_by_parent_id_and_user_id(current_folder.id, user_id, db=db)
folder_ids.extend(child.id for child in children)
folders.extend(children)
return folder_ids
async def update_folder_parent_id_by_id_and_user_id(
self,
id: str,
+2 -1
View File
@@ -519,7 +519,8 @@ async def delete_folder_by_id(
folder_owner_id = folder.user_id
if await Chats.count_chats_by_folder_id_and_user_id(id, folder_owner_id, db=db):
folder_ids = await Folders.get_folder_ids_by_id_and_user_id_in_subtree(id, folder_owner_id, db=db)
if await Chats.count_chats_by_folder_ids_and_user_id(folder_ids, folder_owner_id, db=db):
chat_delete_permission = await has_permission(
user.id, 'chat.delete', request.app.state.config.USER_PERMISSIONS, db=db
)