akf-forum/classes/message.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-04-06 21:14:46 +03:00
const { MessageModel } = require("../models");
const User = require("./user");
2022-03-13 16:16:46 +03:00
module.exports = class Message {
2022-04-06 21:14:46 +03:00
constructor(content, author = User, threadID = null, time = Date.now(), deleted = false, edited = false, react = {}) {
2022-03-13 16:16:46 +03:00
2022-04-06 21:14:46 +03:00
this.authorID = author?.id;
2022-03-13 16:16:46 +03:00
this.content = content;
this.author = author;
this.time = time;
2022-04-06 21:14:46 +03:00
this.threadID = threadID;
2022-03-13 16:16:46 +03:00
this.deleted = deleted;
this.edited = edited;
this.react = react;
2022-04-06 21:14:46 +03:00
2022-03-13 16:16:46 +03:00
}
2022-04-06 21:14:46 +03:00
async getById(id = this.id) {
this.id = Number(id);
const message = await MessageModel.findOne({ id });
2022-03-13 16:16:46 +03:00
if (!message) return null;
2022-04-06 21:14:46 +03:00
const { content, authorID, author = null, threadID = null, time = Date.now(), deleted = false, edited = false, react = {} } = message;
2022-03-13 16:16:46 +03:00
this.content = content;
2022-04-06 21:14:46 +03:00
this.threadID = threadID;
2022-03-13 16:16:46 +03:00
this.author = author;
2022-04-06 21:14:46 +03:00
this.authorID = authorID;
2022-03-13 16:16:46 +03:00
this.time = time;
this.deleted = deleted;
this.edited = edited;
this.react = react;
2022-04-06 21:14:46 +03:00
return this;
2022-03-13 16:16:46 +03:00
}
2022-04-06 21:14:46 +03:00
async takeId() {
this.id = await MessageModel.count({}) || 0;
2022-03-13 16:16:46 +03:00
return this;
}
2022-04-06 21:14:46 +03:00
async write(id = this.id) {
const writing = await MessageModel.findOneAndUpdate({ id }, this);
if (!writing)
await MessageModel.create(this);
2022-03-13 16:16:46 +03:00
return this;
}
2022-04-06 21:14:46 +03:00
2022-03-13 16:16:46 +03:00
getLink(id = this.id) {
return "/messages/" + id;
}
2022-02-26 21:26:27 +03:00
}