sites.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. theme: {
  80. dark: false,
  81. colorPrimary: '#1976d2',
  82. colorSecondary: '#02c39a',
  83. colorAccent: '#f03a47',
  84. colorHeader: '#000000',
  85. colorSidebar: '#1976d2',
  86. injectCSS: '',
  87. injectHead: '',
  88. injectBody: '',
  89. sidebarPosition: 'left',
  90. tocPosition: 'right',
  91. showSharingMenu: true,
  92. showPrintBtn: true,
  93. baseFont: 'roboto',
  94. contentFont: 'roboto'
  95. }
  96. })
  97. })
  98. WIKI.logger.debug(`Creating new DB storage for site ${newSite.id}`)
  99. await WIKI.models.storage.query().insert({
  100. module: 'db',
  101. siteId: newSite.id,
  102. isEnabled: true,
  103. contentTypes: {
  104. activeTypes: ['pages', 'images', 'documents', 'others', 'large'],
  105. largeThreshold: '5MB'
  106. },
  107. assetDelivery: {
  108. streaming: true,
  109. directAccess: false
  110. },
  111. state: {
  112. current: 'ok'
  113. }
  114. })
  115. return newSite
  116. }
  117. static async updateSite (id, patch) {
  118. return WIKI.models.sites.query().findById(id).patch(patch)
  119. }
  120. static async deleteSite (id) {
  121. await WIKI.models.storage.query().delete().where('siteId', id)
  122. return WIKI.models.sites.query().deleteById(id)
  123. }
  124. }