database basically working

This commit is contained in:
metacryst
2026-01-11 03:47:36 -06:00
parent 463dd183f5
commit 13cdff7a5f
19 changed files with 389 additions and 210 deletions

View File

@@ -0,0 +1,66 @@
import { z } from 'zod';
export default class Conversation {
indices = null
constructor(indices) {
this.indices = indices
}
schema = z.object({
id: z.number(),
between: z.array(z.string()),
lastUpdated: z.string()
}).strict()
save(convo) {
let id = `CONVERSATION-${convo.id}`
let result = this.schema.safeParse(convo)
if(result.success) {
try {
super.add(id, convo)
} catch(e) {
console.error(e)
throw e
}
} else {
console.error(result.error)
throw new global.ServerError(400, "Invalid Conversation Data!");
}
}
get(convoID) {
console.log("convo getting, ", convoID)
return this.entries[this.ids[convoID]]
}
getByMember(userID) {
let convos = []
function populateMemberProfilesFromIDs(ids) {
let result = []
for(let i=0; i<ids.length; i++) {
result[i] = global.db.members.get(ids[i])
}
return result
}
for(let i=0; i<this.entries.length; i++) {
let convo = this.entries[i]
console.log(convo, userID)
if(convo.between.includes(userID)) {
console.log("found user convo: ", convo.id)
let messages = global.db.messages.getByConversation(`CONVERSATION-${convo.id}`)
let result = {
...convo,
messages,
}
result.between = populateMemberProfilesFromIDs(convo.between)
convos.push(result)
}
}
return convos
}
}

61
server/db/model/dms/dm.js Normal file
View File

@@ -0,0 +1,61 @@
import { z } from 'zod';
export default class Message {
indices = null
constructor(indices) {
this.indices = indices
}
schema = z.object({
id: z.number(),
conversation: z.string(),
from: z.string(),
text: z.string(),
time: z.string()
}).strict()
save(msg) {
let id = `DM-${msg.id}`
let result = this.schema.safeParse(msg)
if(result.success) {
try {
super.add(id, msg)
} catch(e) {
console.error(e)
throw e
}
} else {
console.error(result.error)
throw new global.ServerError(400, "Invalid Conversation Data!");
}
}
add(convo, text, userID) {
let newMessage = {}
newMessage.time = global.currentTime()
newMessage.from = userID
newMessage.conversation = convo
newMessage.text = text
newMessage.id = this.entries.length+1
console.log(newMessage)
this.save(newMessage)
}
getByConversation(convoID) {
let result = []
for(let i=0; i<this.entries.length; i++) {
let entry = this.entries[i]
if(entry.conversation = convoID) {
let userID = entry.from
let fromUser = global.db.members.get(userID)
let newObj = {
...entry
}
newObj.from = fromUser
result.push(newObj)
}
}
return result
}
}