pageHistory.mjs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { Model } from 'objection'
  2. import { get, reduce, reverse } from 'lodash-es'
  3. import { DateTime, Duration } from 'luxon'
  4. import { Locale } from './locales.mjs'
  5. import { Page } from './pages.mjs'
  6. import { User } from './users.mjs'
  7. import { Tag } from './tags.mjs'
  8. /**
  9. * Page History model
  10. */
  11. export class PageHistory extends Model {
  12. static get tableName() { return 'pageHistory' }
  13. static get jsonSchema () {
  14. return {
  15. type: 'object',
  16. required: ['path', 'title'],
  17. properties: {
  18. id: {type: 'integer'},
  19. path: {type: 'string'},
  20. hash: {type: 'string'},
  21. title: {type: 'string'},
  22. description: {type: 'string'},
  23. publishState: {type: 'string'},
  24. publishStartDate: {type: 'string'},
  25. publishEndDate: {type: 'string'},
  26. content: {type: 'string'},
  27. contentType: {type: 'string'},
  28. createdAt: {type: 'string'}
  29. }
  30. }
  31. }
  32. static get relationMappings() {
  33. return {
  34. tags: {
  35. relation: Model.ManyToManyRelation,
  36. modelClass: Tag,
  37. join: {
  38. from: 'pageHistory.id',
  39. through: {
  40. from: 'pageHistoryTags.pageId',
  41. to: 'pageHistoryTags.tagId'
  42. },
  43. to: 'tags.id'
  44. }
  45. },
  46. page: {
  47. relation: Model.BelongsToOneRelation,
  48. modelClass: Page,
  49. join: {
  50. from: 'pageHistory.pageId',
  51. to: 'pages.id'
  52. }
  53. },
  54. author: {
  55. relation: Model.BelongsToOneRelation,
  56. modelClass: User,
  57. join: {
  58. from: 'pageHistory.authorId',
  59. to: 'users.id'
  60. }
  61. }
  62. }
  63. }
  64. $beforeInsert() {
  65. this.createdAt = new Date().toISOString()
  66. }
  67. /**
  68. * Create Page Version
  69. */
  70. static async addVersion(opts) {
  71. await WIKI.db.pageHistory.query().insert({
  72. pageId: opts.id,
  73. siteId: opts.siteId,
  74. authorId: opts.authorId,
  75. content: opts.content,
  76. contentType: opts.contentType,
  77. description: opts.description,
  78. editor: opts.editor,
  79. hash: opts.hash,
  80. publishState: opts.publishState,
  81. locale: opts.locale,
  82. path: opts.path,
  83. publishEndDate: opts.publishEndDate?.toISO(),
  84. publishStartDate: opts.publishStartDate?.toISO(),
  85. title: opts.title,
  86. action: opts.action || 'updated',
  87. versionDate: opts.versionDate
  88. })
  89. }
  90. /**
  91. * Get Page Version
  92. */
  93. static async getVersion({ pageId, versionId }) {
  94. const version = await WIKI.db.pageHistory.query()
  95. .column([
  96. 'pageHistory.path',
  97. 'pageHistory.title',
  98. 'pageHistory.description',
  99. 'pageHistory.isPublished',
  100. 'pageHistory.publishStartDate',
  101. 'pageHistory.publishEndDate',
  102. 'pageHistory.content',
  103. 'pageHistory.contentType',
  104. 'pageHistory.createdAt',
  105. 'pageHistory.action',
  106. 'pageHistory.authorId',
  107. 'pageHistory.pageId',
  108. 'pageHistory.versionDate',
  109. {
  110. versionId: 'pageHistory.id',
  111. editor: 'pageHistory.editorKey',
  112. locale: 'pageHistory.locale',
  113. authorName: 'author.name'
  114. }
  115. ])
  116. .joinRelated('author')
  117. .where({
  118. 'pageHistory.id': versionId,
  119. 'pageHistory.pageId': pageId
  120. }).first()
  121. if (version) {
  122. return {
  123. ...version,
  124. updatedAt: version.createdAt || null,
  125. tags: []
  126. }
  127. } else {
  128. return null
  129. }
  130. }
  131. /**
  132. * Get History Trail of a Page
  133. */
  134. static async getHistory({ pageId, offsetPage = 0, offsetSize = 100 }) {
  135. const history = await WIKI.db.pageHistory.query()
  136. .column([
  137. 'pageHistory.id',
  138. 'pageHistory.path',
  139. 'pageHistory.authorId',
  140. 'pageHistory.action',
  141. 'pageHistory.versionDate',
  142. {
  143. authorName: 'author.name'
  144. }
  145. ])
  146. .joinRelated('author')
  147. .where({
  148. 'pageHistory.pageId': pageId
  149. })
  150. .orderBy('pageHistory.versionDate', 'desc')
  151. .page(offsetPage, offsetSize)
  152. let prevPh = null
  153. const upperLimit = (offsetPage + 1) * offsetSize
  154. if (history.total >= upperLimit) {
  155. prevPh = await WIKI.db.pageHistory.query()
  156. .column([
  157. 'pageHistory.id',
  158. 'pageHistory.path',
  159. 'pageHistory.authorId',
  160. 'pageHistory.action',
  161. 'pageHistory.versionDate',
  162. {
  163. authorName: 'author.name'
  164. }
  165. ])
  166. .joinRelated('author')
  167. .where({
  168. 'pageHistory.pageId': pageId
  169. })
  170. .orderBy('pageHistory.versionDate', 'desc')
  171. .offset((offsetPage + 1) * offsetSize)
  172. .limit(1)
  173. .first()
  174. }
  175. return {
  176. trail: reduce(reverse(history.results), (res, ph) => {
  177. let actionType = 'edit'
  178. let valueBefore = null
  179. let valueAfter = null
  180. if (!prevPh && history.total < upperLimit) {
  181. actionType = 'initial'
  182. } else if (get(prevPh, 'path', '') !== ph.path) {
  183. actionType = 'move'
  184. valueBefore = get(prevPh, 'path', '')
  185. valueAfter = ph.path
  186. }
  187. res.unshift({
  188. versionId: ph.id,
  189. authorId: ph.authorId,
  190. authorName: ph.authorName,
  191. actionType,
  192. valueBefore,
  193. valueAfter,
  194. versionDate: ph.versionDate
  195. })
  196. prevPh = ph
  197. return res
  198. }, []),
  199. total: history.total
  200. }
  201. }
  202. /**
  203. * Purge history older than X
  204. *
  205. * @param {String} olderThan ISO 8601 Duration
  206. */
  207. static async purge (olderThan) {
  208. const dur = Duration.fromISO(olderThan)
  209. const olderThanISO = DateTime.utc().minus(dur)
  210. await WIKI.db.pageHistory.query().where('versionDate', '<', olderThanISO.toISO()).del()
  211. }
  212. }