pageHistory.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. versionDate: opts.versionDate
  97. })
  98. }
  99. static async getVersion({ pageId, versionId }) {
  100. const version = await WIKI.models.pageHistory.query()
  101. .column([
  102. 'pageHistory.path',
  103. 'pageHistory.title',
  104. 'pageHistory.description',
  105. 'pageHistory.isPrivate',
  106. 'pageHistory.isPublished',
  107. 'pageHistory.publishStartDate',
  108. 'pageHistory.publishEndDate',
  109. 'pageHistory.content',
  110. 'pageHistory.contentType',
  111. 'pageHistory.createdAt',
  112. 'pageHistory.action',
  113. 'pageHistory.authorId',
  114. 'pageHistory.pageId',
  115. 'pageHistory.versionDate',
  116. {
  117. versionId: 'pageHistory.id',
  118. editor: 'pageHistory.editorKey',
  119. locale: 'pageHistory.localeCode',
  120. authorName: 'author.name'
  121. }
  122. ])
  123. .joinRelated('author')
  124. .where({
  125. 'pageHistory.id': versionId,
  126. 'pageHistory.pageId': pageId
  127. }).first()
  128. return {
  129. ...version,
  130. updatedAt: version.createdAt,
  131. tags: []
  132. }
  133. }
  134. static async getHistory({ pageId, offsetPage = 0, offsetSize = 100 }) {
  135. const history = await WIKI.models.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.models.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. }