mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-30 17:05:16 -05:00
refac
This commit is contained in:
1 parent
9a5ce52843
commit
91ef645d95
3 files changed
+25
-5
No files matched your search
@@ -2257,14 +2257,16 @@ class ChatTable:
|
||||
include_archived: bool = False,
|
||||
skip: int = 0,
|
||||
limit: int = 60,
|
||||
where: ColumnElement[bool] | None = None,
|
||||
db: AsyncSession | None = None,
|
||||
) -> list[ChatTitleIdResponse]:
|
||||
"""
|
||||
Search chats without loading their bodies, with pagination applied in SQL.
|
||||
where adds a condition the chats must also meet.
|
||||
"""
|
||||
search_text = sanitize_text_for_db(search_text).lower().strip()
|
||||
|
||||
if not search_text:
|
||||
if not search_text and where is None:
|
||||
return await self.get_chat_list_by_user_id(
|
||||
user_id, include_archived, filter={}, skip=skip, limit=limit, db=db
|
||||
)
|
||||
@@ -2327,6 +2329,8 @@ class ChatTable:
|
||||
|
||||
if folder_names:
|
||||
stmt = stmt.filter(Chat.folder_id.in_(folder_ids))
|
||||
if where is not None:
|
||||
stmt = stmt.filter(where)
|
||||
|
||||
# Check if the database dialect is either 'sqlite' or 'postgresql'
|
||||
bind = await session.connection()
|
||||
|
||||
@@ -76,7 +76,8 @@ from open_webui.utils.access_control.files import has_access_to_note_files
|
||||
from open_webui.utils.access_control.notes import has_note_access
|
||||
from open_webui.utils.chat_id import is_saved_chat_id
|
||||
from open_webui.utils.json_codec import JSONCodec
|
||||
from open_webui.utils.memory import get_memory_folder
|
||||
from open_webui.models.folders import Folders
|
||||
from open_webui.utils.memory import get_chat_search_condition, get_memory_folder, is_folder_only
|
||||
from open_webui.utils.notifications import notify_target
|
||||
from open_webui.utils.sanitize import sanitize_code
|
||||
from open_webui.utils.skill_files import SkillFile, SkillFileOperation, skill_content_page, skill_overview
|
||||
@@ -1796,6 +1797,7 @@ async def search_chats(
|
||||
include_archived=False,
|
||||
skip=0,
|
||||
limit=count * 3, # Fetch more for filtering
|
||||
where=await get_chat_search_condition(UserModel(**__user__), getattr(__request__.state, 'metadata', None)),
|
||||
)
|
||||
|
||||
matched = []
|
||||
@@ -1862,7 +1864,11 @@ async def view_chat(
|
||||
|
||||
chat = await Chats.get_chat_by_id_and_user_id(chat_id, user_id)
|
||||
|
||||
if not chat:
|
||||
folder = await get_memory_folder(UserModel(**__user__), getattr(__request__.state, 'metadata', None))
|
||||
chat_folder = await Folders.get_folder_by_id(chat.folder_id) if chat and chat.folder_id else None
|
||||
if not chat or (
|
||||
chat.folder_id != folder.id if is_folder_only(folder) else is_folder_only(chat_folder)
|
||||
):
|
||||
return JSONCodec.dumps({'error': 'Chat not found or access denied'})
|
||||
|
||||
# Extract messages from history
|
||||
|
||||
@@ -6,9 +6,10 @@ import re
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException
|
||||
from open_webui.models.chats import Chats
|
||||
from sqlalchemy import or_, select
|
||||
from open_webui.models.chats import Chat, Chats
|
||||
from open_webui.models.config import Config
|
||||
from open_webui.models.folders import FolderModel, Folders
|
||||
from open_webui.models.folders import Folder, FolderModel, Folders
|
||||
from open_webui.models.memories import Memories
|
||||
from open_webui.utils.access_control import has_permission
|
||||
from open_webui.utils.access_control.folders import has_folder_access
|
||||
@@ -264,6 +265,15 @@ async def get_memory_folder(user, metadata: dict | None) -> FolderModel | None:
|
||||
return folder
|
||||
|
||||
|
||||
async def get_chat_search_condition(user, metadata: dict | None):
|
||||
"""Past chats this chat may look up: its folder's if that is Folder only, else all but Folder only folders'."""
|
||||
folder = await get_memory_folder(user, metadata)
|
||||
if is_folder_only(folder):
|
||||
return Chat.folder_id == folder.id
|
||||
folder_only_ids = select(Folder.id).where(Folder.user_id == user.id, Folder.data['memory'].as_string() == 'folder')
|
||||
return or_(Chat.folder_id.is_(None), Chat.folder_id.notin_(folder_only_ids))
|
||||
|
||||
|
||||
def is_folder_only(folder: FolderModel | None) -> bool:
|
||||
"""Whether the folder's chats use only its own memories, leaving out account memories."""
|
||||
return bool(folder) and (folder.data or {}).get('memory') == 'folder'
|
||||
|
||||
Reference in new issue
Block a user