Files
open-webui/src/lib/components/layout/InboxHost.svelte
T
Classic298 b7a7ddff18 perf: fewer requests when opening the inbox or clicking a notification (#35068)
Opening the notification inbox fetched the badge count again right away, although opening the inbox already returns it. Clicking a notification fetched the badge count and the list after the click and a second time from the server's realtime notice that notifications changed. Both duplicates are gone, and the badge and the list still update from that notice as before, which reaches all of the user's connected tabs and so carries changes from other tabs and other users. Each tab still refetches once in reaction to the notice its own action caused, which is left for a separate change. Times are summed server time per action for a typical user on a test instance with 50 users and 22,600 notifications, at two chat counts because each request checks which of the user's chats they can see.

<table>
<tr><th></th><th>Requests</th><th>SQLite, 5,000 chats on the instance</th><th>SQLite, 25,000 chats on the instance</th><th>Postgres, 5,000 chats on the instance</th><th>Postgres, 25,000 chats on the instance</th></tr>
<tr><td>Open the inbox</td><td>4 → 3</td><td>102 → 85 ms</td><td>264 → 221 ms</td><td>142 → 118 ms</td><td>227 → 188 ms</td></tr>
<tr><td>Click a notification that links to an item</td><td>5 → 3</td><td>94 → 54 ms</td><td>227 → 137 ms</td><td>136 → 79 ms</td><td>216 → 128 ms</td></tr>
<tr><td>Click an announcement</td><td>4 → 3</td><td>77 → 54 ms</td><td>184 → 137 ms</td><td>112 → 79 ms</td><td>177 → 128 ms</td></tr>
</table>

Refs #35066
2026-11-26 01:02:36 +04:00

95 lines
3.7 KiB
Svelte

<script lang="ts">
import { onMount, onDestroy } from 'svelte';
import { goto, replaceState } from '$app/navigation';
import { page } from '$app/stores';
import { mobile, showTabs, showSidebar, socket, onSocketRestored, user } from '$lib/stores';
import { commentLink } from '$lib/utils';
import { refreshInboxCount, setInboxCount } from '$lib/stores/inbox';
import { inboxRequest, type InboxItem } from '$lib/apis/notifications/inbox';
import InboxPanel from './InboxPanel.svelte';
import Comments from '$lib/components/comments/Comments.svelte';
import type { CommentsRequest } from '$lib/components/comments/CommentsPanel.svelte';
export let panel: InboxPanel | undefined = undefined;
let comments: CommentsRequest | null = null;
let alive = true;
let timer: ReturnType<typeof setTimeout>;
let connection: import('socket.io-client').Socket | null = null;
const refresh = async () => {
if (!alive) return;
await refreshInboxCount();
if (alive) panel?.refresh();
};
const changed = () => {
clearTimeout(timer);
timer = setTimeout(refresh, 150);
};
const eventHandler = (event: { data?: { type?: string } }) => {
if (event?.data?.type === 'notifications:changed') changed();
};
$: if ($socket !== connection) {
connection?.off('events', eventHandler);
connection = $socket;
connection?.on('events', eventHandler);
}
export const close = () => {
if (!$page.state.inbox) return;
replaceState('', { ...$page.state, inbox: false, inboxUserId: undefined });
};
export const select = async (item: InboxItem) => {
const current = await inboxRequest<InboxItem>(`/${encodeURIComponent(item.id)}`, 'PATCH');
if (!alive) return;
if (!current.resource_type) return;
const state = { inbox: $page.state.inbox, inboxUserId: $page.state.inboxUserId };
if ($mobile) showSidebar.set(false);
if (current.object_type === 'comment' && current.resource_type === 'chat') {
await goto(
commentLink({
id: current.object_id!,
resource_type: 'chat',
resource_id: current.resource_id!,
target_type: current.data.target_type ?? null,
target_id: current.data.target_id ?? null
}),
{ state }
);
showTabs.set({ tab: 'comments', comment_id: current.object_id! });
} else if (current.object_type === 'comment') {
comments = {
resource_type: current.resource_type as CommentsRequest['resource_type'],
resource_id: current.resource_id!,
comment_id: current.object_id!
};
} else if (current.object_type === 'chat_message' && current.resource_type === 'chat') {
const query = new URLSearchParams({ message: current.data.message_id! });
await goto(`/c/${encodeURIComponent(current.resource_id!)}?${query}`, { state });
} else if (current.resource_type === 'channel') {
const query = new URLSearchParams({
message: current.object_id!,
thread: current.data.parent_id ?? current.object_id!
});
await goto(`/channels/${encodeURIComponent(current.resource_id!)}?${query}`, { state });
} else if (current.resource_type === 'automation')
await goto(
`/automations/${encodeURIComponent(current.resource_id!)}?run=${encodeURIComponent(current.object_id!)}`,
{ state }
);
else if (current.resource_type === 'calendar')
await goto(`/calendar?event=${encodeURIComponent(current.object_id!)}`, { state });
};
onMount(() => {
if ($page.state.inbox && $page.state.inboxUserId !== $user?.id)
replaceState('', { ...$page.state, inbox: false, inboxUserId: undefined });
void refresh();
return onSocketRestored(changed);
});
onDestroy(() => {
alive = false;
clearTimeout(timer);
connection?.off('events', eventHandler);
setInboxCount(0);
});
</script>
<svelte:window on:focus={changed} />
<Comments modal bind:view={comments} />