Merge pull request #17128 from overleaf/em-filter-out-comments

Do not send comments to project-history when ranges support is disabled

GitOrigin-RevId: 0c5e5e2c98ea3c2830ba4d5d114bf4730b440440
This commit is contained in:
Eric Mc Sween 2024-02-16 07:40:13 -05:00 committed by Copybot
parent 8c339045b3
commit b55bdf7ced
6 changed files with 226 additions and 181 deletions

View file

@ -1,3 +1,4 @@
// @ts-check
/** /**
* The purpose of this class is to track a set of inserts and deletes to a document, like * The purpose of this class is to track a set of inserts and deletes to a document, like
* track changes in Word. We store these as a set of ShareJs style ranges: * track changes in Word. We store these as a set of ShareJs style ranges:
@ -44,8 +45,21 @@ class RangesTracker {
comments = [] comments = []
} }
this.comments = comments this.comments = comments
this.setIdSeed(RangesTracker.generateIdSeed()) this.track_changes = false
this.resetDirtyState() this.id_seed = RangesTracker.generateIdSeed()
this.id_increment = 0
this._dirtyState = {
comment: {
moved: {},
removed: {},
added: {},
},
change: {
moved: {},
removed: {},
added: {},
},
}
} }
getIdSeed() { getIdSeed() {

View file

@ -37,9 +37,11 @@ const RangesManager = {
* @param {Ranges} ranges - ranges before the updates were applied * @param {Ranges} ranges - ranges before the updates were applied
* @param {Update[]} updates * @param {Update[]} updates
* @param {string[]} newDocLines - the document lines after the updates were applied * @param {string[]} newDocLines - the document lines after the updates were applied
* @param {object} opts
* @param {boolean} [opts.historyRangesSupport] - whether history ranges support is enabled
* @returns {{ newRanges: Ranges, rangesWereCollapsed: boolean, historyUpdates: HistoryUpdate[] }} * @returns {{ newRanges: Ranges, rangesWereCollapsed: boolean, historyUpdates: HistoryUpdate[] }}
*/ */
applyUpdate(projectId, docId, ranges, updates, newDocLines) { applyUpdate(projectId, docId, ranges, updates, newDocLines, opts = {}) {
if (ranges == null) { if (ranges == null) {
ranges = {} ranges = {}
} }
@ -58,9 +60,13 @@ const RangesManager = {
} }
const historyOps = [] const historyOps = []
for (const op of update.op) { for (const op of update.op) {
if (opts.historyRangesSupport) {
historyOps.push( historyOps.push(
getHistoryOp(op, rangesTracker.comments, rangesTracker.changes) getHistoryOp(op, rangesTracker.comments, rangesTracker.changes)
) )
} else if (isInsert(op) || isDelete(op)) {
historyOps.push(op)
}
rangesTracker.applyOp(op, { user_id: update.meta?.user_id }) rangesTracker.applyOp(op, { user_id: update.meta?.user_id })
} }
historyUpdates.push({ ...update, op: historyOps }) historyUpdates.push({ ...update, op: historyOps })

View file

@ -141,20 +141,15 @@ const UpdateManager = {
sync: incomingUpdateVersion === previousVersion, sync: incomingUpdateVersion === previousVersion,
}) })
let { newRanges, rangesWereCollapsed, historyUpdates } = const { newRanges, rangesWereCollapsed, historyUpdates } =
RangesManager.applyUpdate( RangesManager.applyUpdate(
projectId, projectId,
docId, docId,
ranges, ranges,
appliedOps, appliedOps,
updatedDocLines updatedDocLines,
{ historyRangesSupport }
) )
if (!historyRangesSupport) {
// The document has not been transitioned to include comments and
// tracked changes in its history. Send regular updates rather than the
// full history updates.
historyUpdates = appliedOps
}
profile.log('RangesManager.applyUpdate', { sync: true }) profile.log('RangesManager.applyUpdate', { sync: true })
await RedisManager.promises.updateDocument( await RedisManager.promises.updateDocument(

View file

@ -237,6 +237,30 @@ describe('RangesManager', function () {
}) })
}) })
describe('with comment updates', function () {
beforeEach(function () {
this.updates = makeUpdates([
{ i: 'two ', p: 4 },
{ c: 'one', p: 0 },
])
this.ranges = {}
this.result = this.RangesManager.applyUpdate(
this.project_id,
this.doc_id,
this.ranges,
this.updates,
this.newDocLines
)
})
it('should not send comments to the history', function () {
expect(this.result.historyUpdates[0].op).to.deep.equal([
{ i: 'two ', p: 4 },
])
})
})
describe('with history ranges support', function () {
describe('inserts among tracked deletes', function () { describe('inserts among tracked deletes', function () {
beforeEach(function () { beforeEach(function () {
// original text is "on[1]e[22] [333](three) fo[4444]ur five" // original text is "on[1]e[22] [333](three) fo[4444]ur five"
@ -261,7 +285,8 @@ describe('RangesManager', function () {
this.doc_id, this.doc_id,
this.ranges, this.ranges,
this.updates, this.updates,
this.newDocLines this.newDocLines,
{ historyRangesSupport: true }
) )
}) })
@ -288,7 +313,8 @@ describe('RangesManager', function () {
this.doc_id, this.doc_id,
this.ranges, this.ranges,
this.updates, this.updates,
this.newDocLines this.newDocLines,
{ historyRangesSupport: true }
) )
}) })
@ -325,7 +351,8 @@ describe('RangesManager', function () {
this.doc_id, this.doc_id,
this.ranges, this.ranges,
this.updates, this.updates,
this.newDocLines this.newDocLines,
{ historyRangesSupport: true }
) )
}) })
@ -375,7 +402,8 @@ describe('RangesManager', function () {
this.doc_id, this.doc_id,
this.ranges, this.ranges,
this.updates, this.updates,
this.newDocLines this.newDocLines,
{ historyRangesSupport: true }
) )
}) })
@ -410,7 +438,8 @@ describe('RangesManager', function () {
this.doc_id, this.doc_id,
this.ranges, this.ranges,
this.updates, this.updates,
this.newDocLines this.newDocLines,
{ historyRangesSupport: true }
) )
}) })
@ -430,6 +459,7 @@ describe('RangesManager', function () {
}) })
}) })
}) })
})
describe('acceptChanges', function () { describe('acceptChanges', function () {
beforeEach(function () { beforeEach(function () {

View file

@ -396,7 +396,7 @@ describe('UpdateManager', function () {
it('should add metadata to the ops', function () { it('should add metadata to the ops', function () {
this.UpdateManager.promises._addMetadataToHistoryUpdates.should.have.been.calledWith( this.UpdateManager.promises._addMetadataToHistoryUpdates.should.have.been.calledWith(
this.appliedOps, this.historyUpdates,
this.pathname, this.pathname,
this.projectHistoryId, this.projectHistoryId,
this.lines this.lines
@ -406,12 +406,12 @@ describe('UpdateManager', function () {
it('should push the applied ops into the history queue', function () { it('should push the applied ops into the history queue', function () {
this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWith( this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWith(
this.project_id, this.project_id,
...this.appliedOps.map(op => JSON.stringify(op)) ...this.historyUpdates.map(op => JSON.stringify(op))
) )
this.HistoryManager.recordAndFlushHistoryOps.should.have.been.calledWith( this.HistoryManager.recordAndFlushHistoryOps.should.have.been.calledWith(
this.project_id, this.project_id,
this.appliedOps, this.historyUpdates,
this.appliedOps.length this.historyUpdates.length
) )
}) })
}) })

View file

@ -722,7 +722,7 @@ export class DocumentContainer extends EventEmitter {
this.emit('ranges:clear') this.emit('ranges:clear')
this.ranges!.changes = changes this.ranges!.changes = changes
this.ranges!.comments = comments this.ranges!.comments = comments
this.ranges!.track_changes = this.doc?.track_changes this.ranges!.track_changes = this.doc?.track_changes ?? false
for (const op of this.filterOps(this.doc?.getInflightOp() || [])) { for (const op of this.filterOps(this.doc?.getInflightOp() || [])) {
this.ranges!.setIdSeed(this.doc?.track_changes_id_seeds?.inflight) this.ranges!.setIdSeed(this.doc?.track_changes_id_seeds?.inflight)
this.ranges!.applyOp(op, { user_id: this.track_changes_as }) this.ranges!.applyOp(op, { user_id: this.track_changes_as })