akf-forum/models/Message.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-09-17 20:25:19 +03:00
const mongoose = require("mongoose");
const cache = require("./cache");
const { limits } = require("../config.json");
const schema = new mongoose.Schema({
id: { type: String, unique: true },
2022-10-09 20:58:55 +03:00
authorID: {
type: String, get(v) { return v || this.author?.id }
},
author: {
type: Object, set(v) {
this.authorID = v.id;
return v;
}
},
threadID: 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],
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: [String],
dislike: [String]
2022-08-29 19:31:59 +03:00
}
})
2022-08-31 14:44:28 +03:00
schema.methods.get_author = cache.getAuthor
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;