diff --git a/src/lib/blah/connection/chatServer.ts b/src/lib/blah/connection/chatServer.ts index baf3432..ce59f42 100644 --- a/src/lib/blah/connection/chatServer.ts +++ b/src/lib/blah/connection/chatServer.ts @@ -91,12 +91,17 @@ export class BlahChatServerConnection { await this.apiCall('POST', `/room/${room}/item`, payload); } - async fetchRoom( - roomId: string - ): Promise<{ room: BlahRoomInfo; messages: BlahSignedPayload[] }> { - const [room, messages]: [BlahRoomInfo, [number, BlahSignedPayload][]] = - await this.apiCall('GET', `/room/${roomId}/item`); - return { room, messages: messages.toSorted(([a], [b]) => a - b).map(([, message]) => message) }; + async fetchRoomInfo(roomId: string): Promise { + const room: BlahRoomInfo = await this.apiCall('GET', `/room/${roomId}`); + return room; + } + + async fetchRoomHistory(roomId: string): Promise[]> { + const { items }: { items: BlahSignedPayload[] } = await this.apiCall( + 'GET', + `/room/${roomId}/item` + ); + return items; } private createEventSource(roomId: string): EventSource { diff --git a/src/routes/(app)/chats/[chatId]/+page.svelte b/src/routes/(app)/chats/[chatId]/+page.svelte index 03948b8..fc6f94c 100644 --- a/src/routes/(app)/chats/[chatId]/+page.svelte +++ b/src/routes/(app)/chats/[chatId]/+page.svelte @@ -33,14 +33,25 @@ return server; } - async function loadChat(server: BlahChatServerConnection) { - const { room, messages: blahMessages } = await server.fetchRoom(roomId); + async function loadChatInfo(server: BlahChatServerConnection) { + const room = await server.fetchRoomInfo(roomId); chat = { id: roomId, name: room.title, type: 'group' }; - messages = [...blahMessages.map(messageFromBlah), ...messages]; + } + + async function loadChatHistory(server: BlahChatServerConnection) { + const history = await server.fetchRoomHistory(roomId); + messages = [ + ...history.map(messageFromBlah).toSorted((a, b) => a.date.getTime() - b.date.getTime()), + ...messages + ]; + } + + async function loadChat(server: BlahChatServerConnection) { + return await Promise.allSettled([loadChatInfo(server), loadChatHistory(server)]); } $: if (browser) initConnection($currentKeyPair).then((server) => loadChat(server));