diff --git a/backend/open_webui/routers/channels.py b/backend/open_webui/routers/channels.py index ba1c2993c0..91731c920c 100644 --- a/backend/open_webui/routers/channels.py +++ b/backend/open_webui/routers/channels.py @@ -1217,14 +1217,19 @@ async def app_channel_reply_handler(channel_id, message_id, user_id): # An inactive/removed app or one without write access cannot post a public reply. log.warning('Could not save app error reply: app=%s message=%s', app_id, message_id) await emit_to_users( - 'events:app', + 'events', { - 'app_id': app_id, - 'app_name': app_name, - 'channel_id': channel_id, - 'message_id': message_id, - 'parent_id': parent_id, - 'reply': {'type': 'error', 'content': detail}, + 'data': { + 'type': 'channel:app', + 'data': { + 'app_id': app_id, + 'app_name': app_name, + 'channel_id': channel_id, + 'message_id': message_id, + 'parent_id': parent_id, + 'reply': {'type': 'error', 'content': detail}, + }, + } }, [user_id], ) diff --git a/backend/open_webui/routers/notes.py b/backend/open_webui/routers/notes.py index f0438425b5..eb8f77e463 100644 --- a/backend/open_webui/routers/notes.py +++ b/backend/open_webui/routers/notes.py @@ -566,8 +566,8 @@ async def update_note_by_id( event_data['data'] = note.data await sio.emit( - 'events:note', - event_data, + 'events', + {'data': {'type': 'note:updated', 'data': event_data}}, to=f'note:{note.id}', ) diff --git a/backend/open_webui/socket/comments.py b/backend/open_webui/socket/comments.py index 8722820d2c..e5c8cdaa37 100644 --- a/backend/open_webui/socket/comments.py +++ b/backend/open_webui/socket/comments.py @@ -81,4 +81,13 @@ async def revalidate_comment_subscription(sio, sid, session_user): key: value for key, value in session['comments_resources'].items() if tuple(value) != resource } await sio.save_session(sid, session) - await sio.emit('comments:unavailable', {'resource_type': resource[0], 'resource_id': resource[1]}, to=sid) + await sio.emit( + 'events', + { + 'data': { + 'type': 'comments:unavailable', + 'data': {'resource_type': resource[0], 'resource_id': resource[1]}, + } + }, + to=sid, + ) diff --git a/backend/open_webui/socket/encryption.py b/backend/open_webui/socket/encryption.py index 227fb9556d..b3ca7ba7b7 100644 --- a/backend/open_webui/socket/encryption.py +++ b/backend/open_webui/socket/encryption.py @@ -156,8 +156,13 @@ class Relay: async def invite(self, state, channel): await self.sio.emit( - 'encryption:available', - {'channel': channel, 'session': state['session'] if state else None}, + 'events', + { + 'data': { + 'type': 'encryption:available', + 'data': {'channel': channel, 'session': state['session'] if state else None}, + } + }, room=f'channel:{channel}', ) diff --git a/backend/open_webui/socket/main.py b/backend/open_webui/socket/main.py index c42e98cbaa..0669257e3e 100644 --- a/backend/open_webui/socket/main.py +++ b/backend/open_webui/socket/main.py @@ -304,8 +304,13 @@ async def publish_note_presence(note_id: str): if REDIS is not None and not await lock.owned(): return await sio.emit( - 'note:presence', - {'note_id': note_id, 'users': sorted(people.values(), key=lambda person: person['id'])}, + 'events', + { + 'data': { + 'type': 'note:presence', + 'data': {'note_id': note_id, 'users': sorted(people.values(), key=lambda person: person['id'])}, + } + }, room=f'note:{note_id}', ) @@ -1159,11 +1164,16 @@ async def document_save_handler(document_id, data, user): ) if saved: await sio.emit( - 'events:note', + 'events', { - 'id': note_id, - 'page': get_page(saved.data, page['id']), - 'updated_at': saved.updated_at, + 'data': { + 'type': 'note:updated', + 'data': { + 'id': note_id, + 'page': get_page(saved.data, page['id']), + 'updated_at': saved.updated_at, + }, + } }, to=f'note:{note_id}', ) diff --git a/backend/open_webui/tools/builtin.py b/backend/open_webui/tools/builtin.py index 63917ee8c5..21437a5b91 100644 --- a/backend/open_webui/tools/builtin.py +++ b/backend/open_webui/tools/builtin.py @@ -94,7 +94,7 @@ async def _has_write_access_to_note(note, user_id: str) -> bool: async def _emit_note_updated(request: Request, user: dict, note) -> None: - await sio.emit('events:note', note.model_dump(), to=f'note:{note.id}') + await sio.emit('events', {'data': {'type': 'note:updated', 'data': note.model_dump()}}, to=f'note:{note.id}') await publish_event( request, EVENTS.NOTE_UPDATED, diff --git a/src/lib/components/channel/Channel.svelte b/src/lib/components/channel/Channel.svelte index b448a237c3..13d8fc4816 100644 --- a/src/lib/components/channel/Channel.svelte +++ b/src/lib/components/channel/Channel.svelte @@ -51,10 +51,14 @@ if (!enabled || !connection || !channelId) return; let alive = true; let revision = 0; - const receive = (event: { channel: string; session: string | null }) => { - if (event.channel === channelId) { + const receive = (event: { + data?: { type?: string; data?: { channel: string; session: string | null } }; + }) => { + if (event?.data?.type !== 'encryption:available') return; + const data = event.data.data; + if (data?.channel === channelId) { revision++; - invitationSession = event.session; + invitationSession = data.session; } }; const clear = () => { @@ -71,13 +75,13 @@ }) .catch(() => {}); }; - connection.on('encryption:available', receive); + connection.on('events', receive); connection.on('disconnect', clear); connection.on('connect', query); query(); stopInvitation = () => { alive = false; - connection.off('encryption:available', receive); + connection.off('events', receive); connection.off('disconnect', clear); connection.off('connect', query); }; diff --git a/src/lib/components/channel/Messages.svelte b/src/lib/components/channel/Messages.svelte index bbc8aa8890..54f55c6639 100644 --- a/src/lib/components/channel/Messages.svelte +++ b/src/lib/components/channel/Messages.svelte @@ -49,7 +49,9 @@ appReplies = {}; } - const receiveAppChannelReply = (event) => { + const receiveAppChannelReply = (message) => { + if (message?.data?.type !== 'channel:app' || !message.data.data) return; + const event = message.data.data; if (event.reply?.type === 'ack') return; if (event.channel_id !== channel?.id || (event.parent_id ?? null) !== (thread ? id : null)) return; @@ -59,8 +61,8 @@ onMount(() => { const appSocket = $socket; - appSocket?.on('events:app', receiveAppChannelReply); - return () => appSocket?.off('events:app', receiveAppChannelReply); + appSocket?.on('events', receiveAppChannelReply); + return () => appSocket?.off('events', receiveAppChannelReply); }); const loadMoreMessages = async () => { diff --git a/src/lib/components/comments/CommentsPanel.svelte b/src/lib/components/comments/CommentsPanel.svelte index 3b5efdb85f..ccca55cf74 100644 --- a/src/lib/components/comments/CommentsPanel.svelte +++ b/src/lib/components/comments/CommentsPanel.svelte @@ -125,13 +125,18 @@ ); }; const changed = (event: { data?: { type?: string; data?: CommentResource } }) => { - if (event?.data?.type !== 'comments:changed' || !event.data.data) return; - const data = event.data.data; + const { type, data } = event?.data ?? {}; + if (!data) return; if ( data.resource_type !== resource?.resource_type || data.resource_id !== resource?.resource_id ) return; + if (type === 'comments:unavailable') { + fail(new CommentError(403, $i18n.t('Comments are no longer available.'))); + return; + } + if (type !== 'comments:changed') return; clearTimeout(refreshTimer); refreshTimer = setTimeout(refresh, 150); }; @@ -139,13 +144,6 @@ join(); refresh(); }; - const unavailable = (data: CommentResource) => { - if ( - data?.resource_type === resource?.resource_type && - data?.resource_id === resource?.resource_id - ) - fail(new CommentError(403, $i18n.t('Comments are no longer available.'))); - }; const revealInitialPosition = () => { if (!alive || loading || !revealPending || !commentList) return; if (commentList.querySelector('[aria-busy="true"]')) return; @@ -211,11 +209,9 @@ connection?.emit('comments:leave', { subscription_id: subscriptionId }); connection?.off('events', changed); connection?.off('connect', reconnect); - connection?.off('comments:unavailable', unavailable); connection = $socket; connection?.on('events', changed); connection?.on('connect', reconnect); - connection?.on('comments:unavailable', unavailable); join(); } onDestroy(() => { @@ -224,7 +220,6 @@ connection?.emit('comments:leave', { subscription_id: subscriptionId }); connection?.off('events', changed); connection?.off('connect', reconnect); - connection?.off('comments:unavailable', unavailable); }); diff --git a/src/lib/components/notes/NoteEditor.svelte b/src/lib/components/notes/NoteEditor.svelte index cf54fc012d..00aca393d8 100644 --- a/src/lib/components/notes/NoteEditor.svelte +++ b/src/lib/components/notes/NoteEditor.svelte @@ -438,10 +438,8 @@ note.access_grants = []; } - $socket?.off('events:note', noteEventHandler); - $socket?.on('events:note', noteEventHandler); - $socket?.off('note:presence', presenceEventHandler); - $socket?.on('note:presence', presenceEventHandler); + $socket?.off('events', noteEventHandler); + $socket?.on('events', noteEventHandler); $socket?.off('disconnect', clearPresence); $socket?.on('disconnect', clearPresence); $socket?.off('connect', joinNote); @@ -1000,7 +998,15 @@ ${content} inputElement?.insertContent(content); }; - const noteEventHandler = async (_note) => { + const noteEventHandler = async (event) => { + const { type, data } = event?.data ?? {}; + if (!data) return; + if (type === 'note:presence') { + presenceEventHandler(data); + return; + } + if (type !== 'note:updated') return; + const _note = data; if (_note.id !== id || !note) return; if (_note.page) { const remote = _note.page; @@ -1053,8 +1059,7 @@ ${content} loadGeneration++; if (note?.id) $socket?.emit('note:leave', { note_id: note.id }); if (debounceTimeout) clearTimeout(debounceTimeout); - $socket?.off('events:note', noteEventHandler); - $socket?.off('note:presence', presenceEventHandler); + $socket?.off('events', noteEventHandler); $socket?.off('disconnect', clearPresence); $socket?.off('connect', joinNote);