pages.js 4.1 KB

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