You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.4 KiB

  1. const Model = require('objection').Model
  2. const _ = require('lodash')
  3. /* global WIKI */
  4. /**
  5. * Page History model
  6. */
  7. module.exports = class PageHistory extends Model {
  8. static get tableName() { return 'pageHistory' }
  9. static get jsonSchema () {
  10. return {
  11. type: 'object',
  12. required: ['path', 'title'],
  13. properties: {
  14. id: {type: 'integer'},
  15. path: {type: 'string'},
  16. hash: {type: 'string'},
  17. title: {type: 'string'},
  18. description: {type: 'string'},
  19. isPublished: {type: 'boolean'},
  20. publishStartDate: {type: 'string'},
  21. publishEndDate: {type: 'string'},
  22. content: {type: 'string'},
  23. contentType: {type: 'string'},
  24. createdAt: {type: 'string'}
  25. }
  26. }
  27. }
  28. static get relationMappings() {
  29. return {
  30. tags: {
  31. relation: Model.ManyToManyRelation,
  32. modelClass: require('./tags'),
  33. join: {
  34. from: 'pageHistory.id',
  35. through: {
  36. from: 'pageHistoryTags.pageId',
  37. to: 'pageHistoryTags.tagId'
  38. },
  39. to: 'tags.id'
  40. }
  41. },
  42. page: {
  43. relation: Model.BelongsToOneRelation,
  44. modelClass: require('./pages'),
  45. join: {
  46. from: 'pageHistory.pageId',
  47. to: 'pages.id'
  48. }
  49. },
  50. author: {
  51. relation: Model.BelongsToOneRelation,
  52. modelClass: require('./users'),
  53. join: {
  54. from: 'pageHistory.authorId',
  55. to: 'users.id'
  56. }
  57. },
  58. editor: {
  59. relation: Model.BelongsToOneRelation,
  60. modelClass: require('./editors'),
  61. join: {
  62. from: 'pageHistory.editorKey',
  63. to: 'editors.key'
  64. }
  65. },
  66. locale: {
  67. relation: Model.BelongsToOneRelation,
  68. modelClass: require('./locales'),
  69. join: {
  70. from: 'pageHistory.localeCode',
  71. to: 'locales.code'
  72. }
  73. }
  74. }
  75. }
  76. $beforeInsert() {
  77. this.createdAt = new Date().toISOString()
  78. }
  79. static async addVersion(opts) {
  80. await WIKI.models.pageHistory.query().insert({
  81. pageId: opts.id,
  82. authorId: opts.authorId,
  83. content: opts.content,
  84. contentType: opts.contentType,
  85. description: opts.description,
  86. editorKey: opts.editorKey,
  87. hash: opts.hash,
  88. isPrivate: opts.isPrivate,
  89. isPublished: opts.isPublished,
  90. localeCode: opts.localeCode,
  91. path: opts.path,
  92. publishEndDate: opts.publishEndDate || '',
  93. publishStartDate: opts.publishStartDate || '',
  94. title: opts.title
  95. })
  96. }
  97. static async getHistory({ pageId, offsetPage = 0, offsetSize = 100 }) {
  98. const history = await WIKI.models.pageHistory.query()
  99. .column([
  100. 'pageHistory.id',
  101. 'pageHistory.path',
  102. 'pageHistory.authorId',
  103. 'pageHistory.createdAt',
  104. {
  105. authorName: 'author.name'
  106. }
  107. ])
  108. .joinRelation('author')
  109. .where({
  110. 'pageHistory.pageId': pageId
  111. })
  112. .orderBy('pageHistory.createdAt', 'desc')
  113. .page(offsetPage, offsetSize)
  114. let prevPh = null
  115. const upperLimit = (offsetPage + 1) * offsetSize
  116. if (history.total >= upperLimit) {
  117. prevPh = await WIKI.models.pageHistory.query()
  118. .column([
  119. 'pageHistory.id',
  120. 'pageHistory.path',
  121. 'pageHistory.authorId',
  122. 'pageHistory.createdAt',
  123. {
  124. authorName: 'author.name'
  125. }
  126. ])
  127. .joinRelation('author')
  128. .where({
  129. 'pageHistory.pageId': pageId
  130. })
  131. .orderBy('pageHistory.createdAt', 'desc')
  132. .offset((offsetPage + 1) * offsetSize)
  133. .limit(1)
  134. .first()
  135. }
  136. return {
  137. trail: _.reduce(_.reverse(history.results), (res, ph) => {
  138. let actionType = 'edit'
  139. let valueBefore = null
  140. let valueAfter = null
  141. if (!prevPh && history.total < upperLimit) {
  142. actionType = 'initial'
  143. } else if (_.get(prevPh, 'path', '') !== ph.path) {
  144. actionType = 'move'
  145. valueBefore = _.get(prevPh, 'path', '')
  146. valueAfter = ph.path
  147. }
  148. res.unshift({
  149. versionId: ph.id,
  150. authorId: ph.authorId,
  151. authorName: ph.authorName,
  152. actionType,
  153. valueBefore,
  154. valueAfter,
  155. createdAt: ph.createdAt
  156. })
  157. prevPh = ph
  158. return res
  159. }, []),
  160. total: history.total
  161. }
  162. }
  163. }