wikijs-fork/server/graph/resolvers/comment.js

176 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

2020-04-07 02:21:42 +00:00
const _ = require('lodash')
const graphHelper = require('../../helpers/graph')
/* global WIKI */
module.exports = {
2020-04-07 02:21:42 +00:00
Query: {
async comments() { return {} }
},
Mutation: {
async comments() { return {} }
},
CommentQuery: {
/**
* Fetch list of Comments Providers
*/
2020-04-07 02:21:42 +00:00
async providers(obj, args, context, info) {
const providers = await WIKI.models.commentProviders.getProviders()
return providers.map(provider => {
const providerInfo = _.find(WIKI.data.commentProviders, ['key', provider.key]) || {}
return {
...providerInfo,
...provider,
config: _.sortBy(_.transform(provider.config, (res, value, key) => {
const configData = _.get(providerInfo.props, key, false)
if (configData) {
res.push({
key,
value: JSON.stringify({
...configData,
value
})
})
}
}, []), 'key')
}
})
2020-05-23 22:49:10 +00:00
},
/**
* Fetch list of comments for a page
*/
2020-05-23 22:49:10 +00:00
async list (obj, args, context) {
const page = await WIKI.models.pages.query().select('pages.id').findOne({ localeCode: args.locale, path: args.path })
.withGraphJoined('tags')
.modifyGraph('tags', builder => {
builder.select('tag')
})
if (page) {
if (WIKI.auth.checkAccess(context.req.user, ['read:comments'], { tags: page.tags, ...args })) {
2020-05-31 22:15:15 +00:00
const comments = await WIKI.models.comments.query().where('pageId', page.id).orderBy('createdAt')
return comments.map(c => ({
...c,
authorName: c.name,
authorEmail: c.email,
authorIP: c.ip
}))
} else {
2020-05-31 22:15:15 +00:00
throw new WIKI.Error.CommentViewForbidden()
}
} else {
return []
}
2020-05-31 22:15:15 +00:00
},
/**
* Fetch a single comment
*/
async single (obj, args, context) {
const cm = await WIKI.data.commentProvider.getCommentById(args.id)
if (!cm || !cm.pageId) {
throw new WIKI.Error.CommentNotFound()
}
const page = await WIKI.models.pages.query().select('localeCode', 'path').findById(cm.pageId)
.withGraphJoined('tags')
.modifyGraph('tags', builder => {
builder.select('tag')
})
2020-05-31 22:15:15 +00:00
if (page) {
if (WIKI.auth.checkAccess(context.req.user, ['read:comments'], {
path: page.path,
locale: page.localeCode,
tags: page.tags
2020-05-31 22:15:15 +00:00
})) {
return {
...cm,
authorName: cm.name,
authorEmail: cm.email,
authorIP: cm.ip
}
} else {
throw new WIKI.Error.CommentViewForbidden()
}
} else {
WIKI.logger.warn(`Comment #${cm.id} is linked to a page #${cm.pageId} that doesn't exist! [ERROR]`)
throw new WIKI.Error.CommentGenericError()
}
2020-04-07 02:21:42 +00:00
}
},
CommentMutation: {
/**
* Create New Comment
*/
async create (obj, args, context) {
try {
const cmId = await WIKI.models.comments.postNewComment({
...args,
user: context.req.user,
ip: context.req.ip
})
return {
responseResult: graphHelper.generateSuccess('New comment posted successfully'),
id: cmId
}
} catch (err) {
return graphHelper.generateError(err)
}
},
2020-05-31 22:15:15 +00:00
/**
* Update an Existing Comment
*/
async update (obj, args, context) {
try {
const cmRender = await WIKI.models.comments.updateComment({
...args,
user: context.req.user,
ip: context.req.ip
})
return {
responseResult: graphHelper.generateSuccess('Comment updated successfully'),
render: cmRender
}
} catch (err) {
return graphHelper.generateError(err)
}
},
/**
* Delete an Existing Comment
*/
async delete (obj, args, context) {
try {
await WIKI.models.comments.deleteComment({
id: args.id,
user: context.req.user,
ip: context.req.ip
})
return {
responseResult: graphHelper.generateSuccess('Comment deleted successfully')
}
} catch (err) {
return graphHelper.generateError(err)
}
},
/**
* Update Comments Providers
*/
2020-04-07 02:21:42 +00:00
async updateProviders(obj, args, context) {
try {
for (let provider of args.providers) {
2020-05-17 02:46:05 +00:00
await WIKI.models.commentProviders.query().patch({
2020-04-07 02:21:42 +00:00
isEnabled: provider.isEnabled,
config: _.reduce(provider.config, (result, value, key) => {
_.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
return result
}, {})
}).where('key', provider.key)
}
2020-05-17 02:46:05 +00:00
await WIKI.models.commentProviders.initProvider()
2020-04-07 02:21:42 +00:00
return {
responseResult: graphHelper.generateSuccess('Comment Providers updated successfully')
}
} catch (err) {
return graphHelper.generateError(err)
}
}
}
}