Send origin metadata through docupdater and project-history when restoring files (#18721)

* add RestoreFileOrigin in overleaf-editor-core

* support source to be an object

* use sourceOrOrigin as param

* rename to originOrSource so the priority is more clear

* get timestamp from version

* fix test

* include version and min_count in getUpdatesFromHistory

* extractOriginOrSource util function

* fix RestoreManagerTests

GitOrigin-RevId: 0ace05a6ade2794c753a9d0bffb4f858ecc6899a
This commit is contained in:
Domagoj Kriskovic
2024-06-18 08:04:34 +00:00
committed by Copybot
parent 5a5defee69
commit 7e8e2b0585
11 changed files with 186 additions and 15 deletions
+2
View File
@@ -22,6 +22,7 @@ const SetFileMetadataOperation = require('./lib/operation/set_file_metadata_oper
const NoOperation = require('./lib/operation/no_operation')
const Operation = require('./lib/operation')
const RestoreOrigin = require('./lib/origin/restore_origin')
const RestoreFileOrigin = require('./lib/origin/restore_file_origin')
const Origin = require('./lib/origin')
const OtClient = require('./lib/ot_client')
const TextOperation = require('./lib/operation/text_operation')
@@ -66,6 +67,7 @@ exports.SetFileMetadataOperation = SetFileMetadataOperation
exports.NoOperation = NoOperation
exports.Operation = Operation
exports.RestoreOrigin = RestoreOrigin
exports.RestoreFileOrigin = RestoreFileOrigin
exports.Origin = Origin
exports.OtClient = OtClient
exports.TextOperation = TextOperation
@@ -5,6 +5,7 @@ const assert = require('check-types').assert
// Dependencies are loaded at the bottom of the file to mitigate circular
// dependency
let RestoreOrigin = null
let RestoreFileOrigin = null
/**
* An Origin records where a {@link Change} came from. The Origin class handles
@@ -31,6 +32,8 @@ class Origin {
static fromRaw(raw) {
if (!raw) return null
if (raw.kind === RestoreOrigin.KIND) return RestoreOrigin.fromRaw(raw)
if (raw.kind === RestoreFileOrigin.KIND)
return RestoreFileOrigin.fromRaw(raw)
return new Origin(raw.kind)
}
@@ -54,3 +57,4 @@ class Origin {
module.exports = Origin
RestoreOrigin = require('./restore_origin')
RestoreFileOrigin = require('./restore_file_origin')
@@ -0,0 +1,62 @@
'use strict'
const assert = require('check-types').assert
const Origin = require('.')
class RestoreFileOrigin extends Origin {
/**
* @param {number} version that was restored
* @param {string} path that was restored
* @param {Date} timestamp from the restored version
*/
constructor(version, path, timestamp) {
assert.integer(version, 'RestoreFileOrigin: bad version')
assert.string(path, 'RestoreFileOrigin: bad path')
assert.date(timestamp, 'RestoreFileOrigin: bad timestamp')
super(RestoreFileOrigin.KIND)
this.version = version
this.path = path
this.timestamp = timestamp
}
static fromRaw(raw) {
return new RestoreFileOrigin(raw.version, raw.path, new Date(raw.timestamp))
}
/** @inheritdoc */
toRaw() {
return {
kind: RestoreFileOrigin.KIND,
version: this.version,
path: this.path,
timestamp: this.timestamp.toISOString(),
}
}
/**
* @return {number}
*/
getVersion() {
return this.version
}
/**
* @return {string}
*/
getPath() {
return this.path
}
/**
* @return {Date}
*/
getTimestamp() {
return this.timestamp
}
}
RestoreFileOrigin.KIND = 'file-restore'
module.exports = RestoreFileOrigin
@@ -34,4 +34,29 @@ describe('Change', function () {
expect(blobHashes.has(File.EMPTY_FILE_HASH)).to.be.true
})
})
describe('RestoreFileOrigin', function () {
it('should convert to and from raw', function () {
const origin = new core.RestoreFileOrigin(1, 'path', new Date())
const raw = origin.toRaw()
const newOrigin = core.Origin.fromRaw(raw)
expect(newOrigin).to.eql(origin)
})
it('change should have a correct origin class', function () {
const change = Change.fromRaw({
operations: [],
timestamp: '2015-03-05T12:03:53.035Z',
authors: [null],
origin: {
kind: 'file-restore',
version: 1,
path: 'path',
timestamp: '2015-03-05T12:03:53.035Z',
},
})
expect(change.getOrigin()).to.be.an.instanceof(core.RestoreFileOrigin)
})
})
})