added mutations

This commit is contained in:
Wyatt Johnson
2018-02-12 19:02:27 -07:00
parent 5221ebbe9a
commit 5d1eff0ddb
5 changed files with 96 additions and 15 deletions
@@ -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],
};
@@ -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,
};
@@ -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;
};
@@ -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);
},
},
};
@@ -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
}