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 { 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 { personas, type DraftPersona, type Persona } from '$lib/stores/persona'
import { profile } from '$lib/stores/profile' import { profile } from '$lib/stores/profile'
import { getFromLocalStorage, saveToLocalStorage } from '$lib/utils' 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 const address = get(profile).address
if (!address) throw new Error('You need to be logged in to start a chat') 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.address) throw new Error('Info about original poster is missing')
if (!chat.post.postId) throw new Error('PostId is missing') if (!chat.post.postId) throw new Error('PostId is missing')
if (!chat.persona.personaId) throw new Error('PersonaId 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 = { const dbChat = {
users: [address, chat.post.address], 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, personaId: chat.persona.personaId,
messages: chat.messages,
} }
const chatCollection = collection(db, `/chats`) const chatCollection = collection(db, `/chats`)
const chatDoc = await addDoc(chatCollection, dbChat) const chatDoc = await addDoc(chatCollection, dbChat)

View File

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

View File

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