mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-11-29 22:20:41 +03:00
Akif9748
4be2069587
Co-authored-by: Sapphire1525 <Sapphire1525@users.noreply.github.com> Co-authored-by: inoaa79 <inoaa79@users.noreply.github.com>
38 lines
973 B
JavaScript
38 lines
973 B
JavaScript
const mongoose = require("mongoose")
|
|
const UserModel = require("./User");
|
|
|
|
const schema = new mongoose.Schema({
|
|
id: { type: String, unique: true },
|
|
|
|
threadID: String,
|
|
author: UserModel.schema, // user-model
|
|
|
|
content: String,
|
|
time: { type: Date, default: Date.now },
|
|
deleted: { type: Boolean, default: false },
|
|
edited: { type: Boolean, default: false },
|
|
react: { type:Object, default: {} }
|
|
|
|
})
|
|
|
|
schema.virtual('authorID').get(function() { return this.author?.id; });
|
|
schema.virtual('reactCount').get(function() {
|
|
const arr = Object.values(this.react)
|
|
return arr.filter(Boolean).length - arr.filter(x => !x).length;
|
|
});
|
|
|
|
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);
|
|
|
|
model.get = id => model.findOne({ id });
|
|
|
|
module.exports = model;
|
|
|