Download src/lib/messages.ts from HonzaH/Letschat: direct link, hf CLI and curl.
- Browser
- Download file 3.79 kB
-
https://huggingface.co/spaces/HonzaH/Letschat/resolve/main/src/lib/messages.ts
- Command line
-
hf download hf://spaces/HonzaH/Letschat/src/lib/messages.ts
-
curl -L -o messages.ts https://huggingface.co/spaces/HonzaH/Letschat/resolve/main/src/lib/messages.ts
3.79 kB
| export interface MessageWithUser { | |
| id: string | |
| room_id: string | |
| content: string | |
| created_at: string | |
| image_url?: string | null | |
| user_id?: string | null | |
| code_id: string | |
| users?: { | |
| fname?: string | |
| sname?: string | |
| nickname?: string | |
| } | null | |
| } | |
| export interface MessageData { | |
| id: string | |
| room_id: string | |
| content: string | |
| created_at: string | |
| image_url?: string | |
| user_id?: string | |
| code_id: string | |
| sender?: { | |
| fname?: string | |
| sname?: string | |
| nickname?: string | |
| } | |
| } | |
| import { savePendingMessage, getLocalMessages, getPendingMessages, clearPendingMessages, saveLocalMessage } from './offlineStore' | |
| export function mapMessage(row: MessageWithUser): MessageData { | |
| const { users, image_url, user_id, ...rest } = row | |
| return { | |
| ...rest, | |
| image_url: image_url || undefined, | |
| user_id: user_id || undefined, | |
| sender: users || undefined | |
| } | |
| } | |
| export async function sendMessage( | |
| roomId: string, | |
| codeId: string, | |
| content: string, | |
| userId?: string | |
| ): Promise<MessageData> { | |
| try { | |
| const res = await fetch(`/api/messages/${roomId}`, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ content, codeId, userId }) | |
| }) | |
| if (!res.ok) { | |
| throw new Error('Failed to send message') | |
| } | |
| const { message } = await res.json() | |
| return mapMessage(message as MessageWithUser) | |
| } catch (e) { | |
| const pending = await savePendingMessage({ room_id: roomId, code_id: codeId, content, user_id: userId || null }) | |
| if (pending) { | |
| return { | |
| id: pending.id, | |
| room_id: roomId, | |
| code_id: codeId, | |
| content, | |
| created_at: pending.created_at, | |
| user_id: userId || undefined | |
| } | |
| } | |
| throw e | |
| } | |
| } | |
| export async function getMessages(roomId: string, limit: number = 50): Promise<MessageData[]> { | |
| try { | |
| const res = await fetch(`/api/messages/${roomId}?limit=${limit}`, { cache: 'no-store' }) | |
| if (!res.ok) { | |
| throw new Error('Failed to fetch messages') | |
| } | |
| const { messages } = await res.json() | |
| console.debug('[getMessages] fetched', Array.isArray(messages) ? messages.length : typeof messages, 'messages for room', roomId) | |
| // Deduplicate messages by id (preserve order) and save locally for offline use | |
| const seen = new Set<string>() | |
| const unique: MessageWithUser[] = [] | |
| for (const m of messages as MessageWithUser[]) { | |
| if (!m || !m.id) continue | |
| if (!seen.has(m.id)) { | |
| seen.add(m.id) | |
| unique.push(m) | |
| } | |
| } | |
| for (const m of unique) { | |
| await saveLocalMessage({ | |
| room_id: m.room_id, | |
| code_id: m.code_id, | |
| content: m.content, | |
| user_id: m.user_id || null, | |
| image_url: m.image_url || null, | |
| created_at: m.created_at | |
| }) | |
| } | |
| return unique.map(mapMessage) | |
| } catch (e) { | |
| const offline = await getLocalMessages(roomId, limit) | |
| if (offline.length > 0) { | |
| return offline.map((m) => ({ | |
| id: m.id, | |
| room_id: m.room_id, | |
| code_id: m.code_id, | |
| content: m.content, | |
| created_at: m.created_at, | |
| image_url: m.image_url || undefined, | |
| user_id: m.user_id || undefined | |
| })) | |
| } | |
| throw e | |
| } | |
| } | |
| export async function syncPendingMessages() { | |
| const pending = await getPendingMessages() | |
| if (!pending.length) return { synced: 0 } | |
| try { | |
| const res = await fetch('/api/sync/push', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ messages: pending }) | |
| }) | |
| if (!res.ok) throw new Error('sync push failed') | |
| await clearPendingMessages(pending.map((m) => m.id)) | |
| return { synced: pending.length } | |
| } catch (e) { | |
| return { synced: 0, error: (e as Error).message } | |
| } | |
| } | |