Skip to content

Commit

Permalink
move parent revision id check to comment reply creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kabeaty committed Aug 31, 2023
1 parent a50fff7 commit d003b0f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
18 changes: 17 additions & 1 deletion src/core/server/stacks/createComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AuthorAlreadyHasRatedStory,
CannotCreateCommentOnArchivedStory,
CommentNotFoundError,
CommentRevisionNotFoundError,
CoralError,
StoryNotFoundError,
UserSiteBanned,
Expand All @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/stacks/editComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 0 additions & 8 deletions src/core/server/stacks/helpers/retrieveParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ParentCommentRejectedError,
} from "coral-server/errors";
import {
getLatestRevision,
hasPublishedStatus,
retrieveComment,
} from "coral-server/models/comment";
Expand All @@ -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) {
Expand Down

0 comments on commit d003b0f

Please sign in to comment.