From 6aece16fd01f59b9516a6b32e88c7a330c2ad7f1 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 16 Jan 2018 11:31:44 +0100 Subject: [PATCH] Add userFlagged subscription --- graph/mutators/action.js | 18 ++++++++++++++---- graph/resolvers/subscription.js | 3 +++ graph/setupFunctions.js | 11 +++++++++++ graph/typeDefs.graphql | 5 +++++ perms/constants/subscription.js | 1 + perms/reducers/subscription.js | 1 + 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/graph/mutators/action.js b/graph/mutators/action.js index 13ccfda6b..58e215916 100644 --- a/graph/mutators/action.js +++ b/graph/mutators/action.js @@ -73,10 +73,20 @@ const createAction = async ( metadata, }); - if (action_type === 'FLAG' && item_type === 'COMMENTS') { - // The item is a comment, and this is a flag. Push that the comment was - // flagged, don't wait for it to finish. - pubsub.publish('commentFlagged', item); + if (action_type === 'FLAG') { + switch (item_type) { + case 'COMMENTS': + // The item is a comment, and this is a flag. Push that the comment was + // flagged, don't wait for it to finish. + pubsub.publish('commentFlagged', item); + break; + case 'USERS': + // The item is a user, and this is a flag. Push that the user was + // flagged, don't wait for it to finish. + pubsub.publish('userFlagged', item); + break; + default: + } } return action; diff --git a/graph/resolvers/subscription.js b/graph/resolvers/subscription.js index 8be3236c1..17b8b0075 100644 --- a/graph/resolvers/subscription.js +++ b/graph/resolvers/subscription.js @@ -26,6 +26,9 @@ const Subscription = { usernameRejected(user) { return user; }, + usernameFlagged(user) { + return user; + }, }; module.exports = Subscription; diff --git a/graph/setupFunctions.js b/graph/setupFunctions.js index e25b770dd..a1e0d1145 100644 --- a/graph/setupFunctions.js +++ b/graph/setupFunctions.js @@ -9,6 +9,7 @@ const { SUBSCRIBE_ALL_USER_BANNED, SUBSCRIBE_ALL_USERNAME_REJECTED, SUBSCRIBE_ALL_USERNAME_APPROVED, + SUBSCRIBE_ALL_USERNAME_FLAGGED, } = require('../perms/constants'); const merge = require('lodash/merge'); @@ -97,6 +98,16 @@ const setupFunctions = { } return !args.user_id || user.id === args.user_id; }, + usernameFlagged: (options, args, user, context) => { + if ( + !context.user || + (args.user_id !== user.id && + !context.user.can(SUBSCRIBE_ALL_USERNAME_FLAGGED)) + ) { + return false; + } + return !args.user_id || user.id === args.user_id; + }, usernameRejected: (options, args, user, context) => { if ( !context.user || diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index 002318bc3..e94c8d8de 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -1506,6 +1506,11 @@ type Subscription { # users with the `ADMIN` or `MODERATOR` role. userBanned(user_id: ID): User + # Get an update whenever a username was flagged. + # `user_id` must match id of current user except for + # users with the `ADMIN` or `MODERATOR` role. + usernameFlagged(user_id: ID): User + # Get an update whenever a username has been rejected. # `user_id` must match id of current user except for # users with the `ADMIN` or `MODERATOR` role. diff --git a/perms/constants/subscription.js b/perms/constants/subscription.js index 22dbd2fa3..9d87c07eb 100644 --- a/perms/constants/subscription.js +++ b/perms/constants/subscription.js @@ -9,4 +9,5 @@ module.exports = { SUBSCRIBE_ALL_USER_BANNED: 'SUBSCRIBE_ALL_USER_BANNED', SUBSCRIBE_ALL_USERNAME_REJECTED: 'SUBSCRIBE_ALL_USERNAME_REJECTED', SUBSCRIBE_ALL_USERNAME_APPROVED: 'SUBSCRIBE_ALL_USERNAME_APPROVED', + SUBSCRIBE_ALL_USERNAME_FLAGGED: 'SUBSCRIBE_ALL_USERNAME_FLAGGED', }; diff --git a/perms/reducers/subscription.js b/perms/reducers/subscription.js index 3ae7d1cf8..4f76c8264 100644 --- a/perms/reducers/subscription.js +++ b/perms/reducers/subscription.js @@ -13,6 +13,7 @@ module.exports = (user, perm) => { case types.SUBSCRIBE_ALL_USER_BANNED: case types.SUBSCRIBE_ALL_USERNAME_REJECTED: case types.SUBSCRIBE_ALL_USERNAME_APPROVED: + case types.SUBSCRIBE_ALL_USERNAME_FLAGGED: return check(user, ['ADMIN', 'MODERATOR']); default: break;