diff --git a/src/core/server/services/comments/pipeline/phases/repeatPost.ts b/src/core/server/services/comments/pipeline/phases/repeatPost.ts index 31412575f..82687c186 100644 --- a/src/core/server/services/comments/pipeline/phases/repeatPost.ts +++ b/src/core/server/services/comments/pipeline/phases/repeatPost.ts @@ -16,6 +16,8 @@ import { export const repeatPost: IntermediateModerationPhase = async ({ mongo, + comment, + action, bodyText, tenant, author, @@ -44,6 +46,12 @@ export const repeatPost: IntermediateModerationPhase = async ({ return; } + if (action === "EDIT" && comment.id && comment.id === lastComment.id) { + // The last comment written is this comment because we're editing, so no + // need to compare against. + return; + } + const lastCommentRevision = getLatestRevision(lastComment); const lastCommentBodyText = getHTMLPlainText(lastCommentRevision.body); diff --git a/src/core/server/services/comments/pipeline/pipeline.ts b/src/core/server/services/comments/pipeline/pipeline.ts index fc3040473..76f82752a 100644 --- a/src/core/server/services/comments/pipeline/pipeline.ts +++ b/src/core/server/services/comments/pipeline/pipeline.ts @@ -68,7 +68,12 @@ export interface ModerationPhaseContextInput { comment: RequireProperty< Partial>, "body" | "ancestorIDs" - >; + > & { + /** + * id is used to provide the comment ID in the event this is a EDIT action. + */ + id?: string; + }; author: User; now: Date; action: "NEW" | "EDIT"; diff --git a/src/core/server/stacks/editComment.ts b/src/core/server/stacks/editComment.ts index 3b63fa9aa..6f5a662ea 100644 --- a/src/core/server/stacks/editComment.ts +++ b/src/core/server/stacks/editComment.ts @@ -14,6 +14,7 @@ import { createCommentModerationAction } from "coral-server/models/action/modera import { editComment, EditCommentInput, + getLatestRevision, GiphyMedia, retrieveComment, TwitterMedia, @@ -110,10 +111,22 @@ export default async function edit( throw new StoryNotFoundError(originalStaleComment.storyID); } + const lastRevision = getLatestRevision(originalStaleComment); + let media: GiphyMedia | TwitterMedia | YouTubeMedia | undefined; + + // attach a new media object from input IF: + // input.media is defined AND EITHER: + // - previous revision has no media OR + // - previous revision has media with a different URL + // otherwise, attach previous revision media if present + if (input.media) { - // TODO: (wyattjoh) check to see if the media is the same. - media = await attachMedia(tenant, input.media, input.body); + if (!lastRevision.media || lastRevision.media.url !== input.media.url) { + media = await attachMedia(tenant, input.media, input.body); + } else if (lastRevision.media) { + media = lastRevision.media; + } } // Run the comment through the moderation phases.