sites.mjs 4.6 KB

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