v2_doc_versions.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict'
  2. const _ = require('lodash')
  3. /**
  4. * @typedef {import("./file")} File
  5. * @typedef {import("./types").RawV2DocVersions} RawV2DocVersions
  6. */
  7. class V2DocVersions {
  8. /**
  9. * @param {RawV2DocVersions} data
  10. */
  11. constructor(data) {
  12. this.data = data || {}
  13. }
  14. static fromRaw(raw) {
  15. if (!raw) return undefined
  16. return new V2DocVersions(raw)
  17. }
  18. /**
  19. * @abstract
  20. */
  21. toRaw() {
  22. if (!this.data) return null
  23. const raw = _.clone(this.data)
  24. return raw
  25. }
  26. /**
  27. * Clone this object.
  28. *
  29. * @return {V2DocVersions} a new object of the same type
  30. */
  31. clone() {
  32. return V2DocVersions.fromRaw(this.toRaw())
  33. }
  34. applyTo(snapshot) {
  35. // Only update the snapshot versions if we have new versions
  36. if (!_.size(this.data)) return
  37. // Create v2DocVersions in snapshot if it does not exist
  38. // otherwise update snapshot v2docversions
  39. if (!snapshot.v2DocVersions) {
  40. snapshot.v2DocVersions = this.clone()
  41. } else {
  42. _.assign(snapshot.v2DocVersions.data, this.data)
  43. }
  44. }
  45. /**
  46. * Move or remove a doc.
  47. * Must be called after FileMap#moveFile, which validates the paths.
  48. */
  49. moveFile(pathname, newPathname) {
  50. for (const [id, v] of Object.entries(this.data)) {
  51. if (v.pathname !== pathname) continue
  52. if (newPathname === '') {
  53. delete this.data[id]
  54. } else {
  55. v.pathname = newPathname
  56. }
  57. break
  58. }
  59. }
  60. }
  61. module.exports = V2DocVersions