replaced eslint:recommended with prettier

This commit is contained in:
Wyatt Johnson
2018-01-11 20:00:34 -07:00
parent d56c19016a
commit 0abc2ca243
649 changed files with 16235 additions and 13008 deletions
+25 -47
View File
@@ -1,8 +1,6 @@
const errors = require('../../errors');
const {CREATE_ACTION, DELETE_ACTION} = require('../../perms/constants');
const {
IGNORE_FLAGS_AGAINST_STAFF,
} = require('../../config');
const { CREATE_ACTION, DELETE_ACTION } = require('../../perms/constants');
const { IGNORE_FLAGS_AGAINST_STAFF } = require('../../config');
/**
* getActionItem will return the item that is associated with the given action.
@@ -12,21 +10,16 @@ const {
* @param {Object} action the action being performed
* @return {Promise} resolves to the referenced item
*/
const getActionItem = async (ctx, {item_id, item_type}) => {
const {
loaders: {
Comments,
Users,
},
} = ctx;
const getActionItem = async (ctx, { item_id, item_type }) => {
const { loaders: { Comments, Users } } = ctx;
switch (item_type) {
case 'COMMENTS':
return Comments.get.load(item_id);
case 'USERS':
return Users.getByID.load(item_id);
default:
return null;
case 'COMMENTS':
return Comments.get.load(item_id);
case 'USERS':
return Users.getByID.load(item_id);
default:
return null;
}
};
@@ -38,19 +31,14 @@ const getActionItem = async (ctx, {item_id, item_type}) => {
* @param {Object} action the action being created
* @return {Promise} resolves to the action created
*/
const createAction = async (ctx, {item_id, item_type, action_type, group_id, metadata = {}}) => {
const {
user = {},
pubsub,
connectors: {
services: {
Actions,
},
},
} = ctx;
const createAction = async (
ctx,
{ item_id, item_type, action_type, group_id, metadata = {} }
) => {
const { user = {}, pubsub, connectors: { services: { Actions } } } = ctx;
// Gets the item referenced by the action.
const item = await getActionItem(ctx, {item_id, item_type});
const item = await getActionItem(ctx, { item_id, item_type });
if (!item || item === null) {
throw errors.ErrNotFound;
}
@@ -59,7 +47,6 @@ const createAction = async (ctx, {item_id, item_type, action_type, group_id, met
// staff member.
if (IGNORE_FLAGS_AGAINST_STAFF) {
if (action_type === 'FLAG') {
// If the item is a user, and this is a flag. Check to see if they are
// staff, if they are, don't permit the flag.
if (item_type === 'USERS' && item.isStaff()) {
@@ -69,7 +56,6 @@ const createAction = async (ctx, {item_id, item_type, action_type, group_id, met
}
if (action_type === 'FLAG' && item_type === 'USERS') {
// The item is a user, and this is a flag. Check to see if they are staff,
// if they are, don't permit the flag.
if (item.isStaff()) {
@@ -84,11 +70,10 @@ const createAction = async (ctx, {item_id, item_type, action_type, group_id, met
user_id: user.id,
group_id,
action_type,
metadata
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);
@@ -104,33 +89,26 @@ const createAction = async (ctx, {item_id, item_type, action_type, group_id, met
* @param {String} id the id of the action to delete
* @return {Promise} resolves to the deleted action, or null if not found.
*/
const deleteAction = (ctx, {id}) => {
const {
user,
connectors: {
services: {
Actions,
},
},
} = ctx;
const deleteAction = (ctx, { id }) => {
const { user, connectors: { services: { Actions } } } = ctx;
return Actions.delete({id, user_id: user.id});
return Actions.delete({ id, user_id: user.id });
};
module.exports = (ctx) => {
module.exports = ctx => {
let mutators = {
Action: {
create: () => Promise.reject(errors.ErrNotAuthorized),
delete: () => Promise.reject(errors.ErrNotAuthorized)
}
delete: () => Promise.reject(errors.ErrNotAuthorized),
},
};
if (ctx.user && ctx.user.can(CREATE_ACTION)) {
mutators.Action.create = (action) => createAction(ctx, action);
mutators.Action.create = action => createAction(ctx, action);
}
if (ctx.user && ctx.user.can(DELETE_ACTION)) {
mutators.Action.delete = (action) => deleteAction(ctx, action);
mutators.Action.delete = action => deleteAction(ctx, action);
}
return mutators;
+21 -14
View File
@@ -14,7 +14,8 @@ const AssetModel = require('../../models/asset');
* @param {String} id the asset's id to update
* @param {Object} settings the settings to update on the asset.
*/
const updateSettings = async (ctx, id, settings) => AssetsService.overrideSettings(id, settings);
const updateSettings = async (ctx, id, settings) =>
AssetsService.overrideSettings(id, settings);
/**
* updateStatus will update the status of an asset.
@@ -24,30 +25,36 @@ const updateSettings = async (ctx, id, settings) => AssetsService.overrideSettin
* @param {Object} status the status to change on the asset relating to it's
* current state.
*/
const updateStatus = async (ctx, id, {closedAt, closedMessage}) => AssetModel.update({
id,
}, {
$set: {
closedAt,
closedMessage
}
});
const updateStatus = async (ctx, id, { closedAt, closedMessage }) =>
AssetModel.update(
{
id,
},
{
$set: {
closedAt,
closedMessage,
},
}
);
module.exports = (ctx) => {
module.exports = ctx => {
let mutators = {
Asset: {
updateSettings: () => Promise.reject(errors.ErrNotAuthorized),
updateStatus: () => Promise.reject(errors.ErrNotAuthorized)
}
updateStatus: () => Promise.reject(errors.ErrNotAuthorized),
},
};
if (ctx.user) {
if (ctx.user.can(UPDATE_ASSET_SETTINGS)) {
mutators.Asset.updateSettings = (id, settings) => updateSettings(ctx, id, settings);
mutators.Asset.updateSettings = (id, settings) =>
updateSettings(ctx, id, settings);
}
if (ctx.user.can(UPDATE_ASSET_STATUS)) {
mutators.Asset.updateStatus = (id, status) => updateStatus(ctx, id, status);
mutators.Asset.updateStatus = (id, status) =>
updateStatus(ctx, id, status);
}
}
+158 -139
View File
@@ -12,7 +12,7 @@ const {
CREATE_COMMENT,
SET_COMMENT_STATUS,
ADD_COMMENT_TAG,
EDIT_COMMENT
EDIT_COMMENT,
} = require('../../perms/constants');
const debug = require('debug')('talk:graph:mutators:comment');
const {
@@ -20,26 +20,30 @@ const {
IGNORE_FLAGS_AGAINST_STAFF,
} = require('../../config');
const resolveTagsForComment = async ({user, loaders: {Tags}}, {asset_id, tags = []}) => {
const resolveTagsForComment = async (
{ user, loaders: { Tags } },
{ asset_id, tags = [] }
) => {
const item_type = 'COMMENTS';
// Handle Tags
if (tags.length) {
// Get the global list of tags from the dataloader.
let globalTags = await Tags.getAll.load({
item_type,
asset_id
asset_id,
});
if (!Array.isArray(globalTags)) {
globalTags = [];
}
// Merge in the tags for the given comment.
tags = tags.map((name) => {
tags = tags.map(name => {
// Resolve the TagLink that we can use for the comment.
let {tagLink} = TagsService.resolveLink(user, globalTags, {name, item_type});
let { tagLink } = TagsService.resolveLink(user, globalTags, {
name,
item_type,
});
// Return the tagLink for tag insertion.
return tagLink;
@@ -48,10 +52,12 @@ const resolveTagsForComment = async ({user, loaders: {Tags}}, {asset_id, tags =
// Add the staff tag for comments created as a staff member.
if (user.can(ADD_COMMENT_TAG)) {
tags.push(TagsService.newTagLink(user, {
name: 'STAFF',
item_type
}));
tags.push(
TagsService.newTagLink(user, {
name: 'STAFF',
item_type,
})
);
}
return tags;
@@ -63,14 +69,9 @@ const resolveTagsForComment = async ({user, loaders: {Tags}}, {asset_id, tags =
*/
const adjustKarma = (Comments, id, status) => async () => {
try {
// Use the dataloader to get the comment that was just moderated and
// get the flag user's id's so we can adjust their karma too.
let [
comment,
flagUserIDs
] = await Promise.all([
let [comment, flagUserIDs] = await Promise.all([
// Load the comment that was just made/updated by the setCommentStatus
// operation.
Comments.get.load(id),
@@ -80,51 +81,53 @@ const adjustKarma = (Comments, id, status) => async () => {
ActionModel.find({
item_id: id,
item_type: 'COMMENTS',
action_type: 'FLAG'
}).then((actions) => {
action_type: 'FLAG',
}).then(actions => {
// This is to ensure that this is always an array.
if (!actions) {
return [];
}
return actions.map(({user_id}) => user_id);
})
return actions.map(({ user_id }) => user_id);
}),
]);
debug(`Comment[${id}] by User[${comment.author_id}] was Status[${status}]`);
switch (status) {
case 'REJECTED':
case 'REJECTED':
// Reduce the user's karma.
debug(`CommentUser[${comment.author_id}] had their karma reduced`);
// Reduce the user's karma.
debug(`CommentUser[${comment.author_id}] had their karma reduced`);
// Decrease the flag user's karma, the moderator disagreed with this
// action.
debug(
`FlaggingUser[${flagUserIDs.join(', ')}] had their karma increased`
);
await Promise.all([
KarmaService.modifyUser(comment.author_id, -1, 'comment'),
KarmaService.modifyUser(flagUserIDs, 1, 'flag', true),
]);
// Decrease the flag user's karma, the moderator disagreed with this
// action.
debug(`FlaggingUser[${flagUserIDs.join(', ')}] had their karma increased`);
await Promise.all([
KarmaService.modifyUser(comment.author_id, -1, 'comment'),
KarmaService.modifyUser(flagUserIDs, 1, 'flag', true)
]);
break;
break;
case 'ACCEPTED':
// Increase the user's karma.
debug(`CommentUser[${comment.author_id}] had their karma increased`);
case 'ACCEPTED':
// Increase the user's karma.
debug(`CommentUser[${comment.author_id}] had their karma increased`);
// Increase the flag user's karma, the moderator agreed with this
// action.
debug(`FlaggingUser[${flagUserIDs.join(', ')}] had their karma reduced`);
await Promise.all([
KarmaService.modifyUser(comment.author_id, 1, 'comment'),
KarmaService.modifyUser(flagUserIDs, -1, 'flag', true)
]);
break;
// Increase the flag user's karma, the moderator agreed with this
// action.
debug(
`FlaggingUser[${flagUserIDs.join(', ')}] had their karma reduced`
);
await Promise.all([
KarmaService.modifyUser(comment.author_id, 1, 'comment'),
KarmaService.modifyUser(flagUserIDs, -1, 'flag', true),
]);
break;
default:
return;
}
return;
@@ -142,11 +145,21 @@ const adjustKarma = (Comments, id, status) => async () => {
* @param {String} [status='NONE'] the status of the new comment
* @return {Promise} resolves to the created comment
*/
const createComment = async (context, {tags = [], body, asset_id, parent_id = null, status = 'NONE', metadata = {}}) => {
const {user, loaders: {Comments}, pubsub} = context;
const createComment = async (
context,
{
tags = [],
body,
asset_id,
parent_id = null,
status = 'NONE',
metadata = {},
}
) => {
const { user, loaders: { Comments }, pubsub } = context;
// Resolve the tags for the comment.
tags = await resolveTagsForComment(context, {asset_id, tags});
tags = await resolveTagsForComment(context, { asset_id, tags });
let comment = await CommentsService.publicCreate({
body,
@@ -182,13 +195,9 @@ const createComment = async (context, {tags = [], body, asset_id, parent_id = nu
* @param {String} [asset_id] id of asset comment is posted on
* @return {Object} resolves to the wordlist results
*/
const filterNewComment = async (context, {body, asset_id}) => {
const filterNewComment = async (context, { body, asset_id }) => {
// Load the settings.
const [
settings,
asset,
] = await Promise.all([
const [settings, asset] = await Promise.all([
context.loaders.Settings.load(),
context.loaders.Assets.getByID.load(asset_id),
]);
@@ -201,12 +210,11 @@ const filterNewComment = async (context, {body, asset_id}) => {
// Load the wordlist and filter the comment content.
return [
// Scan the word.
wl.scan('body', body),
// Return the asset's settings.
await AssetsService.rectifySettings(asset, settings)
await AssetsService.rectifySettings(asset, settings),
];
};
@@ -215,10 +223,8 @@ const filterNewComment = async (context, {body, asset_id}) => {
* returned.
*/
const moderationPhases = [
// This phase checks to see if the comment is long enough.
(context, comment) => {
// Check to see if the body is too short, if it is, then complain about it!
if (comment.body.length < 2) {
throw errors.ErrCommentTooShort;
@@ -226,8 +232,7 @@ const moderationPhases = [
},
// This phase checks to see if the asset being processed is closed or not.
(context, comment, {asset}) => {
(context, comment, { asset }) => {
// Check to see if the asset has closed commenting...
if (asset.isClosed) {
throw new errors.ErrAssetCommentingClosed(asset.closedMessage);
@@ -235,23 +240,23 @@ const moderationPhases = [
},
// This phase checks the comment against the wordlist.
(context, comment, {wordlist}) => {
(context, comment, { 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
// wordlist, then reject it, otherwise if the moderation setting is
// premod, set it to `premod`.
if (wordlist.banned) {
// Add the flag related to Trust to the comment.
return {
status: 'REJECTED',
actions: [{
action_type: 'FLAG',
user_id: null,
group_id: 'BANNED_WORD',
metadata: {}
}]
actions: [
{
action_type: 'FLAG',
user_id: null,
group_id: 'BANNED_WORD',
metadata: {},
},
],
};
}
@@ -262,44 +267,45 @@ const moderationPhases = [
// If the wordlist has matched the suspect word filter and we haven't disabled
// auto-flagging suspect words, then we should flag the comment!
if (wordlist.suspect && !DISABLE_AUTOFLAG_SUSPECT_WORDS) {
// TODO: this is kind of fragile, we should refactor this to resolve
// all these const's that we're using like 'COMMENTS', 'FLAG' to be
// defined in a checkable schema.
return {
actions: [{
action_type: 'FLAG',
user_id: null,
group_id: 'SUSPECT_WORD',
metadata: {}
}],
actions: [
{
action_type: 'FLAG',
user_id: null,
group_id: 'SUSPECT_WORD',
metadata: {},
},
],
};
}
},
// This phase checks to see if the comment's length exceeds maximum.
(context, comment, {assetSettings: {charCountEnable, charCount}}) => {
(context, comment, { assetSettings: { charCountEnable, charCount } }) => {
// Reject if the comment is too long
if (charCountEnable && comment.body.length > charCount) {
// Add the flag related to Trust to the comment.
return {
status: 'REJECTED',
actions: [{
action_type: 'FLAG',
user_id: null,
group_id: 'BODY_COUNT',
metadata: {
count: comment.body.length,
}
}]
actions: [
{
action_type: 'FLAG',
user_id: null,
group_id: 'BODY_COUNT',
metadata: {
count: comment.body.length,
},
},
],
};
}
},
// If a given user is a staff member, always approve their comment.
(context) => {
context => {
if (IGNORE_FLAGS_AGAINST_STAFF && context.user && context.user.isStaff()) {
return {
status: 'ACCEPTED',
@@ -309,48 +315,52 @@ const moderationPhases = [
// This phase checks the comment if it has any links in it if the check is
// enabled.
(context, comment, {assetSettings: {premodLinksEnable}}) => {
(context, comment, { assetSettings: { premodLinksEnable } }) => {
if (premodLinksEnable && linkify.test(comment.body)) {
// Add the flag related to Trust to the comment.
return {
status: 'SYSTEM_WITHHELD',
actions: [{
action_type: 'FLAG',
user_id: null,
group_id: 'LINKS',
metadata: {
links: comment.body,
}
}],
actions: [
{
action_type: 'FLAG',
user_id: null,
group_id: 'LINKS',
metadata: {
links: comment.body,
},
},
],
};
}
},
// This phase checks to see if the user making the comment is allowed to do so
// considering their reliability (Trust) status.
(context) => {
context => {
if (context.user && context.user.metadata) {
// If the user is not a reliable commenter (passed the unreliability
// threshold by having too many rejected comments) then we can change the
// status of the comment to `SYSTEM_WITHHELD`, therefore pushing the user's
// comments away from the public eye until a moderator can manage them. This of
// course can only be applied if the comment's current status is `NONE`,
// we don't want to interfere if the comment was rejected.
if (KarmaService.isReliable('comment', context.user.metadata.trust) === false) {
if (
KarmaService.isReliable('comment', context.user.metadata.trust) ===
false
) {
// Add the flag related to Trust to the comment.
return {
status: 'SYSTEM_WITHHELD',
actions: [{
action_type: 'FLAG',
user_id: null,
group_id: 'TRUST',
metadata: {
trust: context.user.metadata.trust,
}
}],
actions: [
{
action_type: 'FLAG',
user_id: null,
group_id: 'TRUST',
metadata: {
trust: context.user.metadata.trust,
},
},
],
};
}
}
@@ -358,7 +368,6 @@ const moderationPhases = [
// This phase checks to see if the comment was already prescribed a status.
(context, comment) => {
// If the status was already defined, don't redefine it. It's only defined
// when specific external conditions exist, we don't want to override that.
if (comment.status && comment.status.length > 0) {
@@ -370,8 +379,7 @@ const moderationPhases = [
// This phase checks to see if the settings have premod enabled, if they do,
// the comment is premod, otherwise, it's just none.
(context, comment, {assetSettings: {moderation}}) => {
(context, comment, { assetSettings: { moderation } }) => {
// If the settings say that we're in premod mode, then the comment is in
// premod status.
if (moderation === 'PRE') {
@@ -383,7 +391,7 @@ const moderationPhases = [
return {
status: 'NONE',
};
}
},
];
/**
@@ -395,7 +403,6 @@ const moderationPhases = [
* @return {Promise} resolves to the comment's status and actions
*/
const resolveCommentModeration = async (context, comment) => {
// First we filter the comment contents to ensure that we note any validation
// issues.
let [wordlist, settings] = await filterNewComment(context, comment);
@@ -403,7 +410,6 @@ const resolveCommentModeration = async (context, comment) => {
// Get the asset from the loader.
const asset = await context.loaders.Assets.getByID.load(comment.asset_id);
if (!asset) {
// And leave now if this asset wasn't found.
throw errors.ErrNotFound;
}
@@ -423,7 +429,6 @@ const resolveCommentModeration = async (context, comment) => {
});
if (result) {
if (result.actions) {
actions.push(...result.actions);
}
@@ -431,7 +436,7 @@ const resolveCommentModeration = async (context, comment) => {
// If this result contained a status, then we've finished resolving
// phases!
if (result.status) {
return {status: result.status, actions};
return { status: result.status, actions };
}
}
}
@@ -446,11 +451,10 @@ const resolveCommentModeration = async (context, comment) => {
* @return {Promise} resolves to a new comment
*/
const createPublicComment = async (context, comment) => {
// We then take the wordlist and the comment into consideration when
// considering what status to assign the new comment, and resolve the new
// status to set the comment to.
let {actions, status} = await resolveCommentModeration(context, comment);
let { actions, status } = await resolveCommentModeration(context, comment);
// Assign status to comment.
comment.status = status;
@@ -468,10 +472,17 @@ const createPublicComment = async (context, comment) => {
// createActions will for each of the provided actions, create the given action
// on the comment at the same time using Promise.all.
const createActions = async (item_id, actions = []) => Promise.all(actions.map((action) => merge(action, {
item_id,
item_type: 'COMMENTS',
})).map((action) => ActionsService.create(action)));
const createActions = async (item_id, actions = []) =>
Promise.all(
actions
.map(action =>
merge(action, {
item_id,
item_type: 'COMMENTS',
})
)
.map(action => ActionsService.create(action))
);
/**
* Sets the status of a comment
@@ -480,8 +491,12 @@ const createActions = async (item_id, actions = []) => Promise.all(actions.map((
* @param {String} id identifier of the comment (uuid)
* @param {String} status the new status of the comment
*/
const setStatus = async ({user, loaders: {Comments}}, {id, status}) => {
let comment = await CommentsService.pushStatus(id, status, user ? user.id : null);
const setStatus = async ({ user, loaders: { Comments } }, { id, status }) => {
let comment = await CommentsService.pushStatus(
id,
status,
user ? user.id : null
);
// If the loaders are present, clear the caches for these values because we
// just added a new comment, hence the counts should be updated. It would
@@ -507,17 +522,21 @@ const setStatus = async ({user, loaders: {Comments}}, {id, status}) => {
* @param {Object} edit describes how to edit the comment
* @param {String} edit.body the new Comment body
*/
const edit = async (context, {id, asset_id, edit: {body}}) => {
const edit = async (context, { id, asset_id, edit: { body } }) => {
// Build up the new comment we're setting. We need to check this with
// moderation now.
let comment = {id, asset_id, body};
let comment = { id, asset_id, body };
// Determine the new status of the comment.
const {actions, status} = await resolveCommentModeration(context, comment);
const { actions, status } = await resolveCommentModeration(context, comment);
// Execute the edit.
comment = await CommentsService.edit({id, author_id: context.user.id, body, status});
comment = await CommentsService.edit({
id,
author_id: context.user.id,
body,
status,
});
// Create all the actions that were determined during the moderation check
// phase.
@@ -529,25 +548,25 @@ const edit = async (context, {id, asset_id, edit: {body}}) => {
return comment;
};
module.exports = (context) => {
module.exports = context => {
let mutators = {
Comment: {
create: () => Promise.reject(errors.ErrNotAuthorized),
setStatus: () => Promise.reject(errors.ErrNotAuthorized),
edit: () => Promise.reject(errors.ErrNotAuthorized)
}
edit: () => Promise.reject(errors.ErrNotAuthorized),
},
};
if (context.user && context.user.can(CREATE_COMMENT)) {
mutators.Comment.create = (comment) => createPublicComment(context, comment);
mutators.Comment.create = comment => createPublicComment(context, comment);
}
if (context.user && context.user.can(SET_COMMENT_STATUS)) {
mutators.Comment.setStatus = (action) => setStatus(context, action);
mutators.Comment.setStatus = action => setStatus(context, action);
}
if (context.user && context.user.can(EDIT_COMMENT)) {
mutators.Comment.edit = (action) => edit(context, action);
mutators.Comment.edit = action => edit(context, action);
}
return mutators;
+11 -13
View File
@@ -12,7 +12,6 @@ const User = require('./user');
const plugins = require('../../services/plugins');
let mutators = [
// Load in the core mutators.
Comment,
Action,
@@ -23,12 +22,11 @@ let mutators = [
User,
// Load the plugin mutators from the manager.
...plugins
.get('server', 'mutators').map(({plugin, mutators}) => {
debug(`added plugin '${plugin.name}'`);
...plugins.get('server', 'mutators').map(({ plugin, mutators }) => {
debug(`added plugin '${plugin.name}'`);
return mutators;
})
return mutators;
}),
];
/**
@@ -36,12 +34,12 @@ let mutators = [
* @param {Object} context the context of the GraphQL request
* @return {Object} object of mutators
*/
module.exports = (context) => {
module.exports = context => {
// We need to return an object to be accessed.
return _.merge(...mutators.map((mutators) => {
// Each set of mutators is a function which takes the context.
return mutators(context);
}));
return _.merge(
...mutators.map(mutators => {
// Each set of mutators is a function which takes the context.
return mutators(context);
})
);
};
+3 -5
View File
@@ -1,18 +1,16 @@
const errors = require('../../errors');
const {
UPDATE_SETTINGS,
} = require('../../perms/constants');
const { UPDATE_SETTINGS } = require('../../perms/constants');
const SettingsService = require('../../services/settings');
const update = async (ctx, settings) => SettingsService.update(settings);
module.exports = (ctx) => {
module.exports = ctx => {
let mutators = {
Settings: {
update: () => Promise.reject(errors.ErrNotAuthorized),
}
},
};
if (ctx.user) {
+19 -10
View File
@@ -1,38 +1,47 @@
const TagsService = require('../../services/tags');
const errors = require('../../errors');
const {ADD_COMMENT_TAG, REMOVE_COMMENT_TAG} = require('../../perms/constants');
const {
ADD_COMMENT_TAG,
REMOVE_COMMENT_TAG,
} = require('../../perms/constants');
/**
* Modifies the targeted model with the specified operation to add/remove a tag.
*/
const modify = async ({user, loaders: {Tags}}, operation, {name, id, item_type, asset_id}) => {
const modify = async (
{ user, loaders: { Tags } },
operation,
{ name, id, item_type, asset_id }
) => {
// Get the global list of tags from the dataloader.
const tags = await Tags.getAll.load({id, item_type, asset_id});
const tags = await Tags.getAll.load({ id, item_type, asset_id });
// Resolve the TagLink that should be used to insert to the user. This will
// additionally return with an ownership property that can be used to determine
// that the user who adds this tag must also be the owner of the resource.
let {tagLink, ownership} = TagsService.resolveLink(user, tags, {name, item_type});
let { tagLink, ownership } = TagsService.resolveLink(user, tags, {
name,
item_type,
});
// Actually modify the tag on the model.
return operation(id, item_type, tagLink, ownership);
};
module.exports = (context) => {
module.exports = context => {
let mutators = {
Tag: {
add: () => Promise.reject(errors.ErrNotAuthorized),
remove: () => Promise.reject(errors.ErrNotAuthorized)
}
remove: () => Promise.reject(errors.ErrNotAuthorized),
},
};
if (context.user && context.user.can(ADD_COMMENT_TAG)) {
mutators.Tag.add = (tag) => modify(context, TagsService.add, tag);
mutators.Tag.add = tag => modify(context, TagsService.add, tag);
}
if (context.user && context.user.can(REMOVE_COMMENT_TAG)) {
mutators.Tag.remove = (tag) => modify(context, TagsService.remove, tag);
mutators.Tag.remove = tag => modify(context, TagsService.remove, tag);
}
return mutators;
+9 -12
View File
@@ -1,13 +1,10 @@
const errors = require('../../errors');
const TokensService = require('../../services/tokens');
const {
CREATE_TOKEN,
REVOKE_TOKEN
} = require('../../perms/constants');
const { CREATE_TOKEN, REVOKE_TOKEN } = require('../../perms/constants');
// Creates a new token for a user.
const createToken = async ({user}, {name}) => {
let {pat, jwt} = await TokensService.create(user.id, name);
const createToken = async ({ user }, { name }) => {
let { pat, jwt } = await TokensService.create(user.id, name);
// Attach the token to the PAT.
pat.jwt = jwt;
@@ -17,24 +14,24 @@ const createToken = async ({user}, {name}) => {
};
// Revokes the token from the user.
const revokeToken = async ({user}, {id}) => {
const revokeToken = async ({ user }, { id }) => {
return TokensService.revoke(user.id, id);
};
module.exports = (context) => {
module.exports = context => {
let mutators = {
Token: {
create: () => Promise.reject(errors.ErrNotAuthorized),
revoke: () => Promise.reject(errors.ErrNotAuthorized)
}
revoke: () => Promise.reject(errors.ErrNotAuthorized),
},
};
if (context.user && context.user.can(CREATE_TOKEN)) {
mutators.Token.create = (input) => createToken(context, input);
mutators.Token.create = input => createToken(context, input);
}
if (context.user && context.user.can(REVOKE_TOKEN)) {
mutators.Token.revoke = (input) => revokeToken(context, input);
mutators.Token.revoke = input => revokeToken(context, input);
}
return mutators;
+34 -14
View File
@@ -19,24 +19,39 @@ const setUserUsernameStatus = async (ctx, id, status) => {
};
const setUserBanStatus = async (ctx, id, status = false, message = null) => {
const user = await UsersService.setBanStatus(id, status, ctx.user.id, message);
const user = await UsersService.setBanStatus(
id,
status,
ctx.user.id,
message
);
if (user.banned) {
ctx.pubsub.publish('userBanned', user);
}
};
const setUserSuspensionStatus = async (ctx, id, until = null, message = null) => {
const user = await UsersService.setSuspensionStatus(id, until, ctx.user.id, message);
const setUserSuspensionStatus = async (
ctx,
id,
until = null,
message = null
) => {
const user = await UsersService.setSuspensionStatus(
id,
until,
ctx.user.id,
message
);
if (user.suspended) {
ctx.pubsub.publish('userSuspended', user);
}
};
const ignoreUser = ({user}, userToIgnore) => {
const ignoreUser = ({ user }, userToIgnore) => {
return UsersService.ignoreUsers(user.id, [userToIgnore.id]);
};
const stopIgnoringUser = ({user}, userToStopIgnoring) => {
const stopIgnoringUser = ({ user }, userToStopIgnoring) => {
return UsersService.stopIgnoringUsers(user.id, [userToStopIgnoring.id]);
};
@@ -52,7 +67,7 @@ const setRole = (ctx, id, role) => {
return UsersService.setRole(id, role);
};
module.exports = (ctx) => {
module.exports = ctx => {
let mutators = {
User: {
changeUsername: () => Promise.reject(errors.ErrNotAuthorized),
@@ -63,35 +78,40 @@ module.exports = (ctx) => {
setUserUsernameStatus: () => Promise.reject(errors.ErrNotAuthorized),
setUsername: () => Promise.reject(errors.ErrNotAuthorized),
stopIgnoringUser: () => Promise.reject(errors.ErrNotAuthorized),
}
},
};
if (ctx.user) {
mutators.User.ignoreUser = (action) => ignoreUser(ctx, action);
mutators.User.stopIgnoringUser = (action) => stopIgnoringUser(ctx, action);
mutators.User.ignoreUser = action => ignoreUser(ctx, action);
mutators.User.stopIgnoringUser = action => stopIgnoringUser(ctx, action);
if (ctx.user.can(UPDATE_USER_ROLES)) {
mutators.User.setRole = (id, role) => setRole(ctx, id, role);
}
if (ctx.user.can(CHANGE_USERNAME)) {
mutators.User.changeUsername = (id, username) => changeUsername(ctx, id, username);
mutators.User.changeUsername = (id, username) =>
changeUsername(ctx, id, username);
}
if (ctx.user.can(SET_USERNAME)) {
mutators.User.setUsername = (id, username) => setUsername(ctx, id, username);
mutators.User.setUsername = (id, username) =>
setUsername(ctx, id, username);
}
if (ctx.user.can(SET_USER_USERNAME_STATUS)) {
mutators.User.setUserUsernameStatus = (id, status) => setUserUsernameStatus(ctx, id, status);
mutators.User.setUserUsernameStatus = (id, status) =>
setUserUsernameStatus(ctx, id, status);
}
if (ctx.user.can(SET_USER_BAN_STATUS)) {
mutators.User.setUserBanStatus = (id, status, message) => setUserBanStatus(ctx, id, status, message);
mutators.User.setUserBanStatus = (id, status, message) =>
setUserBanStatus(ctx, id, status, message);
}
if (ctx.user.can(SET_USER_SUSPENSION_STATUS)) {
mutators.User.setUserSuspensionStatus = (id, until, message) => setUserSuspensionStatus(ctx, id, until, message);
mutators.User.setUserSuspensionStatus = (id, until, message) =>
setUserSuspensionStatus(ctx, id, until, message);
}
}