akf-forum/classes/message.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-02-26 21:26:27 +03:00
const User = require("./user")
2022-03-13 16:06:25 +03:00
const Thread = require("./thread")
const db = require("quick.db");
2022-02-26 21:26:27 +03:00
module.exports = class Message {
2022-03-13 16:06:25 +03:00
constructor(content, author = new User(), thread = new Thread(), time = new Date().getTime(), deleted = false, edited = false, react = {}) {
2022-02-26 21:26:27 +03:00
this.content = content;
this.author = author;
this.time = time;
2022-03-13 16:06:25 +03:00
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);
if (!message) return null;
2022-02-26 21:26:27 +03:00
2022-03-13 16:06:25 +03:00
this.id = id;
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
}
}