comment.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const md = require('markdown-it')
  2. const mdEmoji = require('markdown-it-emoji')
  3. const { JSDOM } = require('jsdom')
  4. const createDOMPurify = require('dompurify')
  5. const _ = require('lodash')
  6. const { AkismetClient } = require('akismet-api')
  7. /* global WIKI */
  8. const window = new JSDOM('').window
  9. const DOMPurify = createDOMPurify(window)
  10. md.use(mdEmoji)
  11. let akismetClient = null
  12. // ------------------------------------
  13. // Default Comment Provider
  14. // ------------------------------------
  15. module.exports = {
  16. /**
  17. * Init
  18. */
  19. async init (config) {
  20. if (WIKI.data.commentProvider.config.akismet && WIKI.data.commentProvider.config.akismet.length > 2) {
  21. akismetClient = new AkismetClient({
  22. key: WIKI.data.commentProvider.config.akismet,
  23. blog: WIKI.config.host,
  24. lang: WIKI.config.lang.namespacing ? WIKI.config.lang.namespaces.join(', ') : WIKI.config.lang.code,
  25. charset: 'UTF-8'
  26. })
  27. try {
  28. const isValid = await akismetClient.verifyKey()
  29. if (!isValid) {
  30. WIKI.logger.warn('Akismet Key is invalid!')
  31. }
  32. } catch (err) {
  33. WIKI.logger.warn('Unable to verify Akismet Key: ' + err.message)
  34. }
  35. } else {
  36. akismetClient = null
  37. }
  38. },
  39. /**
  40. * Create New Comment
  41. */
  42. async create ({ page, replyTo, content, user }) {
  43. // -> Render Markdown
  44. const mkdown = md({
  45. html: false,
  46. breaks: true,
  47. linkify: true,
  48. highlight(str, lang) {
  49. return `<pre><code class="language-${lang}">${_.escape(str)}</code></pre>`
  50. }
  51. })
  52. // -> Build New Comment
  53. const newComment = {
  54. content,
  55. render: DOMPurify.sanitize(mkdown.render(content)),
  56. replyTo,
  57. pageId: page.id,
  58. authorId: user.id,
  59. name: user.name,
  60. email: user.email,
  61. ip: user.ip
  62. }
  63. // Check for Spam with Akismet
  64. if (akismetClient) {
  65. let userRole = 'user'
  66. if (user.groups.indexOf(1) >= 0) {
  67. userRole = 'administrator'
  68. } else if (user.groups.indexOf(2) >= 0) {
  69. userRole = 'guest'
  70. }
  71. let isSpam = false
  72. try {
  73. isSpam = await akismetClient.checkSpam({
  74. ip: user.ip,
  75. useragent: user.agentagent,
  76. content,
  77. name: user.name,
  78. email: user.email,
  79. permalink: `${WIKI.config.host}/${page.localeCode}/${page.path}`,
  80. permalinkDate: page.updatedAt,
  81. type: (replyTo > 0) ? 'reply' : 'comment',
  82. role: userRole
  83. })
  84. } catch (err) {
  85. WIKI.logger.warn('Akismet Comment Validation: [ FAILED ]')
  86. WIKI.logger.warn(err)
  87. }
  88. if (isSpam) {
  89. throw new Error('Comment was rejected because it is marked as spam.')
  90. }
  91. }
  92. // Save Comment
  93. await WIKI.models.comments.query().insert(newComment)
  94. },
  95. async update ({ id, content, user, ip }) {
  96. },
  97. async remove ({ id, user, ip }) {
  98. },
  99. async count ({ pageId }) {
  100. }
  101. }