// comment.js // Copyright (C) 2021 Digital Telepresence, LLC // License: Apache-2.0 'use strict'; const path = require('path'); const mongoose = require('mongoose'); const Schema = mongoose.Schema; const CommentHistorySchema = new Schema({ created: { type: Date }, content: { type: String, maxlength: 3000 }, }); const { CommentStats, CommentStatsDefaults } = require(path.join(__dirname, 'lib', 'resource-stats.js')); const COMMENT_STATUS_LIST = ['published', 'removed', 'mod-warn', 'mod-removed']; const CommentSchema = new Schema({ created: { type: Date, default: Date.now, required: true, index: 1 }, resourceType: { type: String, enum: ['Post', 'Page', 'Newsletter'], required: true }, resource: { type: Schema.ObjectId, required: true, index: 1, refPath: 'resourceType' }, author: { type: Schema.ObjectId, required: true, index: 1, ref: 'User' }, replyTo: { type: Schema.ObjectId, index: 1, ref: 'Comment' }, status: { type: String, enum: COMMENT_STATUS_LIST, default: 'published', required: true }, content: { type: String, required: true, maxlength: 3000 }, contentHistory: { type: [CommentHistorySchema], select: false }, stats: { type: CommentStats, default: CommentStatsDefaults, required: true }, }); /* * An index to optimize finding replies to a specific comment */ CommentSchema.index({ resource: 1, replyTo: 1, }, { partialFilterExpression: { $exists: { replyTo: 1 } }, name: 'comment_replies', }); module.exports = mongoose.model('Comment', CommentSchema);