commentProviders.mjs 3.4 KB

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