RangeManager.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let RangeManager
  15. const _ = require('underscore')
  16. const { ObjectId } = require('./mongodb')
  17. module.exports = RangeManager = {
  18. shouldUpdateRanges(doc_ranges, incoming_ranges) {
  19. if (incoming_ranges == null) {
  20. throw new Error('expected incoming_ranges')
  21. }
  22. // If the ranges are empty, we don't store them in the DB, so set
  23. // doc_ranges to an empty object as default, since this is was the
  24. // incoming_ranges will be for an empty range set.
  25. if (doc_ranges == null) {
  26. doc_ranges = {}
  27. }
  28. return !_.isEqual(doc_ranges, incoming_ranges)
  29. },
  30. jsonRangesToMongo(ranges) {
  31. if (ranges == null) {
  32. return null
  33. }
  34. const updateMetadata = function (metadata) {
  35. if ((metadata != null ? metadata.ts : undefined) != null) {
  36. metadata.ts = new Date(metadata.ts)
  37. }
  38. if ((metadata != null ? metadata.user_id : undefined) != null) {
  39. return (metadata.user_id = RangeManager._safeObjectId(metadata.user_id))
  40. }
  41. }
  42. for (const change of Array.from(ranges.changes || [])) {
  43. change.id = RangeManager._safeObjectId(change.id)
  44. updateMetadata(change.metadata)
  45. }
  46. for (const comment of Array.from(ranges.comments || [])) {
  47. comment.id = RangeManager._safeObjectId(comment.id)
  48. if ((comment.op != null ? comment.op.t : undefined) != null) {
  49. comment.op.t = RangeManager._safeObjectId(comment.op.t)
  50. }
  51. updateMetadata(comment.metadata)
  52. }
  53. return ranges
  54. },
  55. _safeObjectId(data) {
  56. try {
  57. return ObjectId(data)
  58. } catch (error) {
  59. return data
  60. }
  61. },
  62. }