From d361ac2db36caef9ba296291ecc120a716d0a31d Mon Sep 17 00:00:00 2001 From: Shibo Lyu Date: Wed, 11 Sep 2024 03:18:42 +0800 Subject: [PATCH] fix: show username in chat history --- src/lib/chat.ts | 11 ++++++++++- src/lib/types/message.ts | 4 ++-- .../(app)/chats/[server]/[chatId]/ChatHistory.svelte | 3 +++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lib/chat.ts b/src/lib/chat.ts index 9528698..aeb8d1d 100644 --- a/src/lib/chat.ts +++ b/src/lib/chat.ts @@ -2,6 +2,7 @@ import { derived, readable, type Readable } from 'svelte/store'; import type { BlahChatServerConnection } from './blah/connection/chatServer'; import type { BlahRichText } from './richText'; import { messageFromBlah, type Chat, type Message, type User } from './types'; +import { BlahError } from './blah/connection/error'; const MAX_MESSAGES_PER_SECTION = 10; const SHOW_TIME_AFTER_SILENCE = 30 * 60 * 1000; @@ -79,7 +80,15 @@ export function useChat( }); const sendMessage = async (brt: BlahRichText) => { - await server.sendMessage(chatId, brt); + try { + await server.sendMessage(chatId, brt); + } catch (e) { + console.error(e); + if (e instanceof BlahError && e.statusCode === 403) { + await server.joinRoom(chatId); + await server.sendMessage(chatId, brt); + } + } }; return { info, messages, sectionedMessages, sendMessage }; diff --git a/src/lib/types/message.ts b/src/lib/types/message.ts index ceb35f0..1523daa 100644 --- a/src/lib/types/message.ts +++ b/src/lib/types/message.ts @@ -1,4 +1,4 @@ -import type { BlahSignedPayload } from '$lib/blah/crypto'; +import { generateName, type BlahSignedPayload } from '$lib/blah/crypto'; import type { BlahMessage } from '$lib/blah/structures'; import type { BlahRichText } from '$lib/richText'; @@ -12,7 +12,7 @@ export type Message = { export function messageFromBlah(payload: BlahSignedPayload): Message { return { id: payload.sig, - sender: { id: payload.signee.user, name: payload.signee.user }, + sender: { id: payload.signee.user, name: generateName(payload.signee.user) }, content: payload.signee.payload.rich_text, date: new Date(payload.signee.timestamp * 1000) }; diff --git a/src/routes/(app)/chats/[server]/[chatId]/ChatHistory.svelte b/src/routes/(app)/chats/[server]/[chatId]/ChatHistory.svelte index 7185b13..4aa92b7 100644 --- a/src/routes/(app)/chats/[server]/[chatId]/ChatHistory.svelte +++ b/src/routes/(app)/chats/[server]/[chatId]/ChatHistory.svelte @@ -38,6 +38,9 @@
+ {#if messageSection.sender && !isMyself} +
{messageSection.sender.name}
+ {/if} {#each messageSection.messages as message, idx}