43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
const { z } = require("zod")
|
|
|
|
const sendSchema = z.object({
|
|
forum: z.string(),
|
|
text: z.string(),
|
|
})
|
|
.strict()
|
|
|
|
const getSchema = z.object({
|
|
forum: z.string(),
|
|
number: z.number()
|
|
})
|
|
.strict()
|
|
|
|
|
|
export default class ForumHandler {
|
|
static handleSend(msg, ws) {
|
|
try {
|
|
global.db.posts.add(msg.text, msg.forum, ws.userEmail)
|
|
global.Socket.broadcast({event: "new-message", app: "FORUM", forum: msg.forum, msg: this.handleGet({forum: msg.forum, number: 100})})
|
|
return {success: true}
|
|
} catch(e) {
|
|
console.error(e)
|
|
}
|
|
}
|
|
|
|
static handleGet(msg) {
|
|
let data = global.db.posts.get(msg.forum, msg.number)
|
|
return data
|
|
}
|
|
|
|
static handle(operation, msg, ws) {
|
|
switch(operation) {
|
|
case "SEND":
|
|
if(!sendSchema.safeParse(msg).success) throw new Error("Incorrectly formatted Forum ws message!")
|
|
return this.handleSend(msg, ws)
|
|
case "GET":
|
|
if(!getSchema.safeParse(msg).success) throw new Error("Incorrectly formatted Forum ws message!")
|
|
return this.handleGet(msg)
|
|
}
|
|
|
|
}
|
|
} |