sites.mjs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { Model } from 'objection'
  2. import { defaultsDeep, keyBy } from 'lodash-es'
  3. /**
  4. * Site model
  5. */
  6. export class Site extends Model {
  7. static get tableName () { return 'sites' }
  8. static get jsonSchema () {
  9. return {
  10. type: 'object',
  11. required: ['hostname'],
  12. properties: {
  13. id: { type: 'string' },
  14. hostname: { type: 'string' },
  15. isEnabled: { type: 'boolean', default: false }
  16. }
  17. }
  18. }
  19. static get jsonAttributes () {
  20. return ['config']
  21. }
  22. static async getSiteByHostname ({ hostname, forceReload = false }) {
  23. if (forceReload) {
  24. await WIKI.db.sites.reloadCache()
  25. }
  26. const siteId = WIKI.sitesMappings[hostname] || WIKI.sitesMappings['*']
  27. if (siteId) {
  28. return WIKI.sites[siteId]
  29. }
  30. return null
  31. }
  32. static async reloadCache () {
  33. WIKI.logger.info('Reloading site configurations...')
  34. const sites = await WIKI.db.sites.query().orderBy('id')
  35. WIKI.sites = keyBy(sites, 'id')
  36. WIKI.sitesMappings = {}
  37. for (const site of sites) {
  38. WIKI.sitesMappings[site.hostname] = site.id
  39. }
  40. WIKI.logger.info(`Loaded ${sites.length} site configurations [ OK ]`)
  41. }
  42. static async createSite (hostname, config) {
  43. const newSite = await WIKI.db.sites.query().insertAndFetch({
  44. hostname,
  45. isEnabled: true,
  46. config: defaultsDeep(config, {
  47. title: 'My Wiki Site',
  48. description: '',
  49. company: '',
  50. contentLicense: '',
  51. footerExtra: '',
  52. pageExtensions: ['md', 'html', 'txt'],
  53. pageCasing: true,
  54. discoverable: false,
  55. defaults: {
  56. tocDepth: {
  57. min: 1,
  58. max: 2
  59. }
  60. },
  61. features: {
  62. ratings: false,
  63. ratingsMode: 'off',
  64. comments: false,
  65. contributions: false,
  66. profile: true,
  67. search: true
  68. },
  69. logoUrl: '',
  70. logoText: true,
  71. sitemap: true,
  72. robots: {
  73. index: true,
  74. follow: true
  75. },
  76. locales: {
  77. primary: 'en',
  78. active: ['en']
  79. },
  80. assets: {
  81. logo: false,
  82. logoExt: 'svg',
  83. favicon: false,
  84. faviconExt: 'svg',
  85. loginBg: false
  86. },
  87. theme: {
  88. dark: false,
  89. codeBlocksTheme: 'github-dark',
  90. colorPrimary: '#1976D2',
  91. colorSecondary: '#02C39A',
  92. colorAccent: '#FF9800',
  93. colorHeader: '#000000',
  94. colorSidebar: '#1976D2',
  95. injectCSS: '',
  96. injectHead: '',
  97. injectBody: '',
  98. contentWidth: 'full',
  99. sidebarPosition: 'left',
  100. tocPosition: 'right',
  101. showSharingMenu: true,
  102. showPrintBtn: true,
  103. baseFont: 'roboto',
  104. contentFont: 'roboto'
  105. },
  106. editors: {
  107. asciidoc: {
  108. isActive: true,
  109. config: {}
  110. },
  111. markdown: {
  112. isActive: true,
  113. config: {
  114. allowHTML: true,
  115. kroki: false,
  116. krokiServerUrl: 'https://kroki.io',
  117. latexEngine: 'katex',
  118. lineBreaks: true,
  119. linkify: true,
  120. multimdTable: true,
  121. plantuml: false,
  122. plantumlServerUrl: 'https://www.plantuml.com/plantuml/',
  123. quotes: 'english',
  124. tabWidth: 2,
  125. typographer: false,
  126. underline: true
  127. }
  128. },
  129. wysiwyg: {
  130. isActive: true,
  131. config: {}
  132. }
  133. },
  134. uploads: {
  135. conflictBehavior: 'overwrite',
  136. normalizeFilename: true
  137. }
  138. })
  139. })
  140. WIKI.logger.debug(`Creating new DB storage for site ${newSite.id}`)
  141. await WIKI.db.storage.query().insert({
  142. module: 'db',
  143. siteId: newSite.id,
  144. isEnabled: true,
  145. contentTypes: {
  146. activeTypes: ['pages', 'images', 'documents', 'others', 'large'],
  147. largeThreshold: '5MB'
  148. },
  149. assetDelivery: {
  150. streaming: true,
  151. directAccess: false
  152. },
  153. state: {
  154. current: 'ok'
  155. }
  156. })
  157. return newSite
  158. }
  159. static async updateSite (id, patch) {
  160. return WIKI.db.sites.query().findById(id).patch(patch)
  161. }
  162. static async deleteSite (id) {
  163. await WIKI.db.storage.query().delete().where('siteId', id)
  164. return WIKI.db.sites.query().deleteById(id)
  165. }
  166. }