This commit is contained in:
Timothy Jaeryang Baek committed 2026-10-16 01:36:10 +04:00
1 parent 4a70253bc8
commit 4dedaaf6e2
7 files changed
+259 -42

No files matched your search

+27 -8
View File
@@ -1303,6 +1303,8 @@
};
let joinedChatId = '';
let liveParticipants: { id: string; name: string }[] = [];
const clearPresence = () => (liveParticipants = []);
let chatRefresh: { id: string; promise: Promise<void> } | null = null;
let typingUsers: { id: string; name: string }[] = [];
let typingUsersTimeout: Record<string, ReturnType<typeof setTimeout>> = {};
@@ -1388,6 +1390,7 @@
mergeChatMessages(fresh.chat.history, baseline);
} catch (error) {
if ($chatId === id && joinedChatId === id) {
clearPresence();
chat = null;
history = { messages: {}, currentId: null };
if (!embedded) await goto('/');
@@ -1401,20 +1404,32 @@
};
const syncChatRoom = (socket: typeof $socket, id: string, temporary: boolean) => {
const next = socket && id && !temporary && !isTemporaryChatId(id) ? id : '';
const next = socket?.connected && id && !temporary && !isTemporaryChatId(id) ? id : '';
if (next === joinedChatId) return;
clearTyping();
if (joinedChatId)
socket?.emit('events:chat', { chat_id: joinedChatId, data: { type: 'leave' } });
clearPresence();
if (joinedChatId && socket?.connected)
socket.emit('events:chat', { chat_id: joinedChatId, data: { type: 'leave' } });
joinedChatId = next;
if (next)
socket?.emit('events:chat', { chat_id: next, data: { type: 'join' } }, () => {
if ($chatId === next) void refreshChat();
const connectionId = socket?.id;
if (next && socket)
socket.emit('events:chat', { chat_id: next, data: { type: 'join' } }, (accepted: boolean) => {
if (!socket.connected || socket.id !== connectionId || $chatId !== next) return;
if (accepted) void refreshChat();
else clearPresence();
});
};
$: syncChatRoom($socket, $chatId, $temporaryChatEnabled);
const chatEventHandler = async (event, cb) => {
if (event?.data?.type === 'chat:presence') {
if (!$socket?.connected || event.chat_id !== $chatId || event.chat_id !== joinedChatId)
return;
liveParticipants = (event.data.data?.users ?? []).filter(
(person: { id: string; name: string }) => person.id !== $user?.id
);
return;
}
console.log(event);
if (event?.data?.type === 'voice:play_animation') {
cb?.(
@@ -1850,6 +1865,7 @@
$socket?.on('events', chatEventHandler);
$socket?.on('connect', handleSocketConnect);
$socket?.on('disconnect', clearTyping);
$socket?.on('disconnect', clearPresence);
$socket?.on('disconnect', cancelBrowserInteractions);
$audioQueue?.destroy();
@@ -1947,8 +1963,9 @@
// to the admin panel), otherwise the previously-viewed chat stays selected
// in the sidebar and deleting/archiving it wrongly navigates away.
clearTyping();
if (joinedChatId)
$socket?.emit('events:chat', { chat_id: joinedChatId, data: { type: 'leave' } });
clearPresence();
if (joinedChatId && $socket?.connected)
$socket.emit('events:chat', { chat_id: joinedChatId, data: { type: 'leave' } });
joinedChatId = '';
chatId.set('');
chatTitle.set('');
@@ -1957,6 +1974,7 @@
$socket?.off('events', chatEventHandler);
$socket?.off('connect', handleSocketConnect);
$socket?.off('disconnect', clearTyping);
$socket?.off('disconnect', clearPresence);
$socket?.off('disconnect', cancelBrowserInteractions);
dismissContextCompactionToast();
audioQueueInstance?.destroy();
@@ -4990,6 +5008,7 @@
</div>
{:else}
<Navbar
{liveParticipants}
bind:this={navbarElement}
readOnly={isShared}
chat={{
+14 -2
View File
@@ -36,11 +36,13 @@
import ChatCheck from '../icons/ChatCheck.svelte';
import Knobs from '../icons/Knobs.svelte';
import { isTemporaryChatId } from '$lib/utils/chatId';
import ChatParticipants from '$lib/components/common/ChatParticipants.svelte';
const i18n: any = getContext('i18n');
export let initNewChat: Function;
export let readOnly: boolean = false;
export let liveParticipants: { id: string; name: string }[] = [];
export let shareEnabled: boolean = false;
export let scrollTop = 0;
export let scrollToTop: (() => void) | null = null;
@@ -113,7 +115,9 @@
{/if}
<div
class="flex-1 overflow-hidden max-w-full mt-0.5 py-0.5 pl-1 {$showSidebar ? 'ml-1' : ''}"
class="flex-1 min-w-0 overflow-hidden max-w-full mt-0.5 py-0.5 pl-1 {$showSidebar
? 'ml-1'
: ''}"
>
{#if chat?.id}
<div class="flex max-w-full min-w-0 items-center gap-2 mr-2">
@@ -174,7 +178,7 @@
{/if}
</div>
<div class="lg:mr-1 flex flex-none items-center gap-2 self-center">
<div class="lg:mr-1 flex flex-none items-center gap-4 self-center">
<!-- <div class="md:hidden flex self-center w-[0.0625rem] h-5 mx-2 bg-gray-300 dark:bg-stone-700" /> -->
{#if $user?.role === 'user' ? ($user?.permissions?.chat?.temporary ?? true) && !($user?.permissions?.chat?.temporary_enforced ?? false) : true}
@@ -243,6 +247,14 @@
</Tooltip>
{/if}
{#if liveParticipants.length > 0}
<ChatParticipants
participants={liveParticipants}
mode="presence"
avatarClass="size-5"
/>
{/if}
{#if $user?.role === 'admin' || ($user?.permissions.chat?.controls ?? true)}
<Tooltip content={$i18n.t('Controls')}>
<button
@@ -12,10 +12,15 @@
export let avatarClass = 'size-4';
export let activeProfileId: string | null = null;
export let overflowOpen = false;
export let mode: 'contributors' | 'presence' = 'contributors';
// Preserve the base overflow label alongside its generated plural forms.
// $i18n.t('Show all {{count}} contributors');
$: people = participants.length ? participants : owner?.id ? [owner] : [];
$: people = participants.length
? participants
: mode === 'contributors' && owner?.id
? [owner]
: [];
$: if (activeProfileId && !people.some((person) => person.id === activeProfileId)) {
activeProfileId = null;
}
@@ -47,6 +52,8 @@
{#each people.slice(0, 3) as person (person.id)}
<div class="relative rounded-full hover:z-10 focus-within:z-10">
<ProfilePreview
side={mode === 'presence' ? 'bottom' : 'right'}
align={mode === 'presence' ? 'end' : 'center'}
user={{ id: person.id, name: person.name || $i18n.t('Unknown User') }}
bind:openPreview={
() => !overflowOpen && activeProfileId === person.id,
@@ -55,7 +62,11 @@
>
{@render avatar(
person,
`${avatarClass} bg-white ring-1 ring-white dark:bg-gray-900 dark:ring-gray-900`
`${avatarClass} bg-white dark:bg-gray-900 ${
mode === 'presence'
? `ring-2 ${['ring-red-400', 'ring-sky-400', 'ring-lime-500', 'ring-amber-400', 'ring-violet-400'][person.id.charCodeAt(0) % 5]}`
: 'ring-1 ring-white dark:ring-gray-900'
}`
)}
</ProfilePreview>
</div>
@@ -65,8 +76,12 @@
{#if people.length > 3}
<Popover.Root bind:open={overflowOpen} onOpenChange={() => (activeProfileId = null)}>
<Popover.Trigger
class="shrink-0 rounded px-0.5 text-[0.625rem] text-gray-500 hover:bg-gray-100 focus-visible:outline dark:text-gray-400 dark:hover:bg-gray-800"
aria-label={$i18n.t('Show all {{count}} contributors', { count: people.length })}
class="shrink-0 rounded px-0.5 {mode === 'presence'
? 'h-6 text-xs'
: 'text-[0.625rem]'} text-gray-500 hover:bg-gray-100 focus-visible:outline dark:text-gray-400 dark:hover:bg-gray-800"
aria-label={mode === 'presence'
? $i18n.t('People here')
: $i18n.t('Show all {{count}} contributors', { count: people.length })}
>
+{people.length - 3}
</Popover.Trigger>
@@ -75,15 +90,22 @@
role="dialog"
trapFocus={false}
side="bottom"
align="start"
align={mode === 'presence' ? 'end' : 'start'}
sideOffset={6}
class="z-[9999] w-60 max-w-[calc(100vw-1.5rem)] max-h-80 overflow-y-auto rounded-xl bg-white p-2 text-gray-900 shadow-lg ring-1 ring-black/5 dark:bg-gray-850 dark:text-gray-100 dark:ring-white/10"
aria-label={$i18n.t('Contributors')}
aria-label={mode === 'presence' ? $i18n.t('People here') : $i18n.t('Contributors')}
>
{#if mode === 'presence'}
<div class="px-2 py-1 text-xs text-gray-500 dark:text-gray-400">
{$i18n.t('People here')}
</div>
{/if}
<ul class="space-y-1">
{#each people as person (person.id)}
<li class="rounded-lg p-1 hover:bg-gray-50 dark:hover:bg-gray-800">
<ProfilePreview
side={mode === 'presence' ? 'bottom' : 'right'}
align={mode === 'presence' ? 'end' : 'center'}
user={{ id: person.id, name: person.name || $i18n.t('Unknown User') }}
bind:openPreview={
() => overflowOpen && activeProfileId === person.id,
+77 -4
View File
@@ -29,6 +29,7 @@ export type State = {
status: 'joining' | 'active' | 'ended';
messages: Message[];
members: Member[];
typing: string[];
self: string;
host: string;
};
@@ -73,8 +74,22 @@ export function request(socket: Socket, action: string, data: object): Promise<a
}
export class Conversation {
state = writable<State>({ status: 'joining', messages: [], members: [], self: '', host: '' });
private current: State = { status: 'joining', messages: [], members: [], self: '', host: '' };
state = writable<State>({
status: 'joining',
messages: [],
members: [],
typing: [],
self: '',
host: ''
});
private current: State = {
status: 'joining',
messages: [],
members: [],
typing: [],
self: '',
host: ''
};
private worker: Worker;
private pending = new Map<
number,
@@ -89,6 +104,8 @@ export class Conversation {
private timer?: ReturnType<typeof setInterval>;
private joinTimer?: ReturnType<typeof setTimeout>;
private messageIds = new Set<string>();
private lastTyping?: number;
private typingTimers = new Map<string, ReturnType<typeof setTimeout>>();
private disposers: (() => void)[] = [];
constructor(
@@ -232,6 +249,9 @@ export class Conversation {
const members: Member[] = JSON.parse(await this.crypto('members'));
if (this.stopped) return;
const previous = new Set(this.current.members.map((m) => m.identity.participation));
for (const id of this.current.typing) {
if (!members.some((m) => m.identity.participation === id)) this.removeTyping(id);
}
this.update({
members: members.map((m) => ({
...m,
@@ -280,6 +300,7 @@ export class Conversation {
await this.crypto('join', event.packet, bindings);
await this.activate();
} else if (event.kind === 'departed') {
this.removeTyping(event.participation);
if (event.admitted && this.participation === event.host) {
const packet = await this.crypto('remove', event.participation, bindings);
await this.send('packet', { kind: 'mls', packet });
@@ -319,7 +340,24 @@ export class Conversation {
!/^[a-f0-9-]{36}$/.test(data.id)
)
throw new Error();
if (data.type === 'receipt') {
if (data.type === 'typing') {
if (typeof data.typing !== 'boolean') throw new Error();
const id = sender.participation;
if (
id === this.participation ||
!this.current.members.some((m) => m.identity.participation === id)
)
return;
if (data.typing) {
clearTimeout(this.typingTimers.get(id));
this.typingTimers.set(
id,
setTimeout(() => this.removeTyping(id), 5000)
);
if (!this.current.typing.includes(id))
this.update({ typing: [...this.current.typing, id] });
} else this.removeTyping(id);
} else if (data.type === 'receipt') {
const messages = this.current.messages.map((m) =>
m.id === data.id &&
m.sender === this.participation &&
@@ -336,6 +374,7 @@ export class Conversation {
)
throw new Error();
this.messageIds.add(data.id);
this.removeTyping(sender.participation);
// MLS rejects replay; the bounded ID set additionally rejects repeated app envelopes.
if (this.messageIds.size > 1000)
this.messageIds.delete(this.messageIds.values().next().value!);
@@ -359,6 +398,36 @@ export class Conversation {
await this.send('packet', { kind: 'mls', packet });
} else throw new Error();
}
private removeTyping(id: string) {
clearTimeout(this.typingTimers.get(id));
this.typingTimers.delete(id);
if (this.current.typing.includes(id))
this.update({ typing: this.current.typing.filter((participant) => participant !== id) });
}
sendTyping(typing: boolean) {
if (this.current.status !== 'active') return;
const now = Date.now();
if (
typing
? this.lastTyping !== undefined && now - this.lastTyping < 2000
: this.lastTyping === undefined
)
return;
this.lastTyping = typing ? now : undefined;
return this.enqueue(async () => {
const packet = await this.crypto(
'encrypt',
JSON.stringify({
type: 'typing',
id: crypto.randomUUID(),
typing,
channel: this.channel,
session: this.view!.session
})
);
await this.send('packet', { kind: 'mls', packet });
});
}
sendText(text: string) {
if (
this.current.status !== 'active' ||
@@ -366,6 +435,7 @@ export class Conversation {
new TextEncoder().encode(text).length > 16384
)
return;
this.sendTyping(false);
return this.enqueue(async () => {
const id = crypto.randomUUID();
const expected = this.current.members
@@ -407,6 +477,9 @@ export class Conversation {
}
this.pending.clear();
this.messageIds.clear();
for (const timer of this.typingTimers.values()) clearTimeout(timer);
this.typingTimers.clear();
this.lastTyping = undefined;
if (this.view && this.participation && connected(this.socket)) {
void request(this.socket, 'leave', {
channel: this.channel,
@@ -415,7 +488,7 @@ export class Conversation {
}).catch(() => {});
}
this.view = undefined;
this.current = { status: 'ended', messages: [], members: [], self: '', host: '' };
this.current = { status: 'ended', messages: [], members: [], typing: [], self: '', host: '' };
this.state.set(this.current);
}
}
@@ -1937,6 +1937,7 @@
"pdf, docx, pptx, xlsx": "",
"Peak tokens": "",
"pending": "",
"People here": "People here",
"Pending": "",
"Per automation": "",
"Per chat": "",
@@ -11,6 +11,7 @@
import RichTextInput from '$lib/components/common/RichTextInput.svelte';
import type { Editor } from '@tiptap/core';
import Markdown from '$lib/components/chat/Messages/Markdown.svelte';
import Skeleton from '$lib/components/chat/Messages/Skeleton.svelte';
import Modal from '$lib/components/common/Modal.svelte';
import Tooltip from '$lib/components/common/Tooltip.svelte';
import ProfileImage from '$lib/components/chat/Messages/ProfileImage.svelte';
@@ -33,7 +34,10 @@
is_private: true
};
let conversation: Conversation | undefined;
let state: State = { status: 'ended', messages: [], members: [], self: '', host: '' };
let state: State = { status: 'ended', messages: [], members: [], typing: [], self: '', host: '' };
$: typingMembers = state.members.filter((member) =>
state.typing.includes(member.identity.participation)
);
let draft = '';
let inputEditor: Editor | null = null;
let verified = new Set<string>();
@@ -58,7 +62,7 @@
});
function startSession(channel: string) {
stopSession();
state = { status: 'ended', messages: [], members: [], self: '', host: '' };
state = { status: 'ended', messages: [], members: [], typing: [], self: '', host: '' };
const intent = consumeEntry(channel);
const connection = get(socket);
const account = get(user);
@@ -71,15 +75,17 @@
return;
}
const unsubscribe = conversation.state.subscribe((next) => {
const messagesChanged = state.messages !== next.messages;
state = next;
if (next.status === 'ended') {
showParticipants = false;
draft = '';
verified = new Set();
}
void tick().then(() => {
if (list) list.scrollTop = list.scrollHeight;
});
if (messagesChanged)
void tick().then(() => {
if (list) list.scrollTop = list.scrollHeight;
});
});
const logout = user.subscribe((current) => {
if (current?.id !== account.id || !canUseEncryption(current, get(config)?.features)) close();
@@ -220,7 +226,25 @@
{/each}
</div>
</div>
<form class="shrink-0 pb-3 px-3" on:submit|preventDefault={send}>
<form
class="shrink-0 pb-3 px-3"
on:submit|preventDefault={send}
on:focusout={() => conversation?.sendTyping(false)}
>
{#if typingMembers.length > 0}
<div
class="flex items-center gap-1.5 px-1 pb-2 text-xs text-gray-500 dark:text-gray-400"
role="status"
>
<Skeleton size="xs" />
<div>
<span class="font-normal text-black dark:text-white"
>{typingMembers.map((member) => member.name).join(', ')}</span
>
{typingMembers.length === 1 ? $i18n.t('is typing...') : $i18n.t('are typing...')}
</div>
</div>
{/if}
<div
id="message-input-container"
class="flex-1 flex flex-col relative w-full shadow-sm rounded-2xl border border-gray-100/60 dark:border-gray-800/50 hover:border-gray-100 focus-within:border-gray-100 hover:dark:border-gray-800 focus-within:dark:border-gray-800 transition bg-white dark:bg-gray-400/5 dark:text-gray-100"
@@ -248,6 +272,7 @@
floatingMenuPlacement="top-start"
onChange={({ md }) => {
draft = state.status === 'active' ? md : '';
void conversation?.sendTyping(Boolean(draft.trim()));
}}
on:keydown={({ detail: { event } }) => {
if (event.isComposing || event.defaultPrevented || event.key !== 'Enter') return;