2020-05-06 06:09:15 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:08:21 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-06 06:09:33 -04:00
|
|
|
let RangesManager
|
|
|
|
const RangesTracker = require('./RangesTracker')
|
2021-10-06 05:10:28 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
2022-04-07 05:02:19 -04:00
|
|
|
const Metrics = require('./Metrics')
|
2020-05-06 06:09:33 -04:00
|
|
|
const _ = require('lodash')
|
2016-12-08 07:31:43 -05:00
|
|
|
|
2022-04-07 05:02:19 -04:00
|
|
|
const RANGE_DELTA_BUCKETS = [0, 1, 2, 3, 4, 5, 10, 20, 50]
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
module.exports = RangesManager = {
|
|
|
|
MAX_COMMENTS: 500,
|
|
|
|
MAX_CHANGES: 2000,
|
2017-01-10 10:58:11 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
applyUpdate(project_id, doc_id, entries, updates, newDocLines, callback) {
|
|
|
|
let error
|
|
|
|
if (entries == null) {
|
|
|
|
entries = {}
|
|
|
|
}
|
|
|
|
if (updates == null) {
|
|
|
|
updates = []
|
|
|
|
}
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
const { changes, comments } = _.cloneDeep(entries)
|
|
|
|
const rangesTracker = new RangesTracker(changes, comments)
|
2022-04-07 05:02:19 -04:00
|
|
|
const [emptyRangeCountBefore, totalRangeCountBefore] =
|
|
|
|
RangesManager._emptyRangesCount(rangesTracker)
|
2020-05-06 06:09:33 -04:00
|
|
|
for (const update of Array.from(updates)) {
|
|
|
|
rangesTracker.track_changes = !!update.meta.tc
|
|
|
|
if (update.meta.tc) {
|
|
|
|
rangesTracker.setIdSeed(update.meta.tc)
|
|
|
|
}
|
|
|
|
for (const op of Array.from(update.op)) {
|
|
|
|
try {
|
|
|
|
rangesTracker.applyOp(op, {
|
2021-07-13 07:04:42 -04:00
|
|
|
user_id: update.meta != null ? update.meta.user_id : undefined,
|
2020-05-06 06:09:33 -04:00
|
|
|
})
|
|
|
|
} catch (error1) {
|
|
|
|
error = error1
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-09 08:41:18 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
if (
|
|
|
|
(rangesTracker.changes != null
|
|
|
|
? rangesTracker.changes.length
|
|
|
|
: undefined) > RangesManager.MAX_CHANGES ||
|
|
|
|
(rangesTracker.comments != null
|
|
|
|
? rangesTracker.comments.length
|
|
|
|
: undefined) > RangesManager.MAX_COMMENTS
|
|
|
|
) {
|
|
|
|
return callback(new Error('too many comments or tracked changes'))
|
|
|
|
}
|
2017-03-15 10:12:06 -04:00
|
|
|
|
2020-05-06 06:09:33 -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 (error2) {
|
|
|
|
error = error2
|
|
|
|
logger.error(
|
|
|
|
{ err: error, project_id, doc_id, newDocLines, updates },
|
|
|
|
'error validating ranges'
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
2017-01-09 08:41:18 -05:00
|
|
|
|
2022-04-07 05:02:19 -04:00
|
|
|
const [emptyRangeCountAfter, totalRangeCountAfter] =
|
|
|
|
RangesManager._emptyRangesCount(rangesTracker)
|
2022-05-09 09:07:25 -04:00
|
|
|
const rangesWereCollapsed =
|
|
|
|
emptyRangeCountAfter > emptyRangeCountBefore ||
|
|
|
|
totalRangeCountAfter + 1 < totalRangeCountBefore // also include the case where multiple ranges were removed
|
2022-04-07 05:02:19 -04:00
|
|
|
// monitor the change in range count, we may want to snapshot before large decreases
|
|
|
|
if (totalRangeCountAfter < totalRangeCountBefore) {
|
|
|
|
Metrics.histogram(
|
|
|
|
'range-delta',
|
|
|
|
totalRangeCountBefore - totalRangeCountAfter,
|
|
|
|
RANGE_DELTA_BUCKETS,
|
|
|
|
{ status_code: rangesWereCollapsed ? 'saved' : 'unsaved' }
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
const response = RangesManager._getRanges(rangesTracker)
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2020-05-06 06:09:33 -04:00
|
|
|
{
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
changesCount:
|
|
|
|
response.changes != null ? response.changes.length : undefined,
|
|
|
|
commentsCount:
|
|
|
|
response.comments != null ? response.comments.length : undefined,
|
2021-07-13 07:04:42 -04:00
|
|
|
rangesWereCollapsed,
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
'applied updates to ranges'
|
|
|
|
)
|
|
|
|
return callback(null, response, rangesWereCollapsed)
|
|
|
|
},
|
2017-05-04 10:32:54 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
acceptChanges(change_ids, ranges, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
const { changes, comments } = ranges
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(`accepting ${change_ids.length} changes in ranges`)
|
2020-05-06 06:09:33 -04:00
|
|
|
const rangesTracker = new RangesTracker(changes, comments)
|
|
|
|
rangesTracker.removeChangeIds(change_ids)
|
|
|
|
const response = RangesManager._getRanges(rangesTracker)
|
|
|
|
return callback(null, response)
|
|
|
|
},
|
2019-04-11 08:25:03 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
deleteComment(comment_id, ranges, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
const { changes, comments } = ranges
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug({ comment_id }, 'deleting comment in ranges')
|
2020-05-06 06:09:33 -04:00
|
|
|
const rangesTracker = new RangesTracker(changes, comments)
|
|
|
|
rangesTracker.removeCommentId(comment_id)
|
|
|
|
const response = RangesManager._getRanges(rangesTracker)
|
|
|
|
return callback(null, response)
|
|
|
|
},
|
|
|
|
|
|
|
|
_getRanges(rangesTracker) {
|
|
|
|
// Return the minimal data structure needed, since most documents won't have any
|
|
|
|
// changes or comments
|
|
|
|
let response = {}
|
|
|
|
if (
|
|
|
|
(rangesTracker.changes != null
|
|
|
|
? rangesTracker.changes.length
|
|
|
|
: undefined) > 0
|
|
|
|
) {
|
|
|
|
if (response == null) {
|
|
|
|
response = {}
|
|
|
|
}
|
|
|
|
response.changes = rangesTracker.changes
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(rangesTracker.comments != null
|
|
|
|
? rangesTracker.comments.length
|
|
|
|
: undefined) > 0
|
|
|
|
) {
|
|
|
|
if (response == null) {
|
|
|
|
response = {}
|
|
|
|
}
|
|
|
|
response.comments = rangesTracker.comments
|
|
|
|
}
|
|
|
|
return response
|
|
|
|
},
|
|
|
|
|
|
|
|
_emptyRangesCount(ranges) {
|
2022-04-07 05:02:19 -04:00
|
|
|
let emptyCount = 0
|
|
|
|
let totalCount = 0
|
2020-05-06 06:09:33 -04:00
|
|
|
for (const comment of Array.from(ranges.comments || [])) {
|
2022-04-07 05:02:19 -04:00
|
|
|
totalCount++
|
2020-05-06 06:09:33 -04:00
|
|
|
if (comment.op.c === '') {
|
2022-04-07 05:02:19 -04:00
|
|
|
emptyCount++
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const change of Array.from(ranges.changes || [])) {
|
2022-04-07 05:02:19 -04:00
|
|
|
totalCount++
|
2020-05-06 06:09:33 -04:00
|
|
|
if (change.op.i != null) {
|
|
|
|
if (change.op.i === '') {
|
2022-04-07 05:02:19 -04:00
|
|
|
emptyCount++
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-07 05:02:19 -04:00
|
|
|
return [emptyCount, totalCount]
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|