From c8c25521bf7233cdea4e9f498392fb0fccacb5cd Mon Sep 17 00:00:00 2001 From: rob Date: Thu, 7 Jul 2022 15:56:23 -0400 Subject: [PATCH] add authorType to Comment --- app/models/comment.js | 18 +++++++++++++++++- app/services/comment.js | 14 ++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/models/comment.js b/app/models/comment.js index ec0a0e1..2ea385e 100644 --- a/app/models/comment.js +++ b/app/models/comment.js @@ -26,7 +26,8 @@ const CommentSchema = new Schema({ created: { type: Date, default: Date.now, required: true, index: 1 }, resourceType: { type: String, enum: RESOURCE_TYPE_LIST, required: true }, resource: { type: Schema.ObjectId, required: true, index: 1, refPath: 'resourceType' }, - author: { type: Schema.ObjectId, required: true, index: 1, ref: 'User' }, + authorType: { type: String, enum: ['User', 'CoreUser'], required: true}, + author: { type: Schema.ObjectId, required: true, index: 1, refPath: 'authorType' }, 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 }, @@ -37,12 +38,27 @@ const CommentSchema = new Schema({ stats: { type: CommentStats, default: CommentStatsDefaults, required: true }, }); +/* + * An index to optimize finding comments authored by a specific author. It helps + * to use authorType as a pre-filter, then find the author by ID. This compound + * index accomplishes that. The author's comments are then indexed by status for + * additional filtering and speed. + */ +CommentSchema.index({ + authorType: 1, + author: 1, + status: 1, +}, { + name: 'comment_author_by_type', +}); + /* * An index to optimize finding replies to a specific comment */ CommentSchema.index({ resource: 1, replyTo: 1, + status: 1, }, { partialFilterExpression: { replyTo: { $exists: true } }, name: 'comment_replies', diff --git a/app/services/comment.js b/app/services/comment.js index da0cc85..96124fb 100644 --- a/app/services/comment.js +++ b/app/services/comment.js @@ -80,6 +80,7 @@ class CommentService extends SiteService { comment.created = NOW; comment.resourceType = resourceType; comment.resource = resource._id; + comment.authorType = author.type; comment.author = author._id; if (commentDefinition.replyTo) { comment.replyTo = mongoose.Types.ObjectId(commentDefinition.replyTo); @@ -125,6 +126,7 @@ class CommentService extends SiteService { comment = comment.toObject(); comment.author = author; + return comment; } @@ -187,7 +189,11 @@ class CommentService extends SiteService { async getForResource (resource, statuses, pagination) { const comments = await Comment - .find({ resource: resource._id, replyTo: { $exists: false }, status: { $in: statuses } }) + .find({ // index: 'comment_replies' + resource: resource._id, + replyTo: { $exists: false }, + status: { $in: statuses }, + }) .sort({ created: -1 }) .skip(pagination.skip) .limit(pagination.cpp) @@ -198,7 +204,11 @@ class CommentService extends SiteService { async getForAuthor (author, pagination) { const comments = await Comment - .find({ author: author._id, status: { $in: ['published', 'mod-warn'] } }) + .find({ // index: 'comment_author_by_type' + authorType: author.type, + author: author._id, + status: { $in: ['published', 'mod-warn'] }, + }) .sort({ created: -1 }) .skip(pagination.skip) .limit(pagination.cpp)