sites.js 3.5 KB

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