fix: chat in firebase (#386)

This commit is contained in:
Vojtech Simetka 2023-04-04 17:45:01 +02:00 committed by GitHub
parent 7dd6c59b4a
commit 10137cf1e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 15 deletions

View File

@ -1,5 +1,5 @@
import { connectWallet } from '$lib/services'
import { chats, type Chat, type Message } from '$lib/stores/chat'
import { chats, type Chat, type Message, type DraftChat } from '$lib/stores/chat'
import { personas, type DraftPersona, type Persona } from '$lib/stores/persona'
import { profile } from '$lib/stores/profile'
import { getFromLocalStorage, saveToLocalStorage } from '$lib/utils'
@ -486,20 +486,24 @@ export class Firebase implements Adapter {
})
}
async startChat(chat: Chat): Promise<string> {
async startChat(chat: DraftChat): Promise<string> {
const address = get(profile).address
if (!address) throw new Error('You need to be logged in to start a chat')
if (!chat.post.address) throw new Error('Info about original poster is missing')
if (!chat.post.postId) throw new Error('PostId is missing')
if (!chat.persona.personaId) throw new Error('PersonaId is missing')
if (chat.messages.length === 0) throw new Error('No messages to start a chat')
const dbChat = {
users: [address, chat.post.address],
post: chat.post,
post: {
postId: chat.post.postId,
address: chat.post.address,
images: chat.post.images,
timestamp: chat.post.timestamp,
text: chat.post.text,
} as Post,
personaId: chat.persona.personaId,
messages: chat.messages,
}
const chatCollection = collection(db, `/chats`)
const chatDoc = await addDoc(chatCollection, dbChat)

View File

@ -68,11 +68,8 @@
async function sendMessage(text: string) {
if (!draftChat) return
const chat = {
...draftChat,
messages: [{ timestamp: Date.now(), text, address: $profile.address }],
}
const chatId = await adapter.startChat(chat)
const chatId = await adapter.startChat(draftChat)
await adapter.sendChatMessage(chatId, text)
goto(ROUTES.CHAT(chatId))
}

View File

@ -38,11 +38,7 @@
async function sendMessage(text: string) {
if (!draftChat) return
const chat = {
...draftChat,
messages: [],
}
const chatId = await adapter.startChat(chat)
const chatId = await adapter.startChat(draftChat)
await adapter.sendChatMessage(chatId, text)
goto(ROUTES.CHAT(chatId))
}