2022-08-09 19:16:34 +03:00
|
|
|
const mongoose = require("mongoose")
|
|
|
|
const UserModel = require("./User");
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-08-09 19:16:34 +03:00
|
|
|
const schema = new mongoose.Schema({
|
|
|
|
id: { type: String, unique: true },
|
|
|
|
|
|
|
|
threadID: String,
|
|
|
|
author: UserModel.schema, // user-model
|
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-09 19:16:34 +03:00
|
|
|
react: { type:Object, default: {} }
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
schema.virtual('authorID').get(function() { return this.author?.id; });
|
2022-08-24 22:09:21 +03:00
|
|
|
schema.virtual('reactCount').get(function() {
|
|
|
|
const arr = Object.values(this.react)
|
|
|
|
return arr.filter(Boolean).length - arr.filter(x => !x).length;
|
|
|
|
});
|
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);
|
|
|
|
|
|
|
|
model.get = id => model.findOne({ id });
|
|
|
|
|
|
|
|
module.exports = model;
|
2022-04-06 21:14:46 +03:00
|
|
|
|