From 5d1eff0ddb69b7472ddf79ab1f356ce17e31a019 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 12 Feb 2018 19:02:27 -0700 Subject: [PATCH] added mutations --- .../index.js | 36 ++++++++++----- .../talk-plugin-notifications/server/index.js | 2 + .../server/mutators.js | 46 +++++++++++++++++++ .../server/resolvers.js | 7 ++- .../server/typeDefs.graphql | 20 ++++++-- 5 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 plugins/talk-plugin-notifications/server/mutators.js diff --git a/plugins/talk-plugin-notifications-category-reply/index.js b/plugins/talk-plugin-notifications-category-reply/index.js index cbbb9171f..32d90e05d 100644 --- a/plugins/talk-plugin-notifications-category-reply/index.js +++ b/plugins/talk-plugin-notifications-category-reply/index.js @@ -33,20 +33,19 @@ const handle = async (ctx, comment) => { { comment_id: parentID } ); if (reply.errors) { - console.error(reply.errors); + ctx.log.error({ err: reply.errors }, 'could not query for author metadata'); return; } - // TODO: re-enable. - // // Check if the user has notifications enabled. - // const enabled = get( - // reply, - // 'data.comment.user.notificationSettings.onReply', - // false - // ); - // if (!enabled) { - // return; - // } + // Check if the user has notifications enabled. + const enabled = get( + reply, + 'data.comment.user.notificationSettings.onReply', + false + ); + if (!enabled) { + return; + } const userID = get(reply, 'data.comment.user.id', null); if (!userID) { @@ -105,6 +104,21 @@ const hydrate = async (ctx, category, context) => { const handler = { handle, category: 'reply', event: 'commentAdded', hydrate }; module.exports = { + typeDefs: ` + type NotificationSettings { + onReply: Boolean! + } + + input NotificationSettingsInput { + onReply: Boolean + } + `, + resolvers: { + NotificationSettings: { + // onReply returns false by default if not specified. + onReply: settings => get(settings, 'onReply', false), + }, + }, translations: path.join(__dirname, 'translations.yml'), notifications: [handler], }; diff --git a/plugins/talk-plugin-notifications/server/index.js b/plugins/talk-plugin-notifications/server/index.js index c1f17e7c8..0c7e993b9 100644 --- a/plugins/talk-plugin-notifications/server/index.js +++ b/plugins/talk-plugin-notifications/server/index.js @@ -3,12 +3,14 @@ const connect = require('./connect'); const typeDefs = require('./typeDefs'); const resolvers = require('./resolvers'); const router = require('./router'); +const mutators = require('./mutators'); const translations = path.join(__dirname, 'translations.yml'); module.exports = { translations, typeDefs, resolvers, + mutators, connect, router, }; diff --git a/plugins/talk-plugin-notifications/server/mutators.js b/plugins/talk-plugin-notifications/server/mutators.js new file mode 100644 index 000000000..5c0db9c8d --- /dev/null +++ b/plugins/talk-plugin-notifications/server/mutators.js @@ -0,0 +1,46 @@ +const { reduce, isNull, isEmpty } = require('lodash'); + +/** + * Reduce the settings to dotize the settings. + */ +function reduceSettings(newSettings, newValue, key) { + if (!isNull(newValue)) { + newSettings[`metadata.notifications.settings.${key}`] = newValue; + } + + return newSettings; +} + +/** + * Update the user notification settings. + */ +async function updateNotificationSettings(ctx, settings) { + const { connectors: { models: { User } }, user } = ctx; + + // Generate the settings set object, and just exit if we haven't changed + // anything. + const $set = reduce(settings, reduceSettings, {}); + if (isEmpty($set)) { + return; + } + + // Update the user. + return User.updateOne({ id: user.id }, { $set }); +} + +module.exports = ctx => { + let mutators = { + User: { + updateNotificationSettings: () => + Promise.reject(ctx.connectors.errors.ErrNotAuthorized), + }, + }; + + if (ctx.user) { + // TODO: check to see if the user is verified? + mutators.User.updateNotificationSettings = settings => + updateNotificationSettings(ctx, settings); + } + + return mutators; +}; diff --git a/plugins/talk-plugin-notifications/server/resolvers.js b/plugins/talk-plugin-notifications/server/resolvers.js index c03c59e88..0254361de 100644 --- a/plugins/talk-plugin-notifications/server/resolvers.js +++ b/plugins/talk-plugin-notifications/server/resolvers.js @@ -7,8 +7,13 @@ module.exports = { currentUser && (currentUser.id === user.id || currentUser.can('VIEW_USER_STATUS')) ) { - return get(user, 'metadata.notifications.settings'); + return get(user, 'metadata.notifications.settings', {}); } }, }, + RootMutation: { + async updateNotificationSettings(obj, { input }, { mutators: { User } }) { + await User.updateNotificationSettings(input); + }, + }, }; diff --git a/plugins/talk-plugin-notifications/server/typeDefs.graphql b/plugins/talk-plugin-notifications/server/typeDefs.graphql index 3d53ec1c1..6b2a43b2e 100644 --- a/plugins/talk-plugin-notifications/server/typeDefs.graphql +++ b/plugins/talk-plugin-notifications/server/typeDefs.graphql @@ -1,7 +1,21 @@ -type NotificationSettings { - onReply: Boolean! -} +# NotificationSettings stores all the preferences related to notifications. +type NotificationSettings { } type User { notificationSettings: NotificationSettings +} + +type UpdateNotificationSettingsResponse implements Response { + # An array of errors relating to the mutation that occurred. + errors: [UserError!] +} + +input NotificationSettingsInput { + +} + +type RootMutation { + # updateNotificationSettings will update the current user's notification + # settings. + updateNotificationSettings(input: NotificationSettingsInput!): UpdateNotificationSettingsResponse } \ No newline at end of file