From 3ca0faad4f2b7f38c9c6d1404cc9309201bca5b1 Mon Sep 17 00:00:00 2001 From: Rishi Balakrishnan Date: Fri, 25 Sep 2026 14:53:12 -0400 Subject: [PATCH] feat(live): send chat messages with delivery confirmation --- .../streamplace/__tests__/pending.test.ts | 26 ++++++++ src/features/streamplace/pending.ts | 30 +++++++++ src/features/streamplace/useSendChat.ts | 65 +++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 src/features/streamplace/__tests__/pending.test.ts create mode 100644 src/features/streamplace/pending.ts create mode 100644 src/features/streamplace/useSendChat.ts diff --git a/src/features/streamplace/__tests__/pending.test.ts b/src/features/streamplace/__tests__/pending.test.ts new file mode 100644 index 000000000..2417ccc27 --- /dev/null +++ b/src/features/streamplace/__tests__/pending.test.ts @@ -0,0 +1,26 @@ +import {type PendingMessage, reconcilePending} from '../pending' + +const p = (over: Partial): PendingMessage => ({ + localId: '1', + text: 'hi', + sentAt: 0, + status: 'sending', + ...over, +}) + +it('drops pending messages once their uri shows up on the socket', () => { + expect(reconcilePending([p({uri: 'at://me/1'})], new Set(['at://me/1']), 1000, 10_000)).toEqual( + [], + ) +}) + +it('marks sending messages failed after the timeout', () => { + expect(reconcilePending([p({uri: 'at://me/1'})], new Set(), 10_001, 10_000)[0].status).toBe( + 'failed', + ) +}) + +it('leaves recent sends alone', () => { + const list = [p({uri: 'at://me/1'})] + expect(reconcilePending(list, new Set(), 5000, 10_000)).toBe(list) +}) diff --git a/src/features/streamplace/pending.ts b/src/features/streamplace/pending.ts new file mode 100644 index 000000000..e4f4c0f7a --- /dev/null +++ b/src/features/streamplace/pending.ts @@ -0,0 +1,30 @@ +export type PendingMessage = { + localId: string + text: string + sentAt: number + uri?: string + status: 'sending' | 'failed' +} + +export function reconcilePending( + pending: PendingMessage[], + seenUris: ReadonlySet, + now: number, + timeoutMs: number, +): PendingMessage[] { + let changed = false + const next: PendingMessage[] = [] + for (const m of pending) { + if (m.uri && seenUris.has(m.uri)) { + changed = true + continue + } + if (m.status === 'sending' && now - m.sentAt > timeoutMs) { + changed = true + next.push({...m, status: 'failed'}) + continue + } + next.push(m) + } + return changed ? next : pending +} diff --git a/src/features/streamplace/useSendChat.ts b/src/features/streamplace/useSendChat.ts new file mode 100644 index 000000000..567960350 --- /dev/null +++ b/src/features/streamplace/useSendChat.ts @@ -0,0 +1,65 @@ +import {useCallback, useEffect, useMemo, useState} from 'react' + +import {logger} from '#/logger' +import {useAgent} from '#/state/session' +import {type ChatMessage} from './live-state' +import {type PendingMessage, reconcilePending} from './pending' + +const COLLECTION = 'place.stream.chat.message' +export const CHAT_DELIVERY_TIMEOUT_MS = 10_000 + +export function useSendChat(streamerDid: string | undefined, messages: ChatMessage[]) { + const agent = useAgent() + const [pending, setPending] = useState([]) + const seenUris = useMemo(() => new Set(messages.map(m => m.uri)), [messages]) + + useEffect(() => { + setPending(prev => reconcilePending(prev, seenUris, Date.now(), CHAT_DELIVERY_TIMEOUT_MS)) + const id = setInterval( + () => + setPending(prev => reconcilePending(prev, seenUris, Date.now(), CHAT_DELIVERY_TIMEOUT_MS)), + 2000, + ) + return () => clearInterval(id) + }, [seenUris]) + + const send = useCallback( + async (text: string, replacing?: PendingMessage) => { + if (!streamerDid || !agent.session) return + const localId = `${Date.now()}-${Math.random()}` + setPending(prev => [ + ...prev.filter(m => m.localId !== replacing?.localId), + {localId, text, sentAt: Date.now(), status: 'sending'}, + ]) + try { + if (replacing?.uri) { + const rkey = replacing.uri.split('/').pop()! + await agent.com.atproto.repo + .deleteRecord({repo: agent.assertDid, collection: COLLECTION, rkey}) + .catch(() => undefined) + } + const res = await agent.com.atproto.repo.createRecord({ + repo: agent.assertDid, + collection: COLLECTION, + record: { + $type: COLLECTION, + text, + streamer: streamerDid, + createdAt: new Date().toISOString(), + }, + }) + setPending(prev => + prev.map(m => (m.localId === localId ? {...m, uri: res.data.uri} : m)), + ) + } catch (err) { + logger.warn('streamplace: chat send failed', {safeMessage: err}) + setPending(prev => + prev.map(m => (m.localId === localId ? {...m, status: 'failed'} : m)), + ) + } + }, + [agent, streamerDid], + ) + + return {pending, send} +} -- knotmirror 0.0.1