2022-08-09 19:16:34 +03:00
|
|
|
const mongoose = require("mongoose")
|
2022-08-31 14:44:28 +03:00
|
|
|
const cache = require("./cache")
|
2022-08-09 19:16:34 +03:00
|
|
|
const schema = new mongoose.Schema({
|
|
|
|
id: { type: String, unique: true },
|
2022-08-31 14:44:28 +03:00
|
|
|
author: Object,
|
2022-08-09 19:16:34 +03:00
|
|
|
threadID: String,
|
2022-08-31 14:44:28 +03:00
|
|
|
authorID: String,
|
2022-04-06 21:14:46 +03:00
|
|
|
content: String,
|
2022-08-09 19:16:34 +03:00
|
|
|
time: { type: Date, default: Date.now },
|
2022-04-06 21:14:46 +03:00
|
|
|
deleted: { type: Boolean, default: false },
|
|
|
|
edited: { type: Boolean, default: false },
|
2022-08-28 21:14:02 +03:00
|
|
|
react: {
|
|
|
|
like: [Number],
|
|
|
|
dislike: [Number]
|
2022-08-29 19:31:59 +03:00
|
|
|
}
|
2022-08-09 19:16:34 +03:00
|
|
|
})
|
|
|
|
|
2022-08-31 14:44:28 +03:00
|
|
|
schema.methods.get_author = cache.getAuthor
|
2022-08-09 19:16:34 +03:00
|
|
|
|
|
|
|
schema.methods.takeId = async function () {
|
|
|
|
this.id = String(await model.count() || 0);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
schema.methods.getLink = function (id = this.id) {
|
|
|
|
return "/messages/" + id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const model = mongoose.model('message', schema);
|
|
|
|
|
2022-08-31 14:44:28 +03:00
|
|
|
model.get = async id => {
|
|
|
|
const message = await model.findOne({ id })
|
|
|
|
return await message.get_author();
|
|
|
|
};
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-08-31 14:44:28 +03:00
|
|
|
module.exports = model;
|