You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.8 KiB

  1. const _ = require('lodash')
  2. const graphHelper = require('../../helpers/graph')
  3. /* global WIKI */
  4. module.exports = {
  5. Query: {
  6. async comments() { return {} }
  7. },
  8. Mutation: {
  9. async comments() { return {} }
  10. },
  11. CommentQuery: {
  12. /**
  13. * Fetch list of Comments Providers
  14. */
  15. async providers(obj, args, context, info) {
  16. const providers = await WIKI.models.commentProviders.getProviders()
  17. return providers.map(provider => {
  18. const providerInfo = _.find(WIKI.data.commentProviders, ['key', provider.key]) || {}
  19. return {
  20. ...providerInfo,
  21. ...provider,
  22. config: _.sortBy(_.transform(provider.config, (res, value, key) => {
  23. const configData = _.get(providerInfo.props, key, false)
  24. if (configData) {
  25. res.push({
  26. key,
  27. value: JSON.stringify({
  28. ...configData,
  29. value
  30. })
  31. })
  32. }
  33. }, []), 'key')
  34. }
  35. })
  36. },
  37. /**
  38. * Fetch list of comments for a page
  39. */
  40. async list (obj, args, context) {
  41. const page = await WIKI.models.pages.query().select('id').findOne({ localeCode: args.locale, path: args.path })
  42. if (page) {
  43. if (WIKI.auth.checkAccess(context.req.user, ['read:comments'], args)) {
  44. const comments = await WIKI.models.comments.query().where('pageId', page.id).orderBy('createdAt')
  45. return comments.map(c => ({
  46. ...c,
  47. authorName: c.name,
  48. authorEmail: c.email,
  49. authorIP: c.ip
  50. }))
  51. } else {
  52. throw new WIKI.Error.CommentViewForbidden()
  53. }
  54. } else {
  55. return []
  56. }
  57. },
  58. /**
  59. * Fetch a single comment
  60. */
  61. async single (obj, args, context) {
  62. const cm = await WIKI.data.commentProvider.getCommentById(args.id)
  63. if (!cm || !cm.pageId) {
  64. throw new WIKI.Error.CommentNotFound()
  65. }
  66. const page = await WIKI.models.pages.query().select('localeCode', 'path').findById(cm.pageId)
  67. if (page) {
  68. if (WIKI.auth.checkAccess(context.req.user, ['read:comments'], {
  69. path: page.path,
  70. locale: page.localeCode
  71. })) {
  72. return {
  73. ...cm,
  74. authorName: cm.name,
  75. authorEmail: cm.email,
  76. authorIP: cm.ip
  77. }
  78. } else {
  79. throw new WIKI.Error.CommentViewForbidden()
  80. }
  81. } else {
  82. WIKI.logger.warn(`Comment #${cm.id} is linked to a page #${cm.pageId} that doesn't exist! [ERROR]`)
  83. throw new WIKI.Error.CommentGenericError()
  84. }
  85. }
  86. },
  87. CommentMutation: {
  88. /**
  89. * Create New Comment
  90. */
  91. async create (obj, args, context) {
  92. try {
  93. const cmId = await WIKI.models.comments.postNewComment({
  94. ...args,
  95. user: context.req.user,
  96. ip: context.req.ip
  97. })
  98. return {
  99. responseResult: graphHelper.generateSuccess('New comment posted successfully'),
  100. id: cmId
  101. }
  102. } catch (err) {
  103. return graphHelper.generateError(err)
  104. }
  105. },
  106. /**
  107. * Update an Existing Comment
  108. */
  109. async update (obj, args, context) {
  110. try {
  111. const cmRender = await WIKI.models.comments.updateComment({
  112. ...args,
  113. user: context.req.user,
  114. ip: context.req.ip
  115. })
  116. return {
  117. responseResult: graphHelper.generateSuccess('Comment updated successfully'),
  118. render: cmRender
  119. }
  120. } catch (err) {
  121. return graphHelper.generateError(err)
  122. }
  123. },
  124. /**
  125. * Delete an Existing Comment
  126. */
  127. async delete (obj, args, context) {
  128. try {
  129. await WIKI.models.comments.deleteComment({
  130. id: args.id,
  131. user: context.req.user,
  132. ip: context.req.ip
  133. })
  134. return {
  135. responseResult: graphHelper.generateSuccess('Comment deleted successfully')
  136. }
  137. } catch (err) {
  138. return graphHelper.generateError(err)
  139. }
  140. },
  141. /**
  142. * Update Comments Providers
  143. */
  144. async updateProviders(obj, args, context) {
  145. try {
  146. for (let provider of args.providers) {
  147. await WIKI.models.commentProviders.query().patch({
  148. isEnabled: provider.isEnabled,
  149. config: _.reduce(provider.config, (result, value, key) => {
  150. _.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
  151. return result
  152. }, {})
  153. }).where('key', provider.key)
  154. }
  155. await WIKI.models.commentProviders.initProvider()
  156. return {
  157. responseResult: graphHelper.generateSuccess('Comment Providers updated successfully')
  158. }
  159. } catch (err) {
  160. return graphHelper.generateError(err)
  161. }
  162. }
  163. }
  164. }