2020-05-06 06:09:15 -04:00
/ * e s l i n t - d i s a b l e
camelcase ,
handle - callback - err ,
* /
// 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
* /
let RangesManager ;
const RangesTracker = require ( "./RangesTracker" ) ;
const logger = require ( "logger-sharelatex" ) ;
const _ = require ( "lodash" ) ;
2016-12-08 07:31:43 -05:00
2020-05-06 06:08:21 -04:00
module . exports = ( RangesManager = {
MAX _COMMENTS : 500 ,
MAX _CHANGES : 2000 ,
2017-01-10 10:58:11 -05:00
2020-05-06 06:08:21 -04:00
applyUpdate ( project _id , doc _id , entries , updates , newDocLines , callback ) {
let error ;
if ( entries == null ) { entries = { } ; }
if ( updates == null ) { updates = [ ] ; }
if ( callback == null ) { callback = function ( error , new _entries , ranges _were _collapsed ) { } ; }
const { changes , comments } = _ . cloneDeep ( entries ) ;
const rangesTracker = new RangesTracker ( changes , comments ) ;
const emptyRangeCountBefore = RangesManager . _emptyRangesCount ( rangesTracker ) ;
2020-05-06 06:09:15 -04:00
for ( const update of Array . from ( updates ) ) {
2020-05-06 06:08:21 -04:00
rangesTracker . track _changes = ! ! update . meta . tc ;
2020-05-06 06:09:15 -04:00
if ( update . meta . tc ) {
2020-05-06 06:08:21 -04:00
rangesTracker . setIdSeed ( update . meta . tc ) ;
}
2020-05-06 06:09:15 -04:00
for ( const op of Array . from ( update . op ) ) {
2020-05-06 06:08:21 -04:00
try {
rangesTracker . applyOp ( op , { user _id : ( update . meta != null ? update . meta . user _id : undefined ) } ) ;
} catch ( error1 ) {
error = error1 ;
return callback ( error ) ;
}
}
}
2017-01-10 10:58:11 -05:00
2020-05-06 06:08:21 -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-01-09 08:41:18 -05:00
2020-05-06 06:08:21 -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-03-15 10:12:06 -04:00
2020-05-06 06:08:21 -04:00
const emptyRangeCountAfter = RangesManager . _emptyRangesCount ( rangesTracker ) ;
const rangesWereCollapsed = emptyRangeCountAfter > emptyRangeCountBefore ;
const response = RangesManager . _getRanges ( rangesTracker ) ;
logger . log ( { project _id , doc _id , changesCount : ( response . changes != null ? response . changes . length : undefined ) , commentsCount : ( response . comments != null ? response . comments . length : undefined ) , rangesWereCollapsed } , "applied updates to ranges" ) ;
return callback ( null , response , rangesWereCollapsed ) ;
} ,
2017-01-09 08:41:18 -05:00
2020-05-06 06:08:21 -04:00
acceptChanges ( change _ids , ranges , callback ) {
if ( callback == null ) { callback = function ( error , ranges ) { } ; }
const { changes , comments } = ranges ;
logger . log ( ` accepting ${ change _ids . length } changes in ranges ` ) ;
const rangesTracker = new RangesTracker ( changes , comments ) ;
rangesTracker . removeChangeIds ( change _ids ) ;
const response = RangesManager . _getRanges ( rangesTracker ) ;
return callback ( null , response ) ;
} ,
2017-05-04 10:32:54 -04:00
2020-05-06 06:08:21 -04:00
deleteComment ( comment _id , ranges , callback ) {
if ( callback == null ) { callback = function ( error , ranges ) { } ; }
const { changes , comments } = ranges ;
logger . log ( { comment _id } , "deleting comment in ranges" ) ;
const rangesTracker = new RangesTracker ( changes , comments ) ;
rangesTracker . removeCommentId ( comment _id ) ;
const response = RangesManager . _getRanges ( rangesTracker ) ;
return callback ( null , response ) ;
} ,
2017-01-09 08:41:18 -05:00
2020-05-06 06:08:21 -04:00
_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 ;
} ,
2019-04-11 08:25:03 -04:00
2020-05-06 06:08:21 -04:00
_emptyRangesCount ( ranges ) {
let count = 0 ;
2020-05-06 06:09:15 -04:00
for ( const comment of Array . from ( ( ranges . comments || [ ] ) ) ) {
2020-05-06 06:08:21 -04:00
if ( comment . op . c === "" ) {
count ++ ;
}
}
2020-05-06 06:09:15 -04:00
for ( const change of Array . from ( ( ranges . changes || [ ] ) ) ) {
2020-05-06 06:08:21 -04:00
if ( change . op . i != null ) {
if ( change . op . i === "" ) {
count ++ ;
}
}
}
return count ;
}
} ) ;