mirror of https://github.com/requarks/wiki
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.
100 lines
3.4 KiB
100 lines
3.4 KiB
2 years ago
|
import { Model } from 'objection'
|
||
|
import fs from 'node:fs/promises'
|
||
|
import path from 'node:path'
|
||
|
import { defaultTo, find, forOwn, isBoolean, replace, sortBy } from 'lodash-es'
|
||
|
import yaml from 'js-yaml'
|
||
|
import { parseModuleProps } from '../helpers/common.mjs'
|
||
5 years ago
|
|
||
|
/**
|
||
|
* CommentProvider model
|
||
|
*/
|
||
2 years ago
|
export class CommentProvider extends Model {
|
||
5 years ago
|
static get tableName() { return 'commentProviders' }
|
||
|
static get idColumn() { return 'key' }
|
||
|
|
||
|
static get jsonSchema () {
|
||
|
return {
|
||
|
type: 'object',
|
||
|
required: ['key', 'isEnabled'],
|
||
|
|
||
|
properties: {
|
||
|
key: {type: 'string'},
|
||
|
isEnabled: {type: 'boolean'}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static get jsonAttributes() {
|
||
|
return ['config']
|
||
|
}
|
||
|
|
||
|
static async getProvider(key) {
|
||
2 years ago
|
return WIKI.db.commentProviders.query().findOne({ key })
|
||
5 years ago
|
}
|
||
|
|
||
|
static async getProviders(isEnabled) {
|
||
2 years ago
|
const providers = await WIKI.db.commentProviders.query().where(isBoolean(isEnabled) ? { isEnabled } : {})
|
||
|
return sortBy(providers, ['module'])
|
||
5 years ago
|
}
|
||
|
|
||
|
static async refreshProvidersFromDisk() {
|
||
|
try {
|
||
|
// -> Fetch definitions from disk
|
||
3 years ago
|
const commentsDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/comments'))
|
||
|
WIKI.data.commentProviders = []
|
||
|
for (const dir of commentsDirs) {
|
||
5 years ago
|
const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/comments', dir, 'definition.yml'), 'utf8')
|
||
3 years ago
|
const defParsed = yaml.load(def)
|
||
|
defParsed.key = dir
|
||
2 years ago
|
defParsed.props = parseModuleProps(defParsed.props)
|
||
3 years ago
|
WIKI.data.commentProviders.push(defParsed)
|
||
|
WIKI.logger.debug(`Loaded comments provider module definition ${dir}: [ OK ]`)
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
WIKI.logger.info(`Loaded ${WIKI.data.commentProviders.length} comments providers module definitions: [ OK ]`)
|
||
5 years ago
|
} catch (err) {
|
||
3 years ago
|
WIKI.logger.error(`Failed to scan or load comments providers: [ FAILED ]`)
|
||
5 years ago
|
WIKI.logger.error(err)
|
||
|
}
|
||
|
}
|
||
5 years ago
|
|
||
|
static async initProvider() {
|
||
2 years ago
|
const commentProvider = await WIKI.db.commentProviders.query().findOne('isEnabled', true)
|
||
5 years ago
|
if (commentProvider) {
|
||
|
WIKI.data.commentProvider = {
|
||
2 years ago
|
...find(WIKI.data.commentProviders, ['key', commentProvider.module]),
|
||
5 years ago
|
head: '',
|
||
|
bodyStart: '',
|
||
|
bodyEnd: '',
|
||
|
main: '<comments></comments>'
|
||
|
}
|
||
|
|
||
|
if (WIKI.data.commentProvider.codeTemplate) {
|
||
|
const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/comments', commentProvider.key, 'code.yml'), 'utf8')
|
||
|
let code = yaml.safeLoad(def)
|
||
2 years ago
|
code.head = defaultTo(code.head, '')
|
||
|
code.body = defaultTo(code.body, '')
|
||
|
code.main = defaultTo(code.main, '')
|
||
5 years ago
|
|
||
2 years ago
|
forOwn(commentProvider.config, (value, key) => {
|
||
|
code.head = replace(code.head, new RegExp(`{{${key}}}`, 'g'), value)
|
||
|
code.body = replace(code.body, new RegExp(`{{${key}}}`, 'g'), value)
|
||
|
code.main = replace(code.main, new RegExp(`{{${key}}}`, 'g'), value)
|
||
5 years ago
|
})
|
||
|
|
||
|
WIKI.data.commentProvider.head = code.head
|
||
|
WIKI.data.commentProvider.body = code.body
|
||
|
WIKI.data.commentProvider.main = code.main
|
||
|
} else {
|
||
|
WIKI.data.commentProvider = {
|
||
|
...WIKI.data.commentProvider,
|
||
2 years ago
|
...(await import(`../modules/comments/${commentProvider.key}/comment.mjs`)),
|
||
4 years ago
|
config: commentProvider.config
|
||
5 years ago
|
}
|
||
4 years ago
|
await WIKI.data.commentProvider.init()
|
||
5 years ago
|
}
|
||
|
WIKI.data.commentProvider.config = commentProvider.config
|
||
|
}
|
||
|
}
|
||
5 years ago
|
}
|