2020-02-16 09:01:47 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-16 09:01:46 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-16 09:02:21 -05:00
|
|
|
let RangeManager
|
2022-12-15 07:53:03 -05:00
|
|
|
const _ = require('lodash')
|
2020-08-21 12:50:01 -04:00
|
|
|
const { ObjectId } = require('./mongodb')
|
2016-12-05 09:21:49 -05:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
module.exports = RangeManager = {
|
2023-03-16 07:58:12 -04:00
|
|
|
shouldUpdateRanges(docRanges, incomingRanges) {
|
|
|
|
if (incomingRanges == null) {
|
2020-02-16 09:02:21 -05:00
|
|
|
throw new Error('expected incoming_ranges')
|
|
|
|
}
|
2016-12-05 09:21:49 -05:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
// If the ranges are empty, we don't store them in the DB, so set
|
|
|
|
// doc_ranges to an empty object as default, since this is was the
|
|
|
|
// incoming_ranges will be for an empty range set.
|
2023-03-16 07:58:12 -04:00
|
|
|
if (docRanges == null) {
|
|
|
|
docRanges = {}
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2016-12-05 09:21:49 -05:00
|
|
|
|
2023-03-16 07:58:12 -04:00
|
|
|
return !_.isEqual(docRanges, incomingRanges)
|
2020-02-16 09:02:21 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
jsonRangesToMongo(ranges) {
|
|
|
|
if (ranges == null) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
const updateMetadata = function (metadata) {
|
2020-02-16 09:02:21 -05:00
|
|
|
if ((metadata != null ? metadata.ts : undefined) != null) {
|
|
|
|
metadata.ts = new Date(metadata.ts)
|
|
|
|
}
|
|
|
|
if ((metadata != null ? metadata.user_id : undefined) != null) {
|
|
|
|
return (metadata.user_id = RangeManager._safeObjectId(metadata.user_id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const change of Array.from(ranges.changes || [])) {
|
|
|
|
change.id = RangeManager._safeObjectId(change.id)
|
|
|
|
updateMetadata(change.metadata)
|
|
|
|
}
|
|
|
|
for (const comment of Array.from(ranges.comments || [])) {
|
|
|
|
comment.id = RangeManager._safeObjectId(comment.id)
|
|
|
|
if ((comment.op != null ? comment.op.t : undefined) != null) {
|
|
|
|
comment.op.t = RangeManager._safeObjectId(comment.op.t)
|
|
|
|
}
|
|
|
|
updateMetadata(comment.metadata)
|
|
|
|
}
|
|
|
|
return ranges
|
|
|
|
},
|
|
|
|
|
|
|
|
_safeObjectId(data) {
|
|
|
|
try {
|
2023-12-13 04:38:54 -05:00
|
|
|
return new ObjectId(data)
|
2020-02-16 09:02:21 -05:00
|
|
|
} catch (error) {
|
|
|
|
return data
|
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|