mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-12-23 08:09:08 +03:00
53 lines
No EOL
1.4 KiB
JavaScript
53 lines
No EOL
1.4 KiB
JavaScript
const mongoose = require("mongoose");
|
|
const cache = require("./cache")
|
|
const MessageModel = require("./Message");
|
|
const schema = new mongoose.Schema({
|
|
id: { type: String, unique: true },
|
|
|
|
categoryID: String,
|
|
authorID: String,
|
|
author: Object,
|
|
|
|
title: { type: String, maxlength: 128 },
|
|
time: { type: Date, default: Date.now },
|
|
deleted: { type: Boolean, default: false },
|
|
edited: { type: Boolean, default: false },
|
|
|
|
messages: [String],
|
|
views: { type: Number, default: 0 }
|
|
|
|
});
|
|
|
|
|
|
schema.methods.get_author = cache.getAuthor;
|
|
schema.methods.get_category = () => async function () {
|
|
return await require("./Category").findOne({ id: this.categoryID }) || { id: this.categoryID, name: "Unknown" };
|
|
}
|
|
schema.methods.messageCount = async function (admin = false) {
|
|
const query = { threadID: this.id };
|
|
if (!admin) query.deleted = false;
|
|
return await MessageModel.count(query) || 0;
|
|
};
|
|
|
|
schema.methods.push = function (messageID) {
|
|
this.messages.push(messageID);
|
|
return this;
|
|
}
|
|
|
|
schema.methods.takeId = async function () {
|
|
this.id = await model.count() || 0;
|
|
return this;
|
|
}
|
|
|
|
schema.methods.getLink = function (id = this.id) {
|
|
return "/threads/" + id;
|
|
}
|
|
|
|
const model = mongoose.model('thread', schema);
|
|
|
|
model.get = async id => {
|
|
const thread = await model.findOne({ id })
|
|
return await thread.get_author();
|
|
};
|
|
|
|
module.exports = model; |