sites.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const Model = require('objection').Model
  2. const crypto = require('crypto')
  3. const pem2jwk = require('pem-jwk').pem2jwk
  4. const _ = require('lodash')
  5. /* global WIKI */
  6. /**
  7. * Site model
  8. */
  9. module.exports = class Site extends Model {
  10. static get tableName () { return 'sites' }
  11. static get jsonSchema () {
  12. return {
  13. type: 'object',
  14. required: ['hostname'],
  15. properties: {
  16. id: { type: 'string' },
  17. hostname: { type: 'string' },
  18. isEnabled: { type: 'boolean', default: false }
  19. }
  20. }
  21. }
  22. static get jsonAttributes () {
  23. return ['config']
  24. }
  25. static async getSiteByHostname ({ hostname, forceReload = false }) {
  26. if (forceReload) {
  27. await WIKI.models.sites.reloadCache()
  28. }
  29. const siteId = WIKI.sitesMappings[hostname] || WIKI.sitesMappings['*']
  30. if (siteId) {
  31. return WIKI.sites[siteId]
  32. }
  33. return null
  34. }
  35. static async reloadCache () {
  36. WIKI.logger.info('Reloading site configurations...')
  37. const sites = await WIKI.models.sites.query().orderBy('id')
  38. WIKI.sites = _.keyBy(sites, 'id')
  39. WIKI.sitesMappings = {}
  40. for (const site of sites) {
  41. WIKI.sitesMappings[site.hostname] = site.id
  42. }
  43. WIKI.logger.info(`Loaded ${sites.length} site configurations [ OK ]`)
  44. }
  45. static async createSite (hostname, config) {
  46. const newSite = await WIKI.models.sites.query().insertAndFetch({
  47. hostname,
  48. isEnabled: true,
  49. config: _.defaultsDeep(config, {
  50. title: 'My Wiki Site',
  51. description: '',
  52. company: '',
  53. contentLicense: '',
  54. footerExtra: '',
  55. pageExtensions: ['md', 'html', 'txt'],
  56. defaults: {
  57. timezone: 'America/New_York',
  58. dateFormat: 'YYYY-MM-DD',
  59. timeFormat: '12h'
  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. locale: 'en',
  77. localeNamespacing: false,
  78. localeNamespaces: [],
  79. assets: {
  80. logo: false,
  81. logoExt: 'svg',
  82. favicon: false,
  83. faviconExt: 'svg',
  84. loginBg: false
  85. },
  86. theme: {
  87. dark: false,
  88. colorPrimary: '#1976D2',
  89. colorSecondary: '#02C39A',
  90. colorAccent: '#FF9800',
  91. colorHeader: '#000000',
  92. colorSidebar: '#1976D2',
  93. injectCSS: '',
  94. injectHead: '',
  95. injectBody: '',
  96. contentWidth: 'full',
  97. sidebarPosition: 'left',
  98. tocPosition: 'right',
  99. showSharingMenu: true,
  100. showPrintBtn: true,
  101. baseFont: 'roboto',
  102. contentFont: 'roboto'
  103. }
  104. })
  105. })
  106. WIKI.logger.debug(`Creating new DB storage for site ${newSite.id}`)
  107. await WIKI.models.storage.query().insert({
  108. module: 'db',
  109. siteId: newSite.id,
  110. isEnabled: true,
  111. contentTypes: {
  112. activeTypes: ['pages', 'images', 'documents', 'others', 'large'],
  113. largeThreshold: '5MB'
  114. },
  115. assetDelivery: {
  116. streaming: true,
  117. directAccess: false
  118. },
  119. state: {
  120. current: 'ok'
  121. }
  122. })
  123. return newSite
  124. }
  125. static async updateSite (id, patch) {
  126. return WIKI.models.sites.query().findById(id).patch(patch)
  127. }
  128. static async deleteSite (id) {
  129. await WIKI.models.storage.query().delete().where('siteId', id)
  130. return WIKI.models.sites.query().deleteById(id)
  131. }
  132. }