akf-forum/classes/message.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-03-13 16:16:46 +03:00
const User = require("./user")
const Thread = require("./thread")
const db = require("quick.db");
module.exports = class Message {
constructor(content, author = new User(), thread = new Thread(), time = new Date().getTime(), deleted = false, edited = false, react = {}) {
this.content = content;
this.author = author;
this.time = time;
this.thread = thread;
this.deleted = deleted;
this.edited = edited;
this.react = react;
}
getId(id = this.id) {
const message = db.get("messages").find(msg => msg.id == id);
2022-03-13 16:16:46 +03:00
if (!message) return null;
this.id = Number(id);
2022-03-13 16:16:46 +03:00
const { content, author, thread = new Thread(), time = new Date().getTime(), deleted = false, edited = false, react = {} } = message;
this.content = content;
this.thread = thread;
this.author = author;
this.time = time;
this.deleted = deleted;
this.edited = edited;
this.react = react;
return this
}
takeId() {
this.id = db.get("messages").length || 0;
return this;
}
write(id = this.id) {
db.set("messages." + id, this)
return this;
}
getLink(id = this.id) {
return "/messages/" + id;
}
2022-02-26 21:26:27 +03:00
}