mirror of
https://github.com/open-webui/open-webui.git
synced 2026-09-27 17:56:26 -04:00
refac
This commit is contained in:
@@ -1402,7 +1402,7 @@ async def chat_completion(
|
||||
updated['files'] = chat_files
|
||||
if selected_chat_models:
|
||||
updated['models'] = selected_chat_models
|
||||
await Chats.update_chat_by_id(chat_id, updated)
|
||||
await Chats.update_chat_by_id(chat_id, updated, touch=False)
|
||||
|
||||
await Chats.update_chat_variables_by_id(chat_id, chat_variables)
|
||||
|
||||
|
||||
@@ -614,17 +614,18 @@ class ChatTable:
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
async def update_chat_last_read_at_by_id(self, id: str, user_id: str, db: AsyncSession | None = None) -> bool:
|
||||
async def update_chat_last_read_at_by_id(self, id: str, user_id: str, db: AsyncSession | None = None) -> int | None:
|
||||
try:
|
||||
async with get_async_db_context(db) as session:
|
||||
chat = await session.get(Chat, id)
|
||||
if chat and chat.user_id == user_id:
|
||||
chat.last_read_at = int(time.time())
|
||||
last_read_at = int(time.time())
|
||||
chat.last_read_at = last_read_at
|
||||
await session.commit()
|
||||
return True
|
||||
return False
|
||||
return last_read_at
|
||||
return None
|
||||
except Exception:
|
||||
return False
|
||||
return None
|
||||
|
||||
async def update_chat_title_by_id(self, id: str, title: str) -> ChatModel | None:
|
||||
try:
|
||||
|
||||
@@ -31,6 +31,7 @@ from open_webui.env import (
|
||||
from open_webui.models.access_grants import AccessGrants
|
||||
from open_webui.models.channels import Channels
|
||||
from open_webui.models.chats import Chats
|
||||
from open_webui.models.folders import Folders
|
||||
from open_webui.models.notes import Notes, NoteUpdateForm
|
||||
from open_webui.models.users import UserNameResponse, Users
|
||||
from open_webui.socket.utils import RedisDict, RedisLock, YdocManager
|
||||
@@ -508,6 +509,24 @@ async def channel_events(sid, data):
|
||||
await Channels.update_member_last_read_at(data['channel_id'], user['id'])
|
||||
|
||||
|
||||
async def get_folder_unread_counts(user_id: str) -> dict[str, int]:
|
||||
folder_list = await Folders.get_folders_by_user_id(user_id)
|
||||
parent_by_id = {folder.id: folder.parent_id for folder in folder_list}
|
||||
unread_counts = dict.fromkeys(parent_by_id.keys(), 0)
|
||||
|
||||
direct_unread_counts = await Chats.count_unread_by_folder_ids(user_id, list(parent_by_id.keys()))
|
||||
for unread_folder_id, unread_count in direct_unread_counts.items():
|
||||
current_id = unread_folder_id
|
||||
seen = set()
|
||||
while current_id and current_id not in seen:
|
||||
seen.add(current_id)
|
||||
if current_id in unread_counts:
|
||||
unread_counts[current_id] += unread_count
|
||||
current_id = parent_by_id.get(current_id)
|
||||
|
||||
return unread_counts
|
||||
|
||||
|
||||
@sio.on('events:chat')
|
||||
async def chat_events(sid, data):
|
||||
try:
|
||||
@@ -526,13 +545,21 @@ async def chat_events(sid, data):
|
||||
event_type = event_data.get('type')
|
||||
|
||||
if event_type == 'last_read_at':
|
||||
if not await Chats.update_chat_last_read_at_by_id(data['chat_id'], user['id']):
|
||||
last_read_at = await Chats.update_chat_last_read_at_by_id(data['chat_id'], user['id'])
|
||||
if not last_read_at:
|
||||
return
|
||||
await sio.emit(
|
||||
'events',
|
||||
{
|
||||
'chat_id': data['chat_id'],
|
||||
'data': {'type': 'chat:list'},
|
||||
'data': {
|
||||
'type': 'chat:list',
|
||||
'data': {
|
||||
'chat_id': data['chat_id'],
|
||||
'last_read_at': last_read_at,
|
||||
'folder_unread_counts': await get_folder_unread_counts(user['id']),
|
||||
},
|
||||
},
|
||||
},
|
||||
room=f'user:{user["id"]}',
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user