Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actions Metadata #297

Merged
merged 7 commits into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added new action rep, refactored mutations
  • Loading branch information
wyattjoh committed Feb 9, 2017
commit fb9bedc57b805e3ab14fb285a53445e47661cc9c
10 changes: 10 additions & 0 deletions graph/loaders/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ const util = require('./util');
const ActionsService = require('../../services/actions');
const ActionModel = require('../../models/action');

/**
* Gets actions based on their item id's.
*/
const genActionsByItemID = (_, item_ids) => {
return ActionsService
.findByItemIdArray(item_ids)
.then(util.arrayJoinBy(item_ids, 'item_id'));
};

/**
* Looks up actions based on the requested id's all bounded by the user.
* @param {Object} context the context of the request
Expand Down Expand Up @@ -35,6 +44,7 @@ const getItemIdsByActionTypeAndItemType = (_, action_type, item_type) => {
*/
module.exports = (context) => ({
Actions: {
getByID: new DataLoader((ids) => genActionsByItemID(context, ids)),
getSummariesByItemID: new DataLoader((ids) => genActionSummariessByItemID(context, ids)),
getByTypes: ({action_type, item_type}) => getItemIdsByActionTypeAndItemType(context, action_type, item_type)
}
Expand Down
3 changes: 2 additions & 1 deletion graph/mutators/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ const UsersService = require('../../services/users');
* @param {String} action_type type of the action
* @return {Promise} resolves to the action created
*/
const createAction = ({user = {}}, {item_id, item_type, action_type, metadata = {}}) => {
const createAction = ({user = {}}, {item_id, item_type, action_type, group_id, metadata = {}}) => {
return ActionsService.insertUserAction({
item_id,
item_type,
user_id: user.id,
group_id,
action_type,
metadata
}).then((action) => {
Expand Down
8 changes: 8 additions & 0 deletions graph/resolvers/action.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
const Action = {
__resolveType({action_type}) {
switch (action_type) {
case 'FLAG':
return 'FlagAction';
case 'LIKE':
return 'LikeAction';
}
},

// This will load the user for the specific action. We'll limit this to the
// admin users only or the current logged in user.
Expand Down
11 changes: 10 additions & 1 deletion graph/resolvers/action_summary.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
const ActionSummary = {};
const ActionSummary = {
__resolveType({action_type}) {
switch (action_type) {
case 'FLAG':
return 'FlagActionSummary';
case 'LIKE':
return 'LikeActionSummary';
}
},
};

module.exports = ActionSummary;
11 changes: 10 additions & 1 deletion graph/resolvers/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ const Comment = {
replyCount({id}, _, {loaders: {Comments}}) {
return Comments.countByParentID.load(id);
},
actions({id}, _, {loaders: {Actions}}) {
actions({id}, _, {user, loaders: {Actions}}) {

// Only return the actions if the user is not an admin.
if (user && user.hasRoles('ADMIN')) {
return Actions.getByID.load(id);
}

return null;
},
action_summaries({id}, _, {loaders: {Actions}}) {
return Actions.getSummariesByItemID.load(id);
},
asset({asset_id}, _, {loaders: {Assets}}) {
Expand Down
9 changes: 9 additions & 0 deletions graph/resolvers/flag_action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const FlagAction = {

// Stored in the metadata, extract and return.
reason({metadata: {reason}}) {
return reason;
}
};

module.exports = FlagAction;
7 changes: 7 additions & 0 deletions graph/resolvers/flag_action_summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const FlagActionSummary = {
reason({group_id}) {
return group_id;
}
};

module.exports = FlagActionSummary;
3 changes: 3 additions & 0 deletions graph/resolvers/generic_user_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const GenericUserError = {};

module.exports = GenericUserError;
18 changes: 15 additions & 3 deletions graph/resolvers/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
const Action = require('./action');
const ActionSummary = require('./action_summary');
const Action = require('./action');
const Asset = require('./asset');
const Comment = require('./comment');
const Date = require('./date');
const FlagActionSummary = require('./flag_action_summary');
const FlagAction = require('./flag_action');
const GenericUserError = require('./generic_user_error');
const LikeAction = require('./like_action');
const RootMutation = require('./root_mutation');
const RootQuery = require('./root_query');
const Settings = require('./settings');
const UserError = require('./user_error');
const User = require('./user');
const ValidationUserError = require('./validation_user_error');

module.exports = {
Action,
ActionSummary,
Action,
Asset,
Comment,
Date,
FlagActionSummary,
FlagAction,
GenericUserError,
LikeAction,
RootMutation,
RootQuery,
Settings,
User
UserError,
User,
ValidationUserError,
};
5 changes: 5 additions & 0 deletions graph/resolvers/like_action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const LikeAction = {

};

module.exports = LikeAction;
27 changes: 23 additions & 4 deletions graph/resolvers/root_mutation.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
/**
* Wraps up a promise to return an object with the resolution of the promise
* keyed at `key` or an error caught at `errors`.
*/
const wrapResponse = (key) => (promise) => {
return promise.then((value) => {
let res = {};
if (key) {
res[key] = value;
}
return res;
}).catch((err) => ({
errors: [err]
}));
};

const RootMutation = {
createComment(_, {asset_id, parent_id, body}, {mutators: {Comment}}) {
return Comment.create({asset_id, parent_id, body});
return wrapResponse('comment')(Comment.create({asset_id, parent_id, body}));
},
createLike(_, {like: {item_id, item_type}}, {mutators: {Action}}) {
return wrapResponse('like')(Action.create({item_id, item_type, action_type: 'LIKE'}));
},
createAction(_, {action}, {mutators: {Action}}) {
return Action.create(action);
createFlag(_, {flag: {item_id, item_type, reason, message}}, {mutators: {Action}}) {
return wrapResponse('flag')(Action.create({item_id, item_type, action_type: 'FLAG', group_id: reason, metadata: {message}}));
},
deleteAction(_, {id}, {mutators: {Action}}) {
return Action.delete({id});
return wrapResponse(null)(Action.delete({id}));
},
};

Expand Down
10 changes: 9 additions & 1 deletion graph/resolvers/user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const User = {
actions({id}, _, {loaders: {Actions}}) {
action_summaries({id}, _, {loaders: {Actions}}) {
return Actions.getSummariesByItemID.load(id);
},
actions({id}, _, {user, loaders: {Actions}}) {

// Only return the actions if the user is not an admin.
if (user && user.hasRoles('ADMIN')) {
return Actions.getByID.load(id);
}

},
comments({id}, _, {loaders: {Comments}, user}) {

// If the user is not an admin, only return comment list for the owner of
Expand Down
11 changes: 11 additions & 0 deletions graph/resolvers/user_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const UserError = {
__resolveType({field_name}) {
if (field_name) {
return 'ValidationUserError';
}

return 'GenericUserError';
}
};

module.exports = UserError;
3 changes: 3 additions & 0 deletions graph/resolvers/validation_user_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const ValidationUserError = {};

module.exports = ValidationUserError;
7 changes: 7 additions & 0 deletions graph/schema.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
const tools = require('graphql-tools');
const maskErrors = require('graphql-errors').maskErrors;

const resolvers = require('./resolvers');
const typeDefs = require('./typeDefs');

const schema = tools.makeExecutableSchema({typeDefs, resolvers});

if (process.env.NODE_ENV === 'production') {

// Mask errors that are thrown if we are in a production environment.
maskErrors(schema);
}

module.exports = schema;
Loading