2
0

comment.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. let akismetClient = null
  11. // ------------------------------------
  12. // Default Comment Provider
  13. // ------------------------------------
  14. module.exports = {
  15. /**
  16. * Init
  17. */
  18. async init (config) {
  19. WIKI.logger.info('(COMMENTS/DEFAULT) Initializing...')
  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. akismetClient = null
  31. WIKI.logger.warn('(COMMENTS/DEFAULT) Akismet Key is invalid! [ DISABLED ]')
  32. } else {
  33. WIKI.logger.info('(COMMENTS/DEFAULT) Akismet key is valid. [ OK ]')
  34. }
  35. } catch (err) {
  36. akismetClient = null
  37. WIKI.logger.warn('(COMMENTS/DEFAULT) Unable to verify Akismet Key: ' + err.message)
  38. }
  39. } else {
  40. akismetClient = null
  41. }
  42. WIKI.logger.info('(COMMENTS/DEFAULT) Initialization completed.')
  43. },
  44. /**
  45. * Create New Comment
  46. */
  47. async create ({ page, replyTo, content, user }) {
  48. // -> Render Markdown
  49. const mkdown = md({
  50. html: false,
  51. breaks: true,
  52. linkify: true,
  53. highlight(str, lang) {
  54. return `<pre><code class="language-${lang}">${_.escape(str)}</code></pre>`
  55. }
  56. })
  57. mkdown.use(mdEmoji)
  58. // -> Build New Comment
  59. const newComment = {
  60. content,
  61. render: DOMPurify.sanitize(mkdown.render(content)),
  62. replyTo,
  63. pageId: page.id,
  64. authorId: user.id,
  65. name: user.name,
  66. email: user.email,
  67. ip: user.ip
  68. }
  69. // Check for Spam with Akismet
  70. if (akismetClient) {
  71. let userRole = 'user'
  72. if (user.groups.indexOf(1) >= 0) {
  73. userRole = 'administrator'
  74. } else if (user.groups.indexOf(2) >= 0) {
  75. userRole = 'guest'
  76. }
  77. let isSpam = false
  78. try {
  79. isSpam = await akismetClient.checkSpam({
  80. ip: user.ip,
  81. useragent: user.agentagent,
  82. content,
  83. name: user.name,
  84. email: user.email,
  85. permalink: `${WIKI.config.host}/${page.localeCode}/${page.path}`,
  86. permalinkDate: page.updatedAt,
  87. type: (replyTo > 0) ? 'reply' : 'comment',
  88. role: userRole
  89. })
  90. } catch (err) {
  91. WIKI.logger.warn('Akismet Comment Validation: [ FAILED ]')
  92. WIKI.logger.warn(err)
  93. }
  94. if (isSpam) {
  95. throw new Error('Comment was rejected because it is marked as spam.')
  96. }
  97. }
  98. // Save Comment
  99. await WIKI.models.comments.query().insert(newComment)
  100. },
  101. async update ({ id, content, user, ip }) {
  102. },
  103. async remove ({ id, user, ip }) {
  104. },
  105. async count ({ pageId }) {
  106. }
  107. }