diff --git a/classes/index.js b/classes/index.js new file mode 100644 index 0000000..9e5577f --- /dev/null +++ b/classes/index.js @@ -0,0 +1,5 @@ +const Message = require("./message") +const Thread = require("./thread") +const User = require("./user") + +module.exports = { Message, Thread, User } \ No newline at end of file diff --git a/classes/message.js b/classes/message.js new file mode 100644 index 0000000..843e4fd --- /dev/null +++ b/classes/message.js @@ -0,0 +1,15 @@ + +const User = require("./user") + +module.exports = class Message { + + + constructor(content, author = new User(), time = new Date().getTime()) { + + + this.content = content; + this.author = author; + this.time = time; + + } +} \ No newline at end of file diff --git a/classes/thread.js b/classes/thread.js new file mode 100644 index 0000000..7258cb7 --- /dev/null +++ b/classes/thread.js @@ -0,0 +1,49 @@ +const db = require("quick.db") +const User = require("./user") + +module.exports = class Thread { + + constructor(title, content, author = new User(), messages = [], time = new Date().getTime()) { + + this.content = content; + this.author = author; + this.title = title; + this.messages = messages; + this.time = time; + + } + + getId(id = this.id) { + const thread = db.get("threads."+id); + if (!thread) return null; + this.id = id; + const { content, title, author, messages = [], time = new Date().getTime() } = thread; + this.content = content + this.title = title + this.author = author + this.messages = messages; + this.time = time; + + return this + } + takeId(){ + + + this.id = db.get("threads").length; + return this + } + + push(message){ + this.messages.push(message) + return this; + } + write(id = this.id) { + + db.set("threads."+id, this) + } + getLink(id = this.id) { + return "/threads/" + id; + + + } +} \ No newline at end of file diff --git a/classes/user.js b/classes/user.js new file mode 100644 index 0000000..c5df0bd --- /dev/null +++ b/classes/user.js @@ -0,0 +1,40 @@ +const db = require("quick.db") + +module.exports = class User { + + + constructor(name = "guest", avatar = "/images/guest.png", time = new Date().getTime()) { + + this.name = name; + this.avatar = avatar; + this.time = time; + + } + + getId(id = this.id) { + const user = db.get("users." + id); + if (!user) return null; + this.id = id; + const { name = "guest", avatar = "/images/guest.png", time = new Date().getTime() } = user; + this.name = name; + this.avatar = avatar; + this.time = time; + return this + } + takeId() { + let id = db.get("users"); + this.id = id ? id.length : 0; + return this + } + + write(id = this.id) { + + db.set("users." + id, this) + } + getLink(id = this.id) { + return "/users/" + id; + + + } + +} \ No newline at end of file diff --git a/errors/error.js b/errors/error.js new file mode 100644 index 0000000..dec2142 --- /dev/null +++ b/errors/error.js @@ -0,0 +1,2 @@ +module.exports = (res, type, error) => + res.status(type).render("error", { type, error });