akf-forum/models/Message.js

36 lines
903 B
JavaScript
Raw Normal View History

const mongoose = require("mongoose")
2022-08-31 14:44:28 +03:00
const cache = require("./cache")
const schema = new mongoose.Schema({
id: { type: String, unique: true },
2022-08-31 14:44:28 +03:00
author: Object,
threadID: String,
2022-08-31 14:44:28 +03:00
authorID: String,
2022-09-17 00:27:38 +03:00
content: { type: String, maxlength: 1024 },
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-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;