commentProviders.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const Model = require('objection').Model
  2. const fs = require('fs-extra')
  3. const path = require('path')
  4. const _ = require('lodash')
  5. const yaml = require('js-yaml')
  6. const commonHelper = require('../helpers/common')
  7. /* global WIKI */
  8. /**
  9. * CommentProvider model
  10. */
  11. module.exports = class CommentProvider extends Model {
  12. static get tableName() { return 'commentProviders' }
  13. static get idColumn() { return 'key' }
  14. static get jsonSchema () {
  15. return {
  16. type: 'object',
  17. required: ['key', 'isEnabled'],
  18. properties: {
  19. key: {type: 'string'},
  20. isEnabled: {type: 'boolean'}
  21. }
  22. }
  23. }
  24. static get jsonAttributes() {
  25. return ['config']
  26. }
  27. static async getProvider(key) {
  28. return WIKI.models.commentProviders.query().findOne({ key })
  29. }
  30. static async getProviders(isEnabled) {
  31. const providers = await WIKI.models.commentProviders.query().where(_.isBoolean(isEnabled) ? { isEnabled } : {})
  32. return _.sortBy(providers, ['module'])
  33. }
  34. static async refreshProvidersFromDisk() {
  35. try {
  36. // -> Fetch definitions from disk
  37. const commentsDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/comments'))
  38. WIKI.data.commentProviders = []
  39. for (const dir of commentsDirs) {
  40. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/comments', dir, 'definition.yml'), 'utf8')
  41. const defParsed = yaml.load(def)
  42. defParsed.key = dir
  43. defParsed.props = commonHelper.parseModuleProps(defParsed.props)
  44. WIKI.data.commentProviders.push(defParsed)
  45. WIKI.logger.debug(`Loaded comments provider module definition ${dir}: [ OK ]`)
  46. }
  47. WIKI.logger.info(`Loaded ${WIKI.data.commentProviders.length} comments providers module definitions: [ OK ]`)
  48. } catch (err) {
  49. WIKI.logger.error(`Failed to scan or load comments providers: [ FAILED ]`)
  50. WIKI.logger.error(err)
  51. }
  52. }
  53. static async initProvider() {
  54. const commentProvider = await WIKI.models.commentProviders.query().findOne('isEnabled', true)
  55. if (commentProvider) {
  56. WIKI.data.commentProvider = {
  57. ..._.find(WIKI.data.commentProviders, ['key', commentProvider.module]),
  58. head: '',
  59. bodyStart: '',
  60. bodyEnd: '',
  61. main: '<comments></comments>'
  62. }
  63. if (WIKI.data.commentProvider.codeTemplate) {
  64. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/comments', commentProvider.key, 'code.yml'), 'utf8')
  65. let code = yaml.safeLoad(def)
  66. code.head = _.defaultTo(code.head, '')
  67. code.body = _.defaultTo(code.body, '')
  68. code.main = _.defaultTo(code.main, '')
  69. _.forOwn(commentProvider.config, (value, key) => {
  70. code.head = _.replace(code.head, new RegExp(`{{${key}}}`, 'g'), value)
  71. code.body = _.replace(code.body, new RegExp(`{{${key}}}`, 'g'), value)
  72. code.main = _.replace(code.main, new RegExp(`{{${key}}}`, 'g'), value)
  73. })
  74. WIKI.data.commentProvider.head = code.head
  75. WIKI.data.commentProvider.body = code.body
  76. WIKI.data.commentProvider.main = code.main
  77. } else {
  78. WIKI.data.commentProvider = {
  79. ...WIKI.data.commentProvider,
  80. ...require(`../modules/comments/${commentProvider.key}/comment`),
  81. config: commentProvider.config
  82. }
  83. await WIKI.data.commentProvider.init()
  84. }
  85. WIKI.data.commentProvider.config = commentProvider.config
  86. }
  87. }
  88. }