sites.mjs 4.3 KB

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