overleaf/services/docstore/app/coffee/RangeManager.coffee
2016-12-05 14:21:49 +00:00

35 lines
No EOL
1.1 KiB
CoffeeScript

_ = 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)
for comment in ranges.comments or []
comment.id = @_safeObjectId(comment.id)
if comment.metadata?.ts?
comment.metadata.ts = new Date(comment.metadata.ts)
return ranges
_safeObjectId: (data) ->
try
return ObjectId(data)
catch
return data