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

40 lines
1.2 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?
2017-03-30 12:13:43 -04:00
updateMetadata = (metadata) ->
if metadata?.ts?
metadata.ts = new Date(metadata.ts)
if metadata?.user_id?
metadata.user_id = RangeManager._safeObjectId(metadata.user_id)
2016-12-05 09:21:49 -05:00
for change in ranges.changes or []
2017-03-30 12:13:43 -04:00
change.id = RangeManager._safeObjectId(change.id)
updateMetadata(change.metadata)
2016-12-05 09:21:49 -05:00
for comment in ranges.comments or []
2017-03-30 12:13:43 -04:00
comment.id = RangeManager._safeObjectId(comment.id)
2016-12-16 11:44:08 -05:00
if comment.op?.t?
2017-03-30 12:13:43 -04:00
comment.op.t = RangeManager._safeObjectId(comment.op.t)
updateMetadata(comment.metadata)
2016-12-05 09:21:49 -05:00
return ranges
_safeObjectId: (data) ->
try
return ObjectId(data)
catch error
2016-12-05 09:21:49 -05:00
return data