sites.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. defaults: {
  54. tocDepth: {
  55. min: 1,
  56. max: 2
  57. }
  58. },
  59. features: {
  60. ratings: false,
  61. ratingsMode: 'off',
  62. comments: false,
  63. contributions: false,
  64. profile: true,
  65. search: true
  66. },
  67. logoUrl: '',
  68. logoText: true,
  69. sitemap: true,
  70. robots: {
  71. index: true,
  72. follow: true
  73. },
  74. locale: 'en',
  75. localeNamespacing: false,
  76. localeNamespaces: [],
  77. assets: {
  78. logo: false,
  79. logoExt: 'svg',
  80. favicon: false,
  81. faviconExt: 'svg',
  82. loginBg: false
  83. },
  84. theme: {
  85. dark: false,
  86. colorPrimary: '#1976D2',
  87. colorSecondary: '#02C39A',
  88. colorAccent: '#FF9800',
  89. colorHeader: '#000000',
  90. colorSidebar: '#1976D2',
  91. injectCSS: '',
  92. injectHead: '',
  93. injectBody: '',
  94. contentWidth: 'full',
  95. sidebarPosition: 'left',
  96. tocPosition: 'right',
  97. showSharingMenu: true,
  98. showPrintBtn: true,
  99. baseFont: 'roboto',
  100. contentFont: 'roboto'
  101. },
  102. editors: {
  103. asciidoc: {
  104. isActive: true,
  105. config: {}
  106. },
  107. markdown: {
  108. isActive: true,
  109. config: {
  110. allowHTML: true,
  111. kroki: false,
  112. krokiServerUrl: 'https://kroki.io',
  113. latexEngine: 'katex',
  114. lineBreaks: true,
  115. linkify: true,
  116. multimdTable: true,
  117. plantuml: false,
  118. plantumlServerUrl: 'https://www.plantuml.com/plantuml/',
  119. quotes: 'english',
  120. tabWidth: 2,
  121. typographer: false,
  122. underline: true
  123. }
  124. },
  125. wysiwyg: {
  126. isActive: true,
  127. config: {}
  128. }
  129. },
  130. uploads: {
  131. conflictBehavior: 'overwrite',
  132. normalizeFilename: true
  133. }
  134. })
  135. })
  136. WIKI.logger.debug(`Creating new DB storage for site ${newSite.id}`)
  137. await WIKI.db.storage.query().insert({
  138. module: 'db',
  139. siteId: newSite.id,
  140. isEnabled: true,
  141. contentTypes: {
  142. activeTypes: ['pages', 'images', 'documents', 'others', 'large'],
  143. largeThreshold: '5MB'
  144. },
  145. assetDelivery: {
  146. streaming: true,
  147. directAccess: false
  148. },
  149. state: {
  150. current: 'ok'
  151. }
  152. })
  153. return newSite
  154. }
  155. static async updateSite (id, patch) {
  156. return WIKI.db.sites.query().findById(id).patch(patch)
  157. }
  158. static async deleteSite (id) {
  159. await WIKI.db.storage.query().delete().where('siteId', id)
  160. return WIKI.db.sites.query().deleteById(id)
  161. }
  162. }