pageHistory.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. const Model = require('objection').Model
  2. const _ = require('lodash')
  3. const { DateTime, Duration } = require('luxon')
  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. publishState: {type: 'string'},
  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. locale: {
  59. relation: Model.BelongsToOneRelation,
  60. modelClass: require('./locales'),
  61. join: {
  62. from: 'pageHistory.localeCode',
  63. to: 'locales.code'
  64. }
  65. }
  66. }
  67. }
  68. $beforeInsert() {
  69. this.createdAt = new Date().toISOString()
  70. }
  71. /**
  72. * Create Page Version
  73. */
  74. static async addVersion(opts) {
  75. await WIKI.db.pageHistory.query().insert({
  76. pageId: opts.id,
  77. siteId: opts.siteId,
  78. authorId: opts.authorId,
  79. content: opts.content,
  80. contentType: opts.contentType,
  81. description: opts.description,
  82. editor: opts.editor,
  83. hash: opts.hash,
  84. publishState: opts.publishState,
  85. localeCode: opts.localeCode,
  86. path: opts.path,
  87. publishEndDate: opts.publishEndDate?.toISO(),
  88. publishStartDate: opts.publishStartDate?.toISO(),
  89. title: opts.title,
  90. action: opts.action || 'updated',
  91. versionDate: opts.versionDate
  92. })
  93. }
  94. /**
  95. * Get Page Version
  96. */
  97. static async getVersion({ pageId, versionId }) {
  98. const version = await WIKI.db.pageHistory.query()
  99. .column([
  100. 'pageHistory.path',
  101. 'pageHistory.title',
  102. 'pageHistory.description',
  103. 'pageHistory.isPublished',
  104. 'pageHistory.publishStartDate',
  105. 'pageHistory.publishEndDate',
  106. 'pageHistory.content',
  107. 'pageHistory.contentType',
  108. 'pageHistory.createdAt',
  109. 'pageHistory.action',
  110. 'pageHistory.authorId',
  111. 'pageHistory.pageId',
  112. 'pageHistory.versionDate',
  113. {
  114. versionId: 'pageHistory.id',
  115. editor: 'pageHistory.editorKey',
  116. locale: 'pageHistory.localeCode',
  117. authorName: 'author.name'
  118. }
  119. ])
  120. .joinRelated('author')
  121. .where({
  122. 'pageHistory.id': versionId,
  123. 'pageHistory.pageId': pageId
  124. }).first()
  125. if (version) {
  126. return {
  127. ...version,
  128. updatedAt: version.createdAt || null,
  129. tags: []
  130. }
  131. } else {
  132. return null
  133. }
  134. }
  135. /**
  136. * Get History Trail of a Page
  137. */
  138. static async getHistory({ pageId, offsetPage = 0, offsetSize = 100 }) {
  139. const history = await WIKI.db.pageHistory.query()
  140. .column([
  141. 'pageHistory.id',
  142. 'pageHistory.path',
  143. 'pageHistory.authorId',
  144. 'pageHistory.action',
  145. 'pageHistory.versionDate',
  146. {
  147. authorName: 'author.name'
  148. }
  149. ])
  150. .joinRelated('author')
  151. .where({
  152. 'pageHistory.pageId': pageId
  153. })
  154. .orderBy('pageHistory.versionDate', 'desc')
  155. .page(offsetPage, offsetSize)
  156. let prevPh = null
  157. const upperLimit = (offsetPage + 1) * offsetSize
  158. if (history.total >= upperLimit) {
  159. prevPh = await WIKI.db.pageHistory.query()
  160. .column([
  161. 'pageHistory.id',
  162. 'pageHistory.path',
  163. 'pageHistory.authorId',
  164. 'pageHistory.action',
  165. 'pageHistory.versionDate',
  166. {
  167. authorName: 'author.name'
  168. }
  169. ])
  170. .joinRelated('author')
  171. .where({
  172. 'pageHistory.pageId': pageId
  173. })
  174. .orderBy('pageHistory.versionDate', 'desc')
  175. .offset((offsetPage + 1) * offsetSize)
  176. .limit(1)
  177. .first()
  178. }
  179. return {
  180. trail: _.reduce(_.reverse(history.results), (res, ph) => {
  181. let actionType = 'edit'
  182. let valueBefore = null
  183. let valueAfter = null
  184. if (!prevPh && history.total < upperLimit) {
  185. actionType = 'initial'
  186. } else if (_.get(prevPh, 'path', '') !== ph.path) {
  187. actionType = 'move'
  188. valueBefore = _.get(prevPh, 'path', '')
  189. valueAfter = ph.path
  190. }
  191. res.unshift({
  192. versionId: ph.id,
  193. authorId: ph.authorId,
  194. authorName: ph.authorName,
  195. actionType,
  196. valueBefore,
  197. valueAfter,
  198. versionDate: ph.versionDate
  199. })
  200. prevPh = ph
  201. return res
  202. }, []),
  203. total: history.total
  204. }
  205. }
  206. /**
  207. * Purge history older than X
  208. *
  209. * @param {String} olderThan ISO 8601 Duration
  210. */
  211. static async purge (olderThan) {
  212. const dur = Duration.fromISO(olderThan)
  213. const olderThanISO = DateTime.utc().minus(dur)
  214. await WIKI.db.pageHistory.query().where('versionDate', '<', olderThanISO.toISO()).del()
  215. }
  216. }