pages.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const Model = require('objection').Model
  2. /* global WIKI */
  3. /**
  4. * Pages model
  5. */
  6. module.exports = class Page extends Model {
  7. static get tableName() { return 'pages' }
  8. static get jsonSchema () {
  9. return {
  10. type: 'object',
  11. required: ['path', 'title'],
  12. properties: {
  13. id: {type: 'integer'},
  14. path: {type: 'string'},
  15. title: {type: 'string'},
  16. description: {type: 'string'},
  17. isPublished: {type: 'boolean'},
  18. publishStartDate: {type: 'string'},
  19. publishEndDate: {type: 'string'},
  20. content: {type: 'string'},
  21. contentType: {type: 'string'},
  22. createdAt: {type: 'string'},
  23. updatedAt: {type: 'string'}
  24. }
  25. }
  26. }
  27. static get relationMappings() {
  28. return {
  29. tags: {
  30. relation: Model.ManyToManyRelation,
  31. modelClass: require('./tags'),
  32. join: {
  33. from: 'pages.id',
  34. through: {
  35. from: 'pageTags.pageId',
  36. to: 'pageTags.tagId'
  37. },
  38. to: 'tags.id'
  39. }
  40. },
  41. author: {
  42. relation: Model.BelongsToOneRelation,
  43. modelClass: require('./users'),
  44. join: {
  45. from: 'pages.authorId',
  46. to: 'users.id'
  47. }
  48. },
  49. creator: {
  50. relation: Model.BelongsToOneRelation,
  51. modelClass: require('./users'),
  52. join: {
  53. from: 'pages.creatorId',
  54. to: 'users.id'
  55. }
  56. },
  57. editor: {
  58. relation: Model.BelongsToOneRelation,
  59. modelClass: require('./editors'),
  60. join: {
  61. from: 'pages.editorKey',
  62. to: 'editors.key'
  63. }
  64. },
  65. locale: {
  66. relation: Model.BelongsToOneRelation,
  67. modelClass: require('./locales'),
  68. join: {
  69. from: 'pages.localeCode',
  70. to: 'locales.code'
  71. }
  72. }
  73. }
  74. }
  75. $beforeUpdate() {
  76. this.updatedAt = new Date().toISOString()
  77. }
  78. $beforeInsert() {
  79. this.createdAt = new Date().toISOString()
  80. this.updatedAt = new Date().toISOString()
  81. }
  82. static async createPage(opts) {
  83. await WIKI.models.pages.renderPage(opts)
  84. const page = await WIKI.models.pages.query().insertAndFetch({
  85. authorId: opts.authorId,
  86. content: opts.content,
  87. creatorId: opts.authorId,
  88. contentType: _.get(WIKI.data.editors, `${opts.editor}.contentType`, 'text'),
  89. description: opts.description,
  90. editorKey: opts.editor,
  91. isPrivate: opts.isPrivate,
  92. isPublished: opts.isPublished,
  93. localeCode: opts.locale,
  94. path: opts.path,
  95. publishEndDate: opts.publishEndDate,
  96. publishStartDate: opts.publishStartDate,
  97. title: opts.title
  98. })
  99. await WIKI.models.storage.pageEvent({
  100. event: 'created',
  101. page
  102. })
  103. return page
  104. }
  105. static async updatePage(opts) {
  106. const ogPage = await WIKI.models.pages.query().findById(opts.id)
  107. if (!ogPage) {
  108. throw new Error('Invalid Page Id')
  109. }
  110. await WIKI.models.pageHistory.addVersion(ogPage)
  111. const page = await WIKI.models.pages.query().patchAndFetchById(ogPage.id, {
  112. authorId: opts.authorId,
  113. content: opts.content,
  114. description: opts.description,
  115. isPublished: opts.isPublished,
  116. publishEndDate: opts.publishEndDate,
  117. publishStartDate: opts.publishStartDate,
  118. title: opts.title
  119. })
  120. await WIKI.models.storage.pageEvent({
  121. event: 'updated',
  122. page
  123. })
  124. return page
  125. }
  126. static async renderPage(opts) {
  127. WIKI.queue.job.renderPage.add(opts, {
  128. removeOnComplete: true,
  129. removeOnFail: true
  130. })
  131. }
  132. }