Added more mutators

This commit is contained in:
Wyatt Johnson
2017-01-19 10:39:29 -07:00
parent 2d414a5ab7
commit d683b09b1d
5 changed files with 175 additions and 19 deletions
+80 -8
View File
@@ -1,20 +1,22 @@
const errors = require('../../../errors');
const Action = require('../../../models/action');
const Asset = require('../../../models/asset');
const Comment = require('../../../models/comment');
const User = require('../../../models/user');
const Wordlist = require('../../../services/wordlist');
/**
* Creates a new comment.
* @param {Object} context a GraphQL context
* @param {Object} user the user performing the request
* @param {String} body body of the comment
* @param {String} asset_id asset for the comment
* @param {String} parent_id optional parent of the comment
* @param {Object} [wordlist={}] results for the wordlist analysis
* @return {Promise} resolves to the created comment
*/
const createComment = (context, {body, asset_id, parent_id = null}, wordlist = {}) => {
const createComment = ({user}, {body, asset_id, parent_id = null}, wordlist = {}) => {
// Decide the status based on whether or not the current asset/settings
// has pre-mod enabled or not. If the comment was rejected based on the
@@ -58,7 +60,7 @@ const createComment = (context, {body, asset_id, parent_id = null}, wordlist = {
asset_id,
parent_id,
status,
author_id: context.req.user.id
author_id: user.id
}))
.then((comment) => {
if (wordlist.suspect) {
@@ -86,8 +88,78 @@ const filterNewComment = (context, {body}) => {
return wl.load().then(() => wl.scan('body', body));
};
module.exports = (context) => ({
createComment: (comment) => filterNewComment(context, comment).then((wordlist) => {
return createComment(context, comment, wordlist);
})
});
/**
* Creates an action on a item.
* @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
*/
const createAction = ({user}, {item_id, item_type, action_type}) => {
return Action.insertUserAction({
item_id,
item_type,
user_id: user.id,
action_type
});
};
/**
* Deletes an action based on the user id if the user owns that action.
* @param {Object} user the user performing the request
* @param {[type]} id [description]
* @return {[type]} [description]
*/
const deleteAction = ({user}, {id}) => {
return Action.remove({
id,
user_id: user.id
});
};
/**
* Updates a users settings.
* @param {[type]} user [description]
* @param {[type]} bio [description]
* @return {[type]} [description]
*/
const updateUserSettings = ({user}, {bio}) => {
return User.updateSettings(user.id, {bio});
};
module.exports = (context) => {
// TODO: refactor to something that'll return an error in the event an attempt
// is made to mutate state while not logged in. There's got to be a better way
// to do this.
if (context.user) {
return {
Comment: {
create: (comment) => filterNewComment(context, comment).then((wordlist) => {
return createComment(context, comment, wordlist);
})
},
Action: {
create: (action) => createAction(context, action),
delete: (action) => deleteAction(context, action)
},
User: {
updateSettings: (settings) => updateUserSettings(context, settings)
}
};
}
return {
Comment: {
create: () => {}
},
Action: {
create: () => {},
delete: () => {}
},
User: {
updateSettings: () => {}
}
};
};