refac: folders

This commit is contained in:
Timothy Jaeryang Baek 2025-03-30 23:36:15 -07:00
parent 337df80c47
commit e4cc9f2b4f
2 changed files with 28 additions and 8 deletions

View File

@ -9,6 +9,8 @@ from open_webui.models.chats import Chats
from open_webui.env import SRC_LOG_LEVELS from open_webui.env import SRC_LOG_LEVELS
from pydantic import BaseModel, ConfigDict from pydantic import BaseModel, ConfigDict
from sqlalchemy import BigInteger, Column, Text, JSON, Boolean from sqlalchemy import BigInteger, Column, Text, JSON, Boolean
from open_webui.utils.access_control import get_permissions
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"]) log.setLevel(SRC_LOG_LEVELS["MODELS"])
@ -234,13 +236,16 @@ class FolderTable:
log.error(f"update_folder: {e}") log.error(f"update_folder: {e}")
return return
def delete_folder_by_id_and_user_id(self, id: str, user_id: str) -> bool: def delete_folder_by_id_and_user_id(
self, id: str, user_id: str, delete_chats=True
) -> bool:
try: try:
with get_db() as db: with get_db() as db:
folder = db.query(Folder).filter_by(id=id, user_id=user_id).first() folder = db.query(Folder).filter_by(id=id, user_id=user_id).first()
if not folder: if not folder:
return False return False
if delete_chats:
# Delete all chats in the folder # Delete all chats in the folder
Chats.delete_chats_by_user_id_and_folder_id(user_id, folder.id) Chats.delete_chats_by_user_id_and_folder_id(user_id, folder.id)
@ -250,9 +255,11 @@ class FolderTable:
folder.id, user_id folder.id, user_id
) )
for folder_child in folder_children: for folder_child in folder_children:
if delete_chats:
Chats.delete_chats_by_user_id_and_folder_id( Chats.delete_chats_by_user_id_and_folder_id(
user_id, folder_child.id user_id, folder_child.id
) )
delete_children(folder_child) delete_children(folder_child)
folder = db.query(Folder).filter_by(id=folder_child.id).first() folder = db.query(Folder).filter_by(id=folder_child.id).first()

View File

@ -20,11 +20,13 @@ from open_webui.env import SRC_LOG_LEVELS
from open_webui.constants import ERROR_MESSAGES from open_webui.constants import ERROR_MESSAGES
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status, Request
from fastapi.responses import FileResponse, StreamingResponse from fastapi.responses import FileResponse, StreamingResponse
from open_webui.utils.auth import get_admin_user, get_verified_user from open_webui.utils.auth import get_admin_user, get_verified_user
from open_webui.utils.access_control import has_permission
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"]) log.setLevel(SRC_LOG_LEVELS["MODELS"])
@ -228,7 +230,18 @@ async def update_folder_is_expanded_by_id(
@router.delete("/{id}") @router.delete("/{id}")
async def delete_folder_by_id(id: str, user=Depends(get_verified_user)): async def delete_folder_by_id(
request: Request, id: str, user=Depends(get_verified_user)
):
chat_delete_permission = has_permission(
user.id, "chat.delete", request.app.state.config.USER_PERMISSIONS
)
if not chat_delete_permission:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
folder = Folders.get_folder_by_id_and_user_id(id, user.id) folder = Folders.get_folder_by_id_and_user_id(id, user.id)
if folder: if folder:
try: try: