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) {
|
2022-04-06 22:21:50 +03:00
|
|
|
try {
|
|
|
|
this.id = Number(id);
|
|
|
|
|
|
|
|
const message = await MessageModel.findOne({ id });
|
|
|
|
if (!message) return null;
|
|
|
|
|
|
|
|
const { content, authorID, author = null, threadID = null, time = Date.now(), deleted = false, edited = false, react = {} } = message;
|
|
|
|
this.content = content;
|
|
|
|
this.threadID = threadID;
|
|
|
|
this.author = author;
|
|
|
|
this.authorID = authorID;
|
|
|
|
this.time = time;
|
|
|
|
this.deleted = deleted;
|
|
|
|
this.edited = edited;
|
|
|
|
this.react = react;
|
|
|
|
return this;
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
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
|
|
|
}
|