overleaf/services/document-updater/app/coffee/RangesManager.coffee

54 lines
2 KiB
CoffeeScript
Raw Normal View History

RangesTracker = require "./RangesTracker"
logger = require "logger-sharelatex"
module.exports = RangesManager =
MAX_COMMENTS: 500
2017-02-24 08:58:42 -05:00
MAX_CHANGES: 2000
applyUpdate: (project_id, doc_id, entries = {}, updates = [], callback = (error, new_entries) ->) ->
{changes, comments} = entries
rangesTracker = new RangesTracker(changes, comments)
for update in updates
rangesTracker.track_changes = !!update.meta.tc
if !!update.meta.tc
rangesTracker.setIdSeed(update.meta.tc)
for op in update.op
2017-03-06 14:05:11 -05:00
try
rangesTracker.applyOp(op, { user_id: update.meta?.user_id })
catch error
return callback(error)
if rangesTracker.changes?.length > RangesManager.MAX_CHANGES or rangesTracker.comments?.length > RangesManager.MAX_COMMENTS
return callback new Error("too many comments or tracked changes")
response = RangesManager._getRanges rangesTracker
logger.log {project_id, doc_id, changesCount: response.changes?.length, commentsCount: response.comments?.length}, "applied updates to ranges"
callback null, response
acceptChange: (change_id, ranges, callback = (error, ranges) ->) ->
{changes, comments} = ranges
logger.log {change_id}, "accepting change in ranges"
rangesTracker = new RangesTracker(changes, comments)
rangesTracker.removeChangeId(change_id)
response = RangesManager._getRanges(rangesTracker)
callback null, response
2017-01-24 09:57:11 -05:00
deleteComment: (comment_id, ranges, callback = (error, ranges) ->) ->
{changes, comments} = ranges
logger.log {comment_id}, "deleting comment in ranges"
2017-01-24 09:57:11 -05:00
rangesTracker = new RangesTracker(changes, comments)
rangesTracker.removeCommentId(comment_id)
response = RangesManager._getRanges(rangesTracker)
callback null, response
_getRanges: (rangesTracker) ->
# Return the minimal data structure needed, since most documents won't have any
# changes or comments
response = {}
if rangesTracker.changes?.length > 0
response ?= {}
response.changes = rangesTracker.changes
if rangesTracker.comments?.length > 0
response ?= {}
response.comments = rangesTracker.comments
return response