From 5f7c4f82e72ed89cc83e8a1c2f8f13ba4400b6ac Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Wed, 20 Sep 2017 02:50:43 +0700 Subject: [PATCH] Return flags when creating comment, restrict access to actions in comment resolver --- client/coral-embed-stream/src/graphql/index.js | 14 +++++--------- client/talk-plugin-commentbox/CommentBox.js | 11 ++++++----- graph/resolvers/root_mutation.js | 12 +++++++++--- .../client/components/CheckToxicityHook.js | 4 ++-- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/client/coral-embed-stream/src/graphql/index.js b/client/coral-embed-stream/src/graphql/index.js index 82d6ca9e3..d7af76d82 100644 --- a/client/coral-embed-stream/src/graphql/index.js +++ b/client/coral-embed-stream/src/graphql/index.js @@ -64,6 +64,10 @@ export default { hasNextPage } } + flags { + reason + message + } } fragment CoralEmbedStream_CreateCommentResponse_Comment on Comment { @@ -77,14 +81,6 @@ export default { title url } - actions { - __typename - id - ... on FlagAction { - reason - message - } - } tags { tag { name @@ -124,6 +120,7 @@ export default { createComment: { __typename: 'CreateCommentResponse', errors: null, + flags: [], comment: { __typename: 'Comment', user: { @@ -133,7 +130,6 @@ export default { }, created_at: new Date().toISOString(), body, - actions: [], action_summaries: [], tags: tags.map((tag) => ({ tag: { diff --git a/client/talk-plugin-commentbox/CommentBox.js b/client/talk-plugin-commentbox/CommentBox.js index 409ad86ec..49c4df0eb 100644 --- a/client/talk-plugin-commentbox/CommentBox.js +++ b/client/talk-plugin-commentbox/CommentBox.js @@ -13,18 +13,18 @@ export const name = 'talk-plugin-commentbox'; const notifyReasons = ['LINKS', 'TRUST']; -function shouldNotify(actions = []) { - return actions.some(({reason}) => notifyReasons.includes(reason)); +function shouldNotify(flags = []) { + return flags.some(({reason}) => notifyReasons.includes(reason)); } // Given a newly posted comment's status, show a notification to the user // if needed -export const notifyForNewCommentStatus = (notify, comment) => { +export const notifyForNewCommentStatus = (notify, comment, flags) => { if (comment.status === 'REJECTED') { notify('error', t('comment_box.comment_post_banned_word')); } else if ( comment.status === 'PREMOD' || - comment.status === 'SYSTEM_WITHHELD' && shouldNotify(comment.actions)) { + comment.status === 'SYSTEM_WITHHELD' && shouldNotify(flags)) { notify('success', t('comment_box.comment_post_notif_premod')); } }; @@ -78,11 +78,12 @@ class CommentBox extends React.Component { .then(({data}) => { this.setState({loadingState: 'success', body: ''}); const postedComment = data.createComment.comment; + const flags = data.createComment.flags; // Execute postSubmit Hooks this.state.hooks.postSubmit.forEach((hook) => hook(data)); - notifyForNewCommentStatus(notify, postedComment); + notifyForNewCommentStatus(notify, postedComment, flags); if (commentPostedHandler) { commentPostedHandler(); diff --git a/graph/resolvers/root_mutation.js b/graph/resolvers/root_mutation.js index df67b8e8a..c7794303b 100644 --- a/graph/resolvers/root_mutation.js +++ b/graph/resolvers/root_mutation.js @@ -1,7 +1,13 @@ const RootMutation = { - createComment: async (_, {input}, {mutators: {Comment}}) => ({ - comment: await Comment.create(input), - }), + createComment: async (_, {input}, {mutators: {Comment}, loaders: {Actions}}) => { + const comment = await Comment.create(input); + + // Retrieve flags that was assigned to comment. + const actions = await Actions.getByID.load(comment.id); + const flags = actions.filter(({action_type}) => action_type === 'FLAG'); + + return {comment, flags}; + }, editComment: async (_, {id, asset_id, edit: {body}}, {mutators: {Comment}}) => ({ comment: await Comment.edit({id, asset_id, edit: {body}}), }), diff --git a/plugins/talk-plugin-toxic-comments/client/components/CheckToxicityHook.js b/plugins/talk-plugin-toxic-comments/client/components/CheckToxicityHook.js index 8572bdd5a..44e2e89a7 100644 --- a/plugins/talk-plugin-toxic-comments/client/components/CheckToxicityHook.js +++ b/plugins/talk-plugin-toxic-comments/client/components/CheckToxicityHook.js @@ -23,8 +23,8 @@ export default class CheckToxicityHook extends React.Component { }); this.toxicityPostHook = this.props.registerHook('postSubmit', (result) => { - const actions = result.createComment.comment && result.createComment.comment.actions; - if (actions && actions.some(({reason}) => reason === 'TOXIC_COMMENT')) { + const flags = result.createComment.flags; + if (flags && flags.some(({reason}) => reason === 'TOXIC_COMMENT')) { this.props.notify('error', t('talk-plugin-toxic-comments.still_toxic')); }