From 69c03c919061cbb384573d50a54035fcb45ab967 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Wed, 4 Oct 2017 10:29:09 -0600 Subject: [PATCH] cleaned up graph mutators --- graph/mutators/action.js | 89 +++++++++++++++++++++++++-------------- graph/mutators/comment.js | 14 ++---- 2 files changed, 61 insertions(+), 42 deletions(-) diff --git a/graph/mutators/action.js b/graph/mutators/action.js index 794da581a..813d06ec3 100644 --- a/graph/mutators/action.js +++ b/graph/mutators/action.js @@ -4,24 +4,46 @@ const errors = require('../../errors'); const {CREATE_ACTION, DELETE_ACTION} = require('../../perms/constants'); /** - * Creates an action on a item. If the item is a user flag, sets the user's status to - * pending. - * @param {Object} user the user performing the request - * @param {String} item_id id of the item to add the action to - * @param {String} item_type type of the item - * @param {String} action_type type of the action - * @return {Promise} resolves to the action created + * getActionItem will return the item that is associated with the given action. + * If it does not exist, it will throw an error. + * + * @param {Object} ctx the graphql context for the request + * @param {Object} action the action being performed + * @return {Promise} resolves to the referenced item */ -const createAction = async ({user = {}, pubsub, loaders: {Comments}}, {item_id, item_type, action_type, group_id, metadata = {}}) => { - - let comment; +const getActionItem = async ({loaders: {Comments, Users}}, {item_id, item_type}) => { if (item_type === 'COMMENTS') { - comment = await Comments.get.load(item_id); + const comment = await Comments.get.load(item_id); if (!comment) { throw new Error('Comment not found'); } - } + return comment; + } else if (item_type === 'USERS') { + const user = await Users.getByID.load(item_id); + if (!user) { + throw new Error('User not found'); + } + + return user; + } +}; + +/** + * Creates an action on a item. If the item is a user flag, sets the user's status to + * pending. + * + * @param {Object} ctx the graphql context for the request + * @param {Object} action the action being created + * @return {Promise} resolves to the action created + */ +const createAction = async (ctx, {item_id, item_type, action_type, group_id, metadata = {}}) => { + const {user = {}, pubsub} = ctx; + + // Gets the item referenced by the action. + const item = await getActionItem(ctx, {item_id, item_type}); + + // Create the action itself. let action = await ActionsService.create({ item_id, item_type, @@ -31,14 +53,17 @@ const createAction = async ({user = {}, pubsub, loaders: {Comments}}, {item_id, metadata }); - if (item_type === 'USERS' && action_type === 'FLAG') { + // If the action is a flag. + if (action_type === 'FLAG') { + if (item_type === 'USERS') { - // Set the user as pending if it was a user flag. - await UsersService.setStatus(item_id, 'PENDING'); - } + // Set the user as pending if it was a user flag. + await UsersService.setStatus(item_id, 'PENDING'); + } else if (item_type === 'COMMENTS') { - if (comment) { - pubsub.publish('commentFlagged', comment); + // Push that the comment was flagged, don't wait for it to finish. + pubsub.publish('commentFlagged', item); + } } return action; @@ -46,28 +71,28 @@ const createAction = async ({user = {}, pubsub, loaders: {Comments}}, {item_id, /** * Deletes an action based on the user id if the user owns that action. + * * @param {Object} user the user performing the request * @param {String} id the id of the action to delete * @return {Promise} resolves to the deleted action, or null if not found. */ -const deleteAction = ({user}, {id}) => { - return ActionsService.delete({id, user_id: user.id}); -}; +const deleteAction = ({user}, {id}) => ActionsService.delete({id, user_id: user.id}); -module.exports = (context) => { - if (context.user && context.user.can(CREATE_ACTION, DELETE_ACTION)) { - return { - Action: { - create: (action) => createAction(context, action), - delete: (action) => deleteAction(context, action) - } - }; - } - - return { +module.exports = (ctx) => { + const mutators = { Action: { create: () => Promise.reject(errors.ErrNotAuthorized), delete: () => Promise.reject(errors.ErrNotAuthorized) } }; + + if (ctx.user && ctx.user.can(CREATE_ACTION)) { + mutators.Action.create = (action) => createAction(ctx, action); + } + + if (ctx.user && ctx.user.can(DELETE_ACTION)) { + mutators.Action.delete = (action) => deleteAction(ctx, action); + } + + return mutators; }; diff --git a/graph/mutators/comment.js b/graph/mutators/comment.js index 17d4a371a..a0ee0a039 100644 --- a/graph/mutators/comment.js +++ b/graph/mutators/comment.js @@ -1,15 +1,12 @@ const errors = require('../../errors'); - const ActionModel = require('../../models/action'); const AssetsService = require('../../services/assets'); const ActionsService = require('../../services/actions'); const TagsService = require('../../services/tags'); const CommentsService = require('../../services/comments'); const KarmaService = require('../../services/karma'); -const tlds = require('tlds'); const merge = require('lodash/merge'); -const linkify = require('linkify-it')() - .tlds(tlds); +const linkify = require('linkify-it')().tlds(require('tlds')); const Wordlist = require('../../services/wordlist'); const { CREATE_COMMENT, @@ -18,10 +15,7 @@ const { EDIT_COMMENT } = require('../../perms/constants'); const debug = require('debug')('talk:graph:mutators:comment'); - -const { - DISABLE_AUTOFLAG_SUSPECT_WORDS -} = require('../../config'); +const {DISABLE_AUTOFLAG_SUSPECT_WORDS} = require('../../config'); const resolveTagsForComment = async ({user, loaders: {Tags}}, {asset_id, tags = []}) => { const item_type = 'COMMENTS'; @@ -280,7 +274,7 @@ const moderationPhases = [ } }, - // This phase checks to see if the comment's length exeeds maximum. + // This phase checks to see if the comment's length exceeds maximum. (context, comment, {assetSettings: {charCountEnable, charCount}}) => { // Reject if the comment is too long @@ -350,7 +344,7 @@ const moderationPhases = [ } }, - // This phase checks to see if the comment was already perscribed a status. + // This phase checks to see if the comment was already prescribed a status. (context, comment) => { // If the status was already defined, don't redefine it. It's only defined