Ocelot-Social/src/middleware/softDeleteMiddleware.js
Robert Schäfer 7d82b27aaa Hide disabled comments and posts by default
I had to trick neo4j-graphql-js to return disabled and deleted fields by
default on any neo4j node. If disabled is not queried, then we don't
have it in our middleware and we're not able to filter it.
2019-03-13 21:09:15 +01:00

44 lines
1.1 KiB
JavaScript

const isModerator = ({ user }) => {
return user && (user.role === 'moderator' || user.role === 'admin')
}
const setDefaultFilters = (resolve, root, args, context, info) => {
if (typeof args.deleted !== 'boolean') {
args.deleted = false
}
if (!isModerator(context)) {
args.disabled = false
}
return resolve(root, args, context, info)
}
const hideDisabledComments = async (resolve, root, args, context, info) => {
const { comments } = root
if (!Array.isArray(comments)) return resolve(root, args, context, info)
if (!isModerator(context)) {
root.comments = comments.filter((comment) => {
return !comment.disabled
})
}
return resolve(root, args, context, info)
}
export default {
Query: {
Post: setDefaultFilters,
Comment: setDefaultFilters,
User: setDefaultFilters
},
Mutation: async (resolve, root, args, context, info) => {
args.disabled = false
// TODO: remove as soon as our factories don't need this anymore
if (typeof args.deleted !== 'boolean') {
args.deleted = false
}
return resolve(root, args, context, info)
},
Post: hideDisabledComments,
User: hideDisabledComments
}