2016-12-08 07:31:43 -05:00
|
|
|
RangesTracker = require "./RangesTracker"
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
|
|
|
|
module.exports = RangesManager =
|
2017-01-10 10:58:11 -05:00
|
|
|
MAX_COMMENTS: 500
|
2017-02-24 08:58:42 -05:00
|
|
|
MAX_CHANGES: 2000
|
2017-01-10 10:58:11 -05:00
|
|
|
|
2017-03-15 10:12:06 -04:00
|
|
|
applyUpdate: (project_id, doc_id, entries = {}, updates = [], newDocLines, callback = (error, new_entries) ->) ->
|
2016-12-08 07:31:43 -05:00
|
|
|
{changes, comments} = entries
|
|
|
|
rangesTracker = new RangesTracker(changes, comments)
|
|
|
|
for update in updates
|
|
|
|
rangesTracker.track_changes = !!update.meta.tc
|
2017-01-09 04:46:58 -05:00
|
|
|
if !!update.meta.tc
|
|
|
|
rangesTracker.setIdSeed(update.meta.tc)
|
2016-12-08 07:31:43 -05:00
|
|
|
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)
|
2017-01-10 10:58:11 -05:00
|
|
|
|
|
|
|
if rangesTracker.changes?.length > RangesManager.MAX_CHANGES or rangesTracker.comments?.length > RangesManager.MAX_COMMENTS
|
|
|
|
return callback new Error("too many comments or tracked changes")
|
2017-01-09 08:41:18 -05:00
|
|
|
|
2017-03-15 10:12:06 -04:00
|
|
|
try
|
|
|
|
# This is a consistency check that all of our ranges and
|
|
|
|
# comments still match the corresponding text
|
|
|
|
rangesTracker.validate(newDocLines.join("\n"))
|
|
|
|
catch error
|
|
|
|
logger.error {err: error, project_id, doc_id, newDocLines, updates}, "error validating ranges"
|
|
|
|
return callback(error)
|
|
|
|
|
2017-01-09 08:41:18 -05:00
|
|
|
response = RangesManager._getRanges rangesTracker
|
2018-12-13 06:03:08 -05:00
|
|
|
logger.info {project_id, doc_id, changesCount: response.changes?.length, commentsCount: response.comments?.length}, "applied updates to ranges"
|
2017-01-09 08:41:18 -05:00
|
|
|
callback null, response
|
|
|
|
|
2017-05-05 10:12:06 -04:00
|
|
|
acceptChanges: (change_ids, ranges, callback = (error, ranges) ->) ->
|
2017-05-04 10:32:54 -04:00
|
|
|
{changes, comments} = ranges
|
2018-12-13 06:03:08 -05:00
|
|
|
logger.info "accepting #{ change_ids.length } changes in ranges"
|
2017-05-04 10:32:54 -04:00
|
|
|
rangesTracker = new RangesTracker(changes, comments)
|
2017-05-08 11:08:42 -04:00
|
|
|
rangesTracker.removeChangeIds(change_ids)
|
2017-05-04 10:32:54 -04:00
|
|
|
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
|
2018-12-13 06:03:08 -05:00
|
|
|
logger.info {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
|
2017-01-09 08:41:18 -05:00
|
|
|
|
|
|
|
_getRanges: (rangesTracker) ->
|
2016-12-08 07:31:43 -05:00
|
|
|
# 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
|
2017-01-09 08:41:18 -05:00
|
|
|
return response
|