mirror of
https://github.com/open-webui/open-webui.git
synced 2026-04-30 01:10:17 -05:00
Avoid loading full chat JSON blob for pinned/archived/shared list endpoints (#21591)
Co-authored-by: Tim Baek <tim@openwebui.com>
This commit is contained in:
@@ -714,7 +714,7 @@ class ChatTable:
|
|||||||
skip: int = 0,
|
skip: int = 0,
|
||||||
limit: int = 50,
|
limit: int = 50,
|
||||||
db: Optional[Session] = None,
|
db: Optional[Session] = None,
|
||||||
) -> list[ChatModel]:
|
) -> list[ChatTitleIdResponse]:
|
||||||
|
|
||||||
with get_db_context(db) as db:
|
with get_db_context(db) as db:
|
||||||
query = db.query(Chat).filter_by(user_id=user_id, archived=True)
|
query = db.query(Chat).filter_by(user_id=user_id, archived=True)
|
||||||
@@ -740,13 +740,27 @@ class ChatTable:
|
|||||||
else:
|
else:
|
||||||
query = query.order_by(Chat.updated_at.desc())
|
query = query.order_by(Chat.updated_at.desc())
|
||||||
|
|
||||||
|
query = query.with_entities(
|
||||||
|
Chat.id, Chat.title, Chat.updated_at, Chat.created_at
|
||||||
|
)
|
||||||
|
|
||||||
if skip:
|
if skip:
|
||||||
query = query.offset(skip)
|
query = query.offset(skip)
|
||||||
if limit:
|
if limit:
|
||||||
query = query.limit(limit)
|
query = query.limit(limit)
|
||||||
|
|
||||||
all_chats = query.all()
|
all_chats = query.all()
|
||||||
return [ChatModel.model_validate(chat) for chat in all_chats]
|
return [
|
||||||
|
ChatTitleIdResponse.model_validate(
|
||||||
|
{
|
||||||
|
"id": chat[0],
|
||||||
|
"title": chat[1],
|
||||||
|
"updated_at": chat[2],
|
||||||
|
"created_at": chat[3],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
for chat in all_chats
|
||||||
|
]
|
||||||
|
|
||||||
def get_shared_chat_list_by_user_id(
|
def get_shared_chat_list_by_user_id(
|
||||||
self,
|
self,
|
||||||
@@ -802,12 +816,14 @@ class ChatTable:
|
|||||||
|
|
||||||
all_chats = query.all()
|
all_chats = query.all()
|
||||||
return [
|
return [
|
||||||
SharedChatResponse(
|
SharedChatResponse.model_validate(
|
||||||
id=chat[0],
|
{
|
||||||
title=chat[1],
|
"id": chat[0],
|
||||||
share_id=chat[2],
|
"title": chat[1],
|
||||||
updated_at=chat[3],
|
"share_id": chat[2],
|
||||||
created_at=chat[4],
|
"updated_at": chat[3],
|
||||||
|
"created_at": chat[4],
|
||||||
|
}
|
||||||
)
|
)
|
||||||
for chat in all_chats
|
for chat in all_chats
|
||||||
]
|
]
|
||||||
@@ -1017,14 +1033,27 @@ class ChatTable:
|
|||||||
|
|
||||||
def get_pinned_chats_by_user_id(
|
def get_pinned_chats_by_user_id(
|
||||||
self, user_id: str, db: Optional[Session] = None
|
self, user_id: str, db: Optional[Session] = None
|
||||||
) -> list[ChatModel]:
|
) -> list[ChatTitleIdResponse]:
|
||||||
with get_db_context(db) as db:
|
with get_db_context(db) as db:
|
||||||
all_chats = (
|
all_chats = (
|
||||||
db.query(Chat)
|
db.query(Chat)
|
||||||
.filter_by(user_id=user_id, pinned=True, archived=False)
|
.filter_by(user_id=user_id, pinned=True, archived=False)
|
||||||
.order_by(Chat.updated_at.desc())
|
.order_by(Chat.updated_at.desc())
|
||||||
|
.with_entities(
|
||||||
|
Chat.id, Chat.title, Chat.updated_at, Chat.created_at
|
||||||
|
)
|
||||||
)
|
)
|
||||||
return [ChatModel.model_validate(chat) for chat in all_chats]
|
return [
|
||||||
|
ChatTitleIdResponse.model_validate(
|
||||||
|
{
|
||||||
|
"id": chat[0],
|
||||||
|
"title": chat[1],
|
||||||
|
"updated_at": chat[2],
|
||||||
|
"created_at": chat[3],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
for chat in all_chats
|
||||||
|
]
|
||||||
|
|
||||||
def get_archived_chats_by_user_id(
|
def get_archived_chats_by_user_id(
|
||||||
self, user_id: str, db: Optional[Session] = None
|
self, user_id: str, db: Optional[Session] = None
|
||||||
|
|||||||
@@ -723,10 +723,7 @@ async def get_chat_list_by_folder_id(
|
|||||||
async def get_user_pinned_chats(
|
async def get_user_pinned_chats(
|
||||||
user=Depends(get_verified_user), db: Session = Depends(get_session)
|
user=Depends(get_verified_user), db: Session = Depends(get_session)
|
||||||
):
|
):
|
||||||
return [
|
return Chats.get_pinned_chats_by_user_id(user.id, db=db)
|
||||||
ChatTitleIdResponse(**chat.model_dump())
|
|
||||||
for chat in Chats.get_pinned_chats_by_user_id(user.id, db=db)
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
############################
|
############################
|
||||||
@@ -821,18 +818,13 @@ async def get_archived_session_user_chat_list(
|
|||||||
if direction:
|
if direction:
|
||||||
filter["direction"] = direction
|
filter["direction"] = direction
|
||||||
|
|
||||||
chat_list = [
|
return Chats.get_archived_chat_list_by_user_id(
|
||||||
ChatTitleIdResponse(**chat.model_dump())
|
user.id,
|
||||||
for chat in Chats.get_archived_chat_list_by_user_id(
|
filter=filter,
|
||||||
user.id,
|
skip=skip,
|
||||||
filter=filter,
|
limit=limit,
|
||||||
skip=skip,
|
db=db,
|
||||||
limit=limit,
|
)
|
||||||
db=db,
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
return chat_list
|
|
||||||
|
|
||||||
|
|
||||||
############################
|
############################
|
||||||
|
|||||||
Reference in New Issue
Block a user