2016-12-05 09:21:49 -05:00
|
|
|
_ = require "underscore"
|
|
|
|
{ObjectId} = require("./mongojs")
|
|
|
|
|
|
|
|
module.exports = RangeManager =
|
|
|
|
shouldUpdateRanges: (doc_ranges, incoming_ranges) ->
|
|
|
|
# TODO: If we have no incoming_ranges, just ignore for now while
|
|
|
|
# we're rolling this out, but eventually this should be a mandatory
|
|
|
|
# field and this will become an error condition
|
|
|
|
return false if !incoming_ranges?
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
if !doc_ranges?
|
|
|
|
doc_ranges = {}
|
|
|
|
|
|
|
|
return not _.isEqual(doc_ranges, incoming_ranges)
|
|
|
|
|
|
|
|
jsonRangesToMongo: (ranges) ->
|
|
|
|
return null if !ranges?
|
|
|
|
for change in ranges.changes or []
|
|
|
|
change.id = @_safeObjectId(change.id)
|
|
|
|
if change.metadata?.ts?
|
|
|
|
change.metadata.ts = new Date(change.metadata.ts)
|
2016-12-05 10:38:38 -05:00
|
|
|
if change.metadata?.user_id?
|
|
|
|
change.metadata.user_id = @_safeObjectId(change.metadata.user_id)
|
2016-12-05 09:21:49 -05:00
|
|
|
for comment in ranges.comments or []
|
|
|
|
comment.id = @_safeObjectId(comment.id)
|
2016-12-16 11:44:08 -05:00
|
|
|
if comment.op?.t?
|
|
|
|
comment.op.t = @_safeObjectId(comment.op.t)
|
2016-12-05 09:21:49 -05:00
|
|
|
return ranges
|
|
|
|
|
|
|
|
_safeObjectId: (data) ->
|
|
|
|
try
|
|
|
|
return ObjectId(data)
|
|
|
|
catch
|
|
|
|
return data
|