overleaf/services/docstore/app/coffee/RangeManager.coffee

35 lines
1 KiB
CoffeeScript
Raw Normal View History

2016-12-05 09:21:49 -05:00
_ = require "underscore"
{ObjectId} = require("./mongojs")
module.exports = RangeManager =
shouldUpdateRanges: (doc_ranges, incoming_ranges) ->
if !incoming_ranges?
throw new Error("expected incoming_ranges")
2016-12-05 09:21:49 -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.
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