[CORL-1230] corrects logic for editing comments with media attachments (#3063)

* correctly attach media to edited comments

* ensure media can be removed in edits

* fix: updates to support repeat post

Co-authored-by: Kim Gardner <kgardnr@gmail.com>
Co-authored-by: Wyatt Johnson <wyattjoh@gmail.com>
This commit is contained in:
Tessa Thornton
2020-07-31 14:03:54 -04:00
committed by GitHub
parent f7d01174d7
commit 871d3f80c1
3 changed files with 29 additions and 3 deletions
@@ -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);
@@ -68,7 +68,12 @@ export interface ModerationPhaseContextInput {
comment: RequireProperty<
Partial<Omit<CreateCommentInput, "media">>,
"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";
+15 -2
View File
@@ -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.