Merge pull request #13574 from overleaf/msm-fix-history-anon

Fix 'anonymous-user' edits breaking history

GitOrigin-RevId: 4141477e7987c0354a419d2c5d8203b6efcf673d
This commit is contained in:
Miguel Serrano 2023-07-03 13:11:51 +02:00 committed by Copybot
parent 01e3409eb4
commit 21eada08ef
2 changed files with 95 additions and 1 deletions

View file

@ -73,9 +73,18 @@ function _convertToChange(projectId, updateWithBlob) {
throw error
}
let v2Authors
if (update.meta.user_id === 'anonymous-user') {
// history-v1 uses null to represent an anonymous author
v2Authors = [null]
} else {
// user_id is missing on resync operations that update the contents of a doc
v2Authors = _.compact([update.meta.user_id])
}
const rawChange = {
operations,
v2Authors: _.compact([update.meta.user_id]),
v2Authors,
timestamp: new Date(update.meta.ts).toISOString(),
projectVersion,
v2DocVersions: Object.keys(v2DocVersions).length ? v2DocVersions : null,

View file

@ -447,6 +447,91 @@ describe('UpdateTranslator', function () {
)
})
it('sets a null author when user_id is "anonymous-user"', function (done) {
const updates = [
{
update: {
doc: this.doc_id,
pathname: '/main.tex',
docLines: 'a\nb',
meta: {
user_id: 'anonymous-user',
ts: this.timestamp,
},
},
blobHash: this.mockBlobHash,
},
]
const assertion = (error, changes) => {
changes = changes.map(change => change.toRaw())
expect(error).to.be.null
expect(changes).to.deep.equal([
{
authors: [],
operations: [
{
pathname: 'main.tex',
file: {
hash: this.mockBlobHash,
},
},
],
v2Authors: [null],
timestamp: this.timestamp,
},
])
done()
}
this.UpdateTranslator.convertToChanges(
this.project_id,
updates,
assertion
)
})
it('sets an empty array as author when there is no meta.user_id', function (done) {
const updates = [
{
update: {
doc: this.doc_id,
pathname: '/main.tex',
docLines: 'a\nb',
meta: {
ts: this.timestamp,
},
},
blobHash: this.mockBlobHash,
},
]
const assertion = (error, changes) => {
changes = changes.map(change => change.toRaw())
expect(error).to.be.null
expect(changes).to.deep.equal([
{
authors: [],
operations: [
{
pathname: 'main.tex',
file: {
hash: this.mockBlobHash,
},
},
],
v2Authors: [],
timestamp: this.timestamp,
},
])
done()
}
this.UpdateTranslator.convertToChanges(
this.project_id,
updates,
assertion
)
})
describe('text updates', function () {
it('can translate insertions', function (done) {
const updates = [