2022-08-29 19:31:59 +03:00
|
|
|
const mongoose = require("mongoose");
|
2022-08-31 14:44:28 +03:00
|
|
|
const cache = require("./cache")
|
2022-08-29 19:31:59 +03:00
|
|
|
const MessageModel = require("./Message");
|
2022-09-21 23:54:48 +03:00
|
|
|
const { limits, defaultThreadState } = require("../config.json");
|
|
|
|
const { threadEnum } = require("../lib");
|
2022-08-09 19:16:34 +03:00
|
|
|
const schema = new mongoose.Schema({
|
|
|
|
id: { type: String, unique: true },
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-09-09 17:13:37 +03:00
|
|
|
categoryID: String,
|
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;
|
|
|
|
}
|
|
|
|
},
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-09-17 20:25:19 +03:00
|
|
|
title: { type: String, maxlength: limits.title },
|
2022-09-21 23:06:14 +03:00
|
|
|
oldTitles: [String],
|
2022-10-09 20:10:19 +03:00
|
|
|
|
2022-08-09 19:16:34 +03:00
|
|
|
time: { type: Date, default: Date.now },
|
2022-08-29 19:31:59 +03:00
|
|
|
edited: { type: Boolean, default: false },
|
2022-10-09 21:23:31 +03:00
|
|
|
state: { type: String, default: defaultThreadState, enum: threadEnum, uppercase: true },
|
2022-08-28 15:00:53 +03:00
|
|
|
messages: [String],
|
|
|
|
views: { type: Number, default: 0 }
|
2022-08-09 19:16:34 +03:00
|
|
|
});
|
|
|
|
|
2022-08-31 14:44:28 +03:00
|
|
|
|
|
|
|
schema.methods.get_author = cache.getAuthor;
|
2022-10-09 20:10:19 +03:00
|
|
|
|
|
|
|
schema.virtual("deleted").get(function () {
|
|
|
|
return this.state === "DELETED";
|
|
|
|
}).set(function (value) {
|
|
|
|
this.set({ state: value ? "DELETED" : "OPEN" });
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2022-09-21 23:54:48 +03:00
|
|
|
schema.methods.get_category = async function () {
|
2022-09-17 00:27:38 +03:00
|
|
|
return await require("./Category").findOne({ id: this.categoryID }) || { id: this.categoryID, name: "Unknown" };
|
2022-09-09 17:13:37 +03:00
|
|
|
}
|
2022-08-29 19:31:59 +03:00
|
|
|
schema.methods.messageCount = async function (admin = false) {
|
2022-09-17 00:27:38 +03:00
|
|
|
const query = { threadID: this.id };
|
2022-08-29 19:31:59 +03:00
|
|
|
if (!admin) query.deleted = false;
|
|
|
|
return await MessageModel.count(query) || 0;
|
|
|
|
};
|
2022-08-31 14:44:28 +03:00
|
|
|
|
2022-08-09 19:16:34 +03:00
|
|
|
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);
|
|
|
|
|
2022-10-09 20:10:19 +03:00
|
|
|
model.get = id => model.findOne({ id }).then(thread => thread.get_author());
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-08-09 19:16:34 +03:00
|
|
|
module.exports = model;
|