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.

181 lines
4.5 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 === true || opts.isPrivate === 1),
  89. isPublished: (opts.isPublished === true || opts.isPublished === 1),
  90. localeCode: opts.localeCode,
  91. path: opts.path,
  92. publishEndDate: opts.publishEndDate || '',
  93. publishStartDate: opts.publishStartDate || '',
  94. title: opts.title,
  95. action: opts.action || 'updated'
  96. })
  97. }
  98. static async getHistory({ pageId, offsetPage = 0, offsetSize = 100 }) {
  99. const history = await WIKI.models.pageHistory.query()
  100. .column([
  101. 'pageHistory.id',
  102. 'pageHistory.path',
  103. 'pageHistory.authorId',
  104. 'pageHistory.action',
  105. 'pageHistory.createdAt',
  106. {
  107. authorName: 'author.name'
  108. }
  109. ])
  110. .joinRelation('author')
  111. .where({
  112. 'pageHistory.pageId': pageId
  113. })
  114. .orderBy('pageHistory.createdAt', 'desc')
  115. .page(offsetPage, offsetSize)
  116. let prevPh = null
  117. const upperLimit = (offsetPage + 1) * offsetSize
  118. if (history.total >= upperLimit) {
  119. prevPh = await WIKI.models.pageHistory.query()
  120. .column([
  121. 'pageHistory.id',
  122. 'pageHistory.path',
  123. 'pageHistory.authorId',
  124. 'pageHistory.action',
  125. 'pageHistory.createdAt',
  126. {
  127. authorName: 'author.name'
  128. }
  129. ])
  130. .joinRelation('author')
  131. .where({
  132. 'pageHistory.pageId': pageId
  133. })
  134. .orderBy('pageHistory.createdAt', 'desc')
  135. .offset((offsetPage + 1) * offsetSize)
  136. .limit(1)
  137. .first()
  138. }
  139. return {
  140. trail: _.reduce(_.reverse(history.results), (res, ph) => {
  141. let actionType = 'edit'
  142. let valueBefore = null
  143. let valueAfter = null
  144. if (!prevPh && history.total < upperLimit) {
  145. actionType = 'initial'
  146. } else if (_.get(prevPh, 'path', '') !== ph.path) {
  147. actionType = 'move'
  148. valueBefore = _.get(prevPh, 'path', '')
  149. valueAfter = ph.path
  150. }
  151. res.unshift({
  152. versionId: ph.id,
  153. authorId: ph.authorId,
  154. authorName: ph.authorName,
  155. actionType,
  156. valueBefore,
  157. valueAfter,
  158. createdAt: ph.createdAt
  159. })
  160. prevPh = ph
  161. return res
  162. }, []),
  163. total: history.total
  164. }
  165. }
  166. }