refac: folder deletion

This commit is contained in:
Timothy J. Baek 2024-10-17 18:24:58 -07:00
parent 42d048741c
commit 7ffa3cb022
3 changed files with 33 additions and 6 deletions

View File

@ -699,6 +699,18 @@ class ChatTable:
except Exception:
return False
def delete_chats_by_user_id_and_folder_id(
self, user_id: str, folder_id: str
) -> bool:
try:
with get_db() as db:
db.query(Chat).filter_by(user_id=user_id, folder_id=folder_id).delete()
db.commit()
return True
except Exception:
return False
def delete_shared_chats_by_user_id(self, user_id: str) -> bool:
try:
with get_db() as db:

View File

@ -4,7 +4,7 @@ import uuid
from typing import Optional
from open_webui.apps.webui.internal.db import Base, get_db
from open_webui.apps.webui.models.chats import Chats
from open_webui.env import SRC_LOG_LEVELS
from pydantic import BaseModel, ConfigDict
@ -214,6 +214,26 @@ class FolderTable:
try:
with get_db() as db:
folder = db.query(Folder).filter_by(id=id, user_id=user_id).first()
if not folder:
return False
# Delete all chats in the folder
Chats.delete_chats_by_user_id_and_folder_id(user_id, folder.id)
# Delete all children folders
def delete_children(folder):
folder_children = self.get_folders_by_parent_id_and_user_id(
folder.id, user_id
)
for folder_child in folder_children:
Chats.delete_chats_by_user_id_and_folder_id(
user_id, folder_child.id
)
delete_children(folder_child)
db.delete(folder_child)
db.commit()
delete_children(folder)
db.delete(folder)
db.commit()
return True

View File

@ -234,11 +234,6 @@ async def delete_folder_by_id(id: str, user=Depends(get_verified_user)):
try:
result = Folders.delete_folder_by_id_and_user_id(id, user.id)
if result:
# Delete all chats in the folder
chats = Chats.get_chats_by_folder_id_and_user_id(id, user.id)
for chat in chats:
Chats.delete_chat_by_id(chat.id, user.id)
return result
else:
raise HTTPException(