sites.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 createSite (hostname, config) {
  26. const newSite = await WIKI.models.sites.query().insertAndFetch({
  27. hostname,
  28. isEnabled: true,
  29. config: _.defaultsDeep(config, {
  30. title: 'My Wiki Site',
  31. description: '',
  32. company: '',
  33. contentLicense: '',
  34. defaults: {
  35. timezone: 'America/New_York',
  36. dateFormat: 'YYYY-MM-DD',
  37. timeFormat: '12h'
  38. },
  39. features: {
  40. ratings: false,
  41. ratingsMode: 'off',
  42. comments: false,
  43. contributions: false,
  44. profile: true,
  45. search: true
  46. },
  47. logoUrl: '',
  48. logoText: true,
  49. robots: {
  50. index: true,
  51. follow: true
  52. },
  53. locale: 'en',
  54. localeNamespacing: false,
  55. localeNamespaces: [],
  56. theme: {
  57. dark: false,
  58. colorPrimary: '#1976d2',
  59. colorSecondary: '#02c39a',
  60. colorAccent: '#f03a47',
  61. colorHeader: '#000000',
  62. colorSidebar: '#1976d2',
  63. injectCSS: '',
  64. injectHead: '',
  65. injectBody: '',
  66. sidebarPosition: 'left',
  67. tocPosition: 'right',
  68. showSharingMenu: true,
  69. showPrintBtn: true
  70. }
  71. })
  72. })
  73. await WIKI.models.storage.query().insert({
  74. module: 'db',
  75. siteId: newSite.id,
  76. isEnabled: true,
  77. contentTypes: {
  78. activeTypes: ['pages', 'images', 'documents', 'others', 'large'],
  79. largeThreshold: '5MB'
  80. },
  81. assetDelivery: {
  82. streaming: true,
  83. directAccess: false
  84. },
  85. state: {
  86. current: 'ok'
  87. }
  88. })
  89. return newSite
  90. }
  91. static async updateSite (id, patch) {
  92. return WIKI.models.sites.query().findById(id).patch(patch)
  93. }
  94. static async deleteSite (id) {
  95. await WIKI.models.storage.query().delete().where('siteId', id)
  96. return WIKI.models.sites.query().deleteById(id)
  97. }
  98. }