2020-05-27 02:56:11 +00:00
|
|
|
const md = require('markdown-it')
|
|
|
|
const mdEmoji = require('markdown-it-emoji')
|
|
|
|
const { JSDOM } = require('jsdom')
|
|
|
|
const createDOMPurify = require('dompurify')
|
|
|
|
const _ = require('lodash')
|
|
|
|
const { AkismetClient } = require('akismet-api')
|
2020-05-31 19:54:20 +00:00
|
|
|
const moment = require('moment')
|
2020-05-27 02:56:11 +00:00
|
|
|
|
2020-05-17 02:46:05 +00:00
|
|
|
/* global WIKI */
|
|
|
|
|
2020-05-27 02:56:11 +00:00
|
|
|
const window = new JSDOM('').window
|
|
|
|
const DOMPurify = createDOMPurify(window)
|
|
|
|
|
|
|
|
let akismetClient = null
|
|
|
|
|
2020-05-31 22:15:15 +00:00
|
|
|
const mkdown = md({
|
|
|
|
html: false,
|
|
|
|
breaks: true,
|
|
|
|
linkify: true,
|
|
|
|
highlight(str, lang) {
|
|
|
|
return `<pre><code class="language-${lang}">${_.escape(str)}</code></pre>`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mkdown.use(mdEmoji)
|
|
|
|
|
2020-05-17 02:46:05 +00:00
|
|
|
// ------------------------------------
|
|
|
|
// Default Comment Provider
|
|
|
|
// ------------------------------------
|
|
|
|
|
|
|
|
module.exports = {
|
2020-05-27 02:56:11 +00:00
|
|
|
/**
|
|
|
|
* Init
|
|
|
|
*/
|
|
|
|
async init (config) {
|
2020-05-30 20:34:09 +00:00
|
|
|
WIKI.logger.info('(COMMENTS/DEFAULT) Initializing...')
|
2020-05-27 02:56:11 +00:00
|
|
|
if (WIKI.data.commentProvider.config.akismet && WIKI.data.commentProvider.config.akismet.length > 2) {
|
|
|
|
akismetClient = new AkismetClient({
|
|
|
|
key: WIKI.data.commentProvider.config.akismet,
|
|
|
|
blog: WIKI.config.host,
|
|
|
|
lang: WIKI.config.lang.namespacing ? WIKI.config.lang.namespaces.join(', ') : WIKI.config.lang.code,
|
|
|
|
charset: 'UTF-8'
|
|
|
|
})
|
|
|
|
try {
|
|
|
|
const isValid = await akismetClient.verifyKey()
|
|
|
|
if (!isValid) {
|
2020-05-30 20:34:09 +00:00
|
|
|
akismetClient = null
|
|
|
|
WIKI.logger.warn('(COMMENTS/DEFAULT) Akismet Key is invalid! [ DISABLED ]')
|
|
|
|
} else {
|
|
|
|
WIKI.logger.info('(COMMENTS/DEFAULT) Akismet key is valid. [ OK ]')
|
2020-05-27 02:56:11 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2020-05-30 20:34:09 +00:00
|
|
|
akismetClient = null
|
|
|
|
WIKI.logger.warn('(COMMENTS/DEFAULT) Unable to verify Akismet Key: ' + err.message)
|
2020-05-27 02:56:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
akismetClient = null
|
|
|
|
}
|
2020-05-30 20:34:09 +00:00
|
|
|
WIKI.logger.info('(COMMENTS/DEFAULT) Initialization completed.')
|
2020-05-27 02:56:11 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Create New Comment
|
|
|
|
*/
|
|
|
|
async create ({ page, replyTo, content, user }) {
|
|
|
|
// -> Build New Comment
|
|
|
|
const newComment = {
|
|
|
|
content,
|
|
|
|
render: DOMPurify.sanitize(mkdown.render(content)),
|
|
|
|
replyTo,
|
|
|
|
pageId: page.id,
|
|
|
|
authorId: user.id,
|
|
|
|
name: user.name,
|
|
|
|
email: user.email,
|
|
|
|
ip: user.ip
|
|
|
|
}
|
|
|
|
|
2020-05-31 06:13:41 +00:00
|
|
|
// -> Check for Spam with Akismet
|
2020-05-27 02:56:11 +00:00
|
|
|
if (akismetClient) {
|
|
|
|
let userRole = 'user'
|
|
|
|
if (user.groups.indexOf(1) >= 0) {
|
|
|
|
userRole = 'administrator'
|
|
|
|
} else if (user.groups.indexOf(2) >= 0) {
|
|
|
|
userRole = 'guest'
|
|
|
|
}
|
|
|
|
|
|
|
|
let isSpam = false
|
|
|
|
try {
|
|
|
|
isSpam = await akismetClient.checkSpam({
|
|
|
|
ip: user.ip,
|
|
|
|
useragent: user.agentagent,
|
|
|
|
content,
|
|
|
|
name: user.name,
|
|
|
|
email: user.email,
|
|
|
|
permalink: `${WIKI.config.host}/${page.localeCode}/${page.path}`,
|
|
|
|
permalinkDate: page.updatedAt,
|
|
|
|
type: (replyTo > 0) ? 'reply' : 'comment',
|
|
|
|
role: userRole
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.warn('Akismet Comment Validation: [ FAILED ]')
|
|
|
|
WIKI.logger.warn(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSpam) {
|
|
|
|
throw new Error('Comment was rejected because it is marked as spam.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 19:54:20 +00:00
|
|
|
// -> Check for minimum delay between posts
|
|
|
|
if (WIKI.data.commentProvider.config.minDelay > 0) {
|
|
|
|
const lastComment = await WIKI.models.comments.query().select('updatedAt').findOne('authorId', user.id).orderBy('updatedAt', 'desc')
|
|
|
|
if (lastComment && moment().subtract(WIKI.data.commentProvider.config.minDelay, 'seconds').isBefore(lastComment.updatedAt)) {
|
|
|
|
throw new Error('Your administrator has set a time limit before you can post another comment. Try again later.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 06:13:41 +00:00
|
|
|
// -> Save Comment to DB
|
|
|
|
const cm = await WIKI.models.comments.query().insert(newComment)
|
|
|
|
|
|
|
|
// -> Return Comment ID
|
|
|
|
return cm.id
|
2020-05-27 02:56:11 +00:00
|
|
|
},
|
2020-05-31 22:15:15 +00:00
|
|
|
/**
|
|
|
|
* Update an existing comment
|
|
|
|
*/
|
|
|
|
async update ({ id, content, user }) {
|
|
|
|
const renderedContent = DOMPurify.sanitize(mkdown.render(content))
|
|
|
|
await WIKI.models.comments.query().findById(id).patch({
|
|
|
|
render: renderedContent
|
|
|
|
})
|
|
|
|
return renderedContent
|
2020-05-27 02:56:11 +00:00
|
|
|
},
|
2020-05-31 06:13:41 +00:00
|
|
|
/**
|
|
|
|
* Delete an existing comment by ID
|
|
|
|
*/
|
2020-05-31 22:15:15 +00:00
|
|
|
async remove ({ id, user }) {
|
2020-05-31 06:13:41 +00:00
|
|
|
return WIKI.models.comments.query().findById(id).delete()
|
2020-05-27 02:56:11 +00:00
|
|
|
},
|
2020-05-31 06:13:41 +00:00
|
|
|
/**
|
|
|
|
* Get the page ID from a comment ID
|
|
|
|
*/
|
|
|
|
async getPageIdFromCommentId (id) {
|
|
|
|
const result = await WIKI.models.comments.query().select('pageId').findById(id)
|
|
|
|
return (result) ? result.pageId : false
|
|
|
|
},
|
2020-05-31 22:15:15 +00:00
|
|
|
/**
|
|
|
|
* Get a comment by ID
|
|
|
|
*/
|
|
|
|
async getCommentById (id) {
|
|
|
|
return WIKI.models.comments.query().findById(id)
|
|
|
|
},
|
2020-05-31 06:13:41 +00:00
|
|
|
/**
|
|
|
|
* Get the total comments count for a page ID
|
|
|
|
*/
|
|
|
|
async count (pageId) {
|
|
|
|
const result = await WIKI.models.comments.query().count('* as total').where('pageId', pageId).first()
|
|
|
|
return _.toSafeInteger(result.total)
|
2020-05-17 02:46:05 +00:00
|
|
|
}
|
|
|
|
}
|