diff --git a/server/src/core/server/graph/mutators/Comments.ts b/server/src/core/server/graph/mutators/Comments.ts index d3fcb7a15b..f4a2d1ae3a 100644 --- a/server/src/core/server/graph/mutators/Comments.ts +++ b/server/src/core/server/graph/mutators/Comments.ts @@ -248,7 +248,7 @@ export const Comments = (ctx: GraphContext) => ({ // Validate that this user is allowed to moderate this comment await validateUserModerationScopes(ctx, ctx.user!, { commentID }); - const comment = await addTag( + const { comment, alreadyFeatured } = await addTag( ctx.mongo, ctx.tenant, commentID, @@ -258,6 +258,11 @@ export const Comments = (ctx: GraphContext) => ({ ctx.now ); + // The comment is already featured; we don't need to feature again + if (alreadyFeatured) { + return comment; + } + if (comment.status !== GQLCOMMENT_STATUS.APPROVED) { await approveComment( ctx.mongo, @@ -319,13 +324,18 @@ export const Comments = (ctx: GraphContext) => ({ // Validate that this user is allowed to moderate this comment await validateUserModerationScopes(ctx, ctx.user!, { commentID }); - const comment = await removeTag( + const { comment, alreadyUnfeatured } = await removeTag( ctx.mongo, ctx.tenant, commentID, GQLTAG.FEATURED ); + // The comment is already unfeatured; we don't need to unfeature again + if (alreadyUnfeatured) { + return comment; + } + // If the tag is sucessfully removed (the tag is // no longer present on the comment) then we can // update the tag story counts. diff --git a/server/src/core/server/services/comments/comments.ts b/server/src/core/server/services/comments/comments.ts index 4863bd8ec8..124550aa0e 100644 --- a/server/src/core/server/services/comments/comments.ts +++ b/server/src/core/server/services/comments/comments.ts @@ -91,14 +91,17 @@ export async function addTag( // Check to see if this tag is already on this comment. if (comment.tags.some(({ type }) => type === tagType)) { - return comment; + return { comment, alreadyFeatured: true }; } - return addCommentTag(mongo, tenant.id, commentID, { - type: tagType, - createdBy: user.id, - createdAt: now, - }); + return { + comment: await addCommentTag(mongo, tenant.id, commentID, { + type: tagType, + createdBy: user.id, + createdAt: now, + }), + alreadyFeatured: false, + }; } /** @@ -126,10 +129,13 @@ export async function removeTag( // Check to see if this tag is even on this comment. if (comment.tags.every(({ type }) => type !== tagType)) { - return comment; + return { comment, alreadyUnfeatured: true }; } - return removeCommentTag(mongo, tenant.id, commentID, tagType); + return { + comment: await removeCommentTag(mongo, tenant.id, commentID, tagType), + alreadyUnfeatured: false, + }; } /** diff --git a/server/src/core/server/stacks/rejectComment.ts b/server/src/core/server/stacks/rejectComment.ts index 5955c0beae..15e60162dd 100644 --- a/server/src/core/server/stacks/rejectComment.ts +++ b/server/src/core/server/stacks/rejectComment.ts @@ -49,7 +49,12 @@ const stripTag = async ( return comment; } - const tagResult = await removeTag(mongo, tenant, comment.id, tag); + const { comment: tagResult } = await removeTag( + mongo, + tenant, + comment.id, + tag + ); await updateTagCommentCounts( tenant.id,