40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
const { z } = require("zod")
|
|
|
|
const sendSchema = z.object({
|
|
conversation: z.string(),
|
|
text: z.string(),
|
|
})
|
|
.strict()
|
|
|
|
export default class MessagesHandler {
|
|
|
|
static handleSend(msg, ws) {
|
|
let user = global.db.members.getByEmail(ws.userEmail)
|
|
let convo = global.db.conversations.get(msg.conversation)
|
|
if(convo.between.includes(`MEMBER-${user.id}`)) {
|
|
global.db.messages.add(msg.conversation, msg.text, `MEMBER-${user.id}`)
|
|
global.Socket.broadcast({event: "new-message", app: "MESSAGES", msg: {conversationID: convo.id, messages: global.db.messages.getByConversation(`CONVERSATION-${msg.conversation}`)}})
|
|
|
|
} else {
|
|
throw new Error("Can't add to a conversation user is not a part of!")
|
|
}
|
|
return {success: true}
|
|
}
|
|
|
|
static handleGet(ws) {
|
|
let user = global.db.members.getByEmail(ws.userEmail)
|
|
let data = global.db.conversations.getByMember(`MEMBER-${user.id}`)
|
|
return data
|
|
}
|
|
|
|
static handle(operation, msg, ws) {
|
|
switch(operation) {
|
|
case "GET":
|
|
return this.handleGet(ws)
|
|
case "SEND":
|
|
if(!sendSchema.safeParse(msg).success) throw new Error("Incorrectly formatted Forum ws message!")
|
|
return this.handleSend(msg, ws)
|
|
}
|
|
|
|
}
|
|
} |