akf-forum/classes/thread.js

53 lines
1.1 KiB
JavaScript
Raw Normal View History

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