From 74fbadcc0f85223fde5629d6820badeeaddfcc95 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Fri, 16 Jun 2017 22:55:22 +0700 Subject: [PATCH] Refactor subscriptions --- .../Moderation/containers/Moderation.js | 60 +++++++++++++++---- graph/mutators/comment.js | 11 +++- graph/resolvers/subscription.js | 5 +- graph/subscriptions.js | 14 ++++- graph/typeDefs.graphql | 8 ++- 5 files changed, 79 insertions(+), 19 deletions(-) diff --git a/client/coral-admin/src/routes/Moderation/containers/Moderation.js b/client/coral-admin/src/routes/Moderation/containers/Moderation.js index 1d75b3838..021ac3ceb 100644 --- a/client/coral-admin/src/routes/Moderation/containers/Moderation.js +++ b/client/coral-admin/src/routes/Moderation/containers/Moderation.js @@ -39,18 +39,18 @@ class ModerationContainer extends Component { subscribeToUpdates() { const sub1 = this.props.data.subscribeToMore({ - document: STATUS_CHANGED_SUBSCRIPTION, + document: COMMENT_ACCEPTED_SUBSCRIPTION, variables: { asset_id: this.props.data.variables.asset_id, }, - updateQuery: (prev, {subscriptionData: {data: {commentStatusChanged: comment}}}) => { + updateQuery: (prev, {subscriptionData: {data: {commentAccepted: comment}}}) => { const user = comment.status_history[comment.status_history.length - 1].assigned_by; const sort = this.props.moderation.sortOrder; const notify = this.props.auth.user.id === user.id ? {} : { activeQueue: this.activeTab, - text: `${user.username} ${comment.status.toLowerCase()} comment "${truncate(comment.body, {lenght: 50})}"`, + text: `${user.username} accepted comment "${truncate(comment.body, {lenght: 50})}"`, anyQueue: false, }; return handleCommentChange(prev, comment, sort, notify); @@ -58,7 +58,26 @@ class ModerationContainer extends Component { }); const sub2 = this.props.data.subscribeToMore({ - document: COMMENTS_EDITED_SUBSCRIPTION, + document: COMMENT_REJECTED_SUBSCRIPTION, + variables: { + asset_id: this.props.data.variables.asset_id, + }, + updateQuery: (prev, {subscriptionData: {data: {commentRejected: comment}}}) => { + const user = comment.status_history[comment.status_history.length - 1].assigned_by; + const sort = this.props.moderation.sortOrder; + const notify = this.props.auth.user.id === user.id + ? {} + : { + activeQueue: this.activeTab, + text: `${user.username} rejected comment "${truncate(comment.body, {lenght: 50})}"`, + anyQueue: false, + }; + return handleCommentChange(prev, comment, sort, notify); + }, + }); + + const sub3 = this.props.data.subscribeToMore({ + document: COMMENT_EDITED_SUBSCRIPTION, variables: { asset_id: this.props.data.variables.asset_id, }, @@ -73,8 +92,8 @@ class ModerationContainer extends Component { }, }); - const sub3 = this.props.data.subscribeToMore({ - document: COMMENTS_FLAGGED_SUBSCRIPTION, + const sub4 = this.props.data.subscribeToMore({ + document: COMMENT_FLAGGED_SUBSCRIPTION, variables: { asset_id: this.props.data.variables.asset_id, }, @@ -90,7 +109,7 @@ class ModerationContainer extends Component { }, }); - this.subscriptions.push(sub1, sub2, sub3); + this.subscriptions.push(sub1, sub2, sub3, sub4); } unsubscribe() { @@ -227,7 +246,7 @@ class ModerationContainer extends Component { } } -const COMMENTS_EDITED_SUBSCRIPTION = gql` +const COMMENT_EDITED_SUBSCRIPTION = gql` subscription CommentEdited($asset_id: ID){ commentEdited(asset_id: $asset_id){ ...${getDefinitionName(Comment.fragments.comment)} @@ -236,7 +255,7 @@ const COMMENTS_EDITED_SUBSCRIPTION = gql` ${Comment.fragments.comment} `; -const COMMENTS_FLAGGED_SUBSCRIPTION = gql` +const COMMENT_FLAGGED_SUBSCRIPTION = gql` subscription CommentFlagged($asset_id: ID){ commentFlagged(asset_id: $asset_id){ ...${getDefinitionName(Comment.fragments.comment)} @@ -245,9 +264,26 @@ const COMMENTS_FLAGGED_SUBSCRIPTION = gql` ${Comment.fragments.comment} `; -const STATUS_CHANGED_SUBSCRIPTION = gql` - subscription CommentStatusChanged($asset_id: ID){ - commentStatusChanged(asset_id: $asset_id){ +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 + } + } + } + } + ${Comment.fragments.comment} +`; + +const COMMENT_REJECTED_SUBSCRIPTION = gql` + subscription CommentRejected($asset_id: ID){ + commentRejected(asset_id: $asset_id){ ...${getDefinitionName(Comment.fragments.comment)} status_history { type diff --git a/graph/mutators/comment.js b/graph/mutators/comment.js index 8d01ededf..df6ed71ce 100644 --- a/graph/mutators/comment.js +++ b/graph/mutators/comment.js @@ -348,8 +348,15 @@ const setStatus = async ({user, loaders: {Comments}, pubsub}, {id, status}) => { if (pubsub) { - // Publish the comment status change via the subscription. - pubsub.publish('commentStatusChanged', comment); + if (status === 'ACCEPTED') { + + // Publish the comment status change via the subscription. + pubsub.publish('commentAccepted', comment); + } else if (status === 'REJECTED') { + + // Publish the comment status change via the subscription. + pubsub.publish('commentRejected', comment); + } } return comment; diff --git a/graph/resolvers/subscription.js b/graph/resolvers/subscription.js index 3c2130ff1..1f26d5ff3 100644 --- a/graph/resolvers/subscription.js +++ b/graph/resolvers/subscription.js @@ -5,7 +5,10 @@ const Subscription = { commentEdited(comment) { return comment; }, - commentStatusChanged(comment) { + commentAccepted(comment) { + return comment; + }, + commentRejected(comment) { return comment; }, commentFlagged(comment) { diff --git a/graph/subscriptions.js b/graph/subscriptions.js index 494345319..47899e1e3 100644 --- a/graph/subscriptions.js +++ b/graph/subscriptions.js @@ -57,8 +57,18 @@ const setupFunctions = plugins.get('server', 'setupFunctions').reduce((acc, {plu } }, }), - commentStatusChanged: (options, args) => ({ - commentStatusChanged: { + commentAccepted: (options, args) => ({ + commentAccepted: { + filter: ({comment}, context) => { + if (!context.user || !context.user.can(SUBSCRIBE_COMMENT_STATUS)) { + return false; + } + return !args.asset_id || comment.asset_id === args.asset_id; + } + }, + }), + commentRejected: (options, args) => ({ + commentRejected: { filter: ({comment}, context) => { if (!context.user || !context.user.can(SUBSCRIBE_COMMENT_STATUS)) { return false; diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index 948d0672f..9cd147605 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -959,9 +959,13 @@ type Subscription { # Requires the `ADMIN` or `MODERATOR` role. commentFlagged(asset_id: ID): Comment - # Get an update whenever the status of a comment changed due to a moderator action. + # Get an update whenever a comment has been accepted. # Requires the `ADMIN` or `MODERATOR` role. - commentStatusChanged(asset_id: ID): Comment + commentAccepted(asset_id: ID): Comment + + # Get an update whenever a comment has been rejected. + # Requires the `ADMIN` or `MODERATOR` role. + commentRejected(asset_id: ID): Comment } ################################################################################