From d003b0f40cef569a5774f40317daec1a6319097f Mon Sep 17 00:00:00 2001 From: Kathryn Beaty Date: Thu, 31 Aug 2023 09:14:12 -0400 Subject: [PATCH] move parent revision id check to comment reply creation --- src/core/server/stacks/createComment.ts | 18 +++++++++++++++++- src/core/server/stacks/editComment.ts | 2 +- .../server/stacks/helpers/retrieveParent.ts | 8 -------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/core/server/stacks/createComment.ts b/src/core/server/stacks/createComment.ts index f97dab98ea..ed5e647c2c 100644 --- a/src/core/server/stacks/createComment.ts +++ b/src/core/server/stacks/createComment.ts @@ -10,6 +10,7 @@ import { AuthorAlreadyHasRatedStory, CannotCreateCommentOnArchivedStory, CommentNotFoundError, + CommentRevisionNotFoundError, CoralError, StoryNotFoundError, UserSiteBanned, @@ -30,7 +31,11 @@ import { pushChildCommentIDOntoParent, retrieveManyComments, } from "coral-server/models/comment"; -import { getDepth, hasAncestors } from "coral-server/models/comment/helpers"; +import { + getDepth, + getLatestRevision, + hasAncestors, +} from "coral-server/models/comment/helpers"; import { markSeenComments } from "coral-server/models/seenComments/seenComments"; import { retrieveSite } from "coral-server/models/site"; import { @@ -269,6 +274,17 @@ export default async function create( const ancestorIDs: string[] = []; let parent = await retrieveParent(mongo, tenant.id, input); if (parent) { + // Check to see that the most recent revision matches the one we just replied + // to. + if (input.parentRevisionID) { + const revision = getLatestRevision(parent); + if (revision.id !== input.parentRevisionID) { + throw new CommentRevisionNotFoundError( + parent.id, + input.parentRevisionID + ); + } + } ancestorIDs.push(parent.id); if (hasAncestors(parent)) { // Push the parent's ancestors id's into the comment's ancestor id's. diff --git a/src/core/server/stacks/editComment.ts b/src/core/server/stacks/editComment.ts index 78e4f559b8..3baf17180f 100644 --- a/src/core/server/stacks/editComment.ts +++ b/src/core/server/stacks/editComment.ts @@ -103,7 +103,7 @@ export default async function edit( throw new CommentNotFoundError(input.id); } - // If the original comment was a reply, then get it's parent! + // If the original comment was a reply, then get its parent! const { parentID, parentRevisionID, siteID } = originalStaleComment; const parent = await retrieveParent(mongo, tenant.id, { parentID, diff --git a/src/core/server/stacks/helpers/retrieveParent.ts b/src/core/server/stacks/helpers/retrieveParent.ts index 19798d1900..b55d641053 100644 --- a/src/core/server/stacks/helpers/retrieveParent.ts +++ b/src/core/server/stacks/helpers/retrieveParent.ts @@ -5,7 +5,6 @@ import { ParentCommentRejectedError, } from "coral-server/errors"; import { - getLatestRevision, hasPublishedStatus, retrieveComment, } from "coral-server/models/comment"; @@ -31,13 +30,6 @@ async function retrieveParent( throw new CommentNotFoundError(input.parentID); } - // Check to see that the most recent revision matches the one we just replied - // to. - const revision = getLatestRevision(parent); - if (revision.id !== input.parentRevisionID) { - throw new CommentRevisionNotFoundError(parent.id, input.parentRevisionID); - } - // Check that the parent comment was visible. if (!hasPublishedStatus(parent)) { if (parent.status === GQLCOMMENT_STATUS.REJECTED) {