From ce25cfb159ceaa163237e2db55f947b3b1a9e102 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Thu, 25 Jan 2018 22:03:37 +0100 Subject: [PATCH] Reconstruct previous comment state more accurately --- .../src/containers/CommentLabels.js | 1 + .../routes/Moderation/containers/Indicator.js | 11 +-- .../Moderation/containers/Moderation.js | 49 +++++----- .../src/routes/Moderation/graphql.js | 96 ++++++++++++++++--- graph/typeDefs.graphql | 3 + .../containers/ModIndicatorSubscription.js | 53 ++++------ .../client/containers/ModSubscription.js | 27 ++++-- 7 files changed, 153 insertions(+), 87 deletions(-) diff --git a/client/coral-admin/src/containers/CommentLabels.js b/client/coral-admin/src/containers/CommentLabels.js index 4cee8b1cc..ee65eb636 100644 --- a/client/coral-admin/src/containers/CommentLabels.js +++ b/client/coral-admin/src/containers/CommentLabels.js @@ -25,6 +25,7 @@ export default withFragments({ id role } + created_at } ${getSlotFragmentSpreads(slots, 'comment')} } diff --git a/client/coral-admin/src/routes/Moderation/containers/Indicator.js b/client/coral-admin/src/routes/Moderation/containers/Indicator.js index 41c5be990..b5620765e 100644 --- a/client/coral-admin/src/routes/Moderation/containers/Indicator.js +++ b/client/coral-admin/src/routes/Moderation/containers/Indicator.js @@ -135,20 +135,17 @@ const fields = ` status actions { __typename - ... on FlagAction { - reason - } - user { - id - role - } + created_at } status_history { type assigned_by { id } + created_at } + updated_at + created_at `; const COMMENT_ADDED_SUBSCRIPTION = gql` diff --git a/client/coral-admin/src/routes/Moderation/containers/Moderation.js b/client/coral-admin/src/routes/Moderation/containers/Moderation.js index 481a6d331..c654ad6b8 100644 --- a/client/coral-admin/src/routes/Moderation/containers/Moderation.js +++ b/client/coral-admin/src/routes/Moderation/containers/Moderation.js @@ -338,10 +338,30 @@ class ModerationContainer extends Component { ); } } + +const subscriptionFields = ` + status + actions { + __typename + created_at + } + status_history { + type + created_at + assigned_by { + id + username + } + } + created_at + updated_at +`; + const COMMENT_ADDED_SUBSCRIPTION = gql` subscription CommentAdded($asset_id: ID){ commentAdded(asset_id: $asset_id, statuses: null){ ...${getDefinitionName(Comment.fragments.comment)} + ${subscriptionFields} } } ${Comment.fragments.comment} @@ -351,6 +371,7 @@ const COMMENT_EDITED_SUBSCRIPTION = gql` subscription CommentEdited($asset_id: ID){ commentEdited(asset_id: $asset_id){ ...${getDefinitionName(Comment.fragments.comment)} + ${subscriptionFields} } } ${Comment.fragments.comment} @@ -360,6 +381,7 @@ const COMMENT_FLAGGED_SUBSCRIPTION = gql` subscription CommentFlagged($asset_id: ID){ commentFlagged(asset_id: $asset_id){ ...${getDefinitionName(Comment.fragments.comment)} + ${subscriptionFields} } } ${Comment.fragments.comment} @@ -369,14 +391,7 @@ const COMMENT_ACCEPTED_SUBSCRIPTION = gql` subscription CommentAccepted($asset_id: ID){ commentAccepted(asset_id: $asset_id){ ...${getDefinitionName(Comment.fragments.comment)} - status_history { - type - created_at - assigned_by { - id - username - } - } + ${subscriptionFields} } } ${Comment.fragments.comment} @@ -386,14 +401,7 @@ const COMMENT_REJECTED_SUBSCRIPTION = gql` subscription CommentRejected($asset_id: ID){ commentRejected(asset_id: $asset_id){ ...${getDefinitionName(Comment.fragments.comment)} - status_history { - type - created_at - assigned_by { - id - username - } - } + ${subscriptionFields} } } ${Comment.fragments.comment} @@ -403,14 +411,7 @@ const COMMENT_RESET_SUBSCRIPTION = gql` subscription CommentReset($asset_id: ID){ commentReset(asset_id: $asset_id){ ...${getDefinitionName(Comment.fragments.comment)} - status_history { - type - created_at - assigned_by { - id - username - } - } + ${subscriptionFields} } } ${Comment.fragments.comment} diff --git a/client/coral-admin/src/routes/Moderation/graphql.js b/client/coral-admin/src/routes/Moderation/graphql.js index ae4bb5e6e..e113e02e1 100644 --- a/client/coral-admin/src/routes/Moderation/graphql.js +++ b/client/coral-admin/src/routes/Moderation/graphql.js @@ -91,20 +91,87 @@ function getCommentQueues(comment, queueConfig) { return queues; } +function getOlderDate(a, b) { + if (a) { + a = new Date(a); + } + if (b) { + b = new Date(b); + } + + if (!b) { + return a; + } + + if (!a) { + return b; + } + return a < b ? b : a; +} + +function determineLatestChange(comment) { + let lc = null; + + // Skip it when comment itself was not updated. + // We'll get use the one from the status_history instead + // as they might diverge a little bit (status_history item is created + // before the comment itself) + if (comment.createdAt !== comment.updatedAt) { + lc = getOlderDate(lc, comment.createdAt); + lc = getOlderDate(lc, comment.updatedAt); + } + + comment.status_history.forEach(item => { + lc = getOlderDate(lc, item.created_at); + lc = getOlderDate(lc, item.updated_at); + }); + + comment.actions.forEach(item => { + lc = getOlderDate(lc, item.created_at); + lc = getOlderDate(lc, item.updated_at); + }); + + return lc; +} + +function reconstructPreviousCommentState(comment) { + const history = comment.status_history; + const actions = comment.actions; + const lastChangeDate = determineLatestChange(comment); + console.log(lastChangeDate); + const previousComment = { + ...comment, + status_history: history.filter( + item => new Date(item.created_at) < lastChangeDate + ), + actions: actions.filter(item => new Date(item.created_at) < lastChangeDate), + }; + + // Comment did not exist previously. + if (!previousComment.status_history.length) { + return null; + } + + previousComment.status = + previousComment.status_history[ + previousComment.status_history.length - 1 + ].type; + + return previousComment; +} + /** * getPreviousCommentQueues determines queues that this comment previously belonged to. */ function getPreviousCommentQueues(comment, queueConfig) { - return comment.status_history.length <= 1 - ? [] - : getCommentQueues( - { - ...comment, - status: - comment.status_history[comment.status_history.length - 2].type, - }, - queueConfig - ); + const previousCommentState = reconstructPreviousCommentState(comment); + + console.log(previousCommentState, comment); + if (!previousCommentState) { + return []; + } + + return getCommentQueues(previousCommentState, queueConfig); } /** @@ -224,6 +291,8 @@ export function handleCommentChange( // Queues that this comment needs to be placed. const nextQueues = getCommentQueues(comment, queueConfig); + console.log(prevQueues, nextQueues); + let notificationShown = false; const showNotificationOnce = () => { if (notificationShown) { @@ -318,13 +387,12 @@ export function handleIndicatorChange(root, comment, queueConfig) { for (const queue of indicatorQueues) { if (prevQueues.indexOf(queue) === -1 && nextQueues.indexOf(queue) >= 0) { next = increaseCommentCount(next, queue); - } else if ( - prevQueues.indexOf(queue) >= 0 && - nextQueues.indexOf(queue) === -1 - ) { + } + if (prevQueues.indexOf(queue) >= 0 && nextQueues.indexOf(queue) === -1) { next = decreaseCommentCount(next, queue); } } + console.log(prevQueues, nextQueues, next); return next; } diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index 79d1c5b0f..550856082 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -502,6 +502,9 @@ type Comment { # The time when the comment was created created_at: Date! + # The time when the comment was updated. + updated_at: Date + # describes how the comment can be edited editing: EditInfo diff --git a/plugins/talk-plugin-featured-comments/client/containers/ModIndicatorSubscription.js b/plugins/talk-plugin-featured-comments/client/containers/ModIndicatorSubscription.js index 53af12eb4..872fe641c 100644 --- a/plugins/talk-plugin-featured-comments/client/containers/ModIndicatorSubscription.js +++ b/plugins/talk-plugin-featured-comments/client/containers/ModIndicatorSubscription.js @@ -39,27 +39,28 @@ class ModIndicatorSubscription extends React.Component { } } +const fields = ` + status + actions { + __typename + created_at + } + status_history { + type + assigned_by { + id + } + created_at + } + updated_at + created_at +`; + const COMMENT_FEATURED_SUBSCRIPTION = gql` subscription TalkFeaturedComments_Indicator_CommentFeatured { commentFeatured { comment { - status - actions { - __typename - ... on FlagAction { - reason - } - user { - id - role - } - } - status_history { - type - assigned_by { - id - } - } + ${fields} } } } @@ -69,23 +70,7 @@ const COMMENT_UNFEATURED_SUBSCRIPTION = gql` subscription TalkFeaturedComments_Indicator_CommentUnfeatured { commentUnfeatured { comment { - status - actions { - __typename - ... on FlagAction { - reason - } - user { - id - role - } - } - status_history { - type - assigned_by { - id - } - } + ${fields} } } } diff --git a/plugins/talk-plugin-featured-comments/client/containers/ModSubscription.js b/plugins/talk-plugin-featured-comments/client/containers/ModSubscription.js index 3e7eb3aaf..da00e8ba7 100644 --- a/plugins/talk-plugin-featured-comments/client/containers/ModSubscription.js +++ b/plugins/talk-plugin-featured-comments/client/containers/ModSubscription.js @@ -74,19 +74,29 @@ class ModSubscription extends React.Component { } } +const fields = ` + status + actions { + __typename + created_at + } + status_history { + type + assigned_by { + id + } + created_at + } + updated_at + created_at +`; + const COMMENT_FEATURED_SUBSCRIPTION = gql` subscription CommentFeatured($assetId: ID){ commentFeatured(asset_id: $assetId) { comment { ...${getDefinitionName(Comment.fragments.comment)} - status_history { - type - created_at - assigned_by { - id - username - } - } + ${fields} } user { id @@ -102,6 +112,7 @@ const COMMENT_UNFEATURED_SUBSCRIPTION = gql` commentUnfeatured(asset_id: $assetId){ comment { ...${getDefinitionName(Comment.fragments.comment)} + ${fields} } user { id