2022-09-17 20:25:19 +03:00
|
|
|
const mongoose = require("mongoose");
|
|
|
|
const cache = require("./cache");
|
|
|
|
const { limits } = require("../config.json");
|
|
|
|
|
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-09-17 20:25:19 +03:00
|
|
|
content: { type: String, maxlength: limits.message },
|
2022-09-21 23:06:14 +03:00
|
|
|
oldContents: [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: {
|
2022-09-23 23:10:13 +03:00
|
|
|
like: [String],
|
|
|
|
dislike: [String]
|
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 })
|
2022-09-17 00:27:38 +03:00
|
|
|
return await message.get_author();
|
2022-08-31 14:44:28 +03:00
|
|
|
};
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-08-31 14:44:28 +03:00
|
|
|
module.exports = model;
|