2024-04-05 06:27:00 -04:00
|
|
|
// @ts-check
|
2024-04-11 08:32:51 -04:00
|
|
|
|
2024-04-12 07:52:27 -04:00
|
|
|
import { callbackify } from 'util'
|
2023-01-13 07:42:29 -05:00
|
|
|
import Core from 'overleaf-editor-core'
|
|
|
|
import { Readable as StringStream } from 'stream'
|
|
|
|
import OError from '@overleaf/o-error'
|
|
|
|
import * as HistoryStoreManager from './HistoryStoreManager.js'
|
|
|
|
import * as WebApiManager from './WebApiManager.js'
|
|
|
|
import * as Errors from './Errors.js'
|
2024-09-06 05:20:13 -04:00
|
|
|
import _ from 'lodash'
|
2023-01-13 07:42:29 -05:00
|
|
|
|
2024-04-12 07:52:27 -04:00
|
|
|
/**
|
2024-09-20 09:52:23 -04:00
|
|
|
* @import { Snapshot } from 'overleaf-editor-core'
|
|
|
|
* @import { RangesSnapshot } from './types'
|
2024-04-12 07:52:27 -04:00
|
|
|
*/
|
2024-04-05 06:27:00 -04:00
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
StringStream.prototype._read = function () {}
|
|
|
|
|
|
|
|
const MAX_REQUESTS = 4 // maximum number of parallel requests to v1 history service
|
|
|
|
|
2024-04-05 06:27:00 -04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} projectId
|
|
|
|
* @param {number} version
|
|
|
|
* @param {string} pathname
|
|
|
|
*/
|
2024-04-12 07:52:27 -04:00
|
|
|
async function getFileSnapshotStream(projectId, version, pathname) {
|
|
|
|
const snapshot = await _getSnapshotAtVersion(projectId, version)
|
2023-01-13 07:42:29 -05:00
|
|
|
|
2024-04-12 07:52:27 -04:00
|
|
|
const file = snapshot.getFile(pathname)
|
|
|
|
if (file == null) {
|
|
|
|
throw new Errors.NotFoundError(`${pathname} not found`, {
|
|
|
|
projectId,
|
|
|
|
version,
|
|
|
|
pathname,
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
2024-04-12 07:52:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const historyId = await WebApiManager.promises.getHistoryId(projectId)
|
|
|
|
if (file.isEditable()) {
|
|
|
|
await file.load('eager', HistoryStoreManager.getBlobStore(historyId))
|
|
|
|
const stream = new StringStream()
|
|
|
|
stream.push(file.getContent({ filterTrackedDeletes: true }))
|
|
|
|
stream.push(null)
|
|
|
|
return stream
|
|
|
|
} else {
|
|
|
|
return await HistoryStoreManager.promises.getProjectBlobStream(
|
|
|
|
historyId,
|
|
|
|
file.getHash()
|
|
|
|
)
|
|
|
|
}
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2024-04-19 09:05:27 -04:00
|
|
|
/**
|
|
|
|
* Constructs a snapshot of the ranges in a document-updater compatible format.
|
|
|
|
* Positions will be relative to a document where tracked deletes have been
|
|
|
|
* removed from the string. This also means that if a tracked delete overlaps
|
|
|
|
* a comment range, the comment range will be truncated.
|
|
|
|
*
|
|
|
|
* @param {string} projectId
|
|
|
|
* @param {number} version
|
|
|
|
* @param {string} pathname
|
|
|
|
* @returns {Promise<RangesSnapshot>}
|
|
|
|
*/
|
|
|
|
async function getRangesSnapshot(projectId, version, pathname) {
|
|
|
|
const snapshot = await _getSnapshotAtVersion(projectId, version)
|
|
|
|
const file = snapshot.getFile(pathname)
|
|
|
|
if (!file) {
|
|
|
|
throw new Errors.NotFoundError(`${pathname} not found`, {
|
|
|
|
projectId,
|
|
|
|
version,
|
|
|
|
pathname,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (!file.isEditable()) {
|
|
|
|
throw new Error('File is not editable')
|
|
|
|
}
|
|
|
|
const historyId = await WebApiManager.promises.getHistoryId(projectId)
|
|
|
|
await file.load('eager', HistoryStoreManager.getBlobStore(historyId))
|
|
|
|
const content = file.getContent()
|
2024-07-05 11:57:26 -04:00
|
|
|
if (content == null) {
|
2024-04-19 09:05:27 -04:00
|
|
|
throw new Error('Unable to read file contents')
|
|
|
|
}
|
|
|
|
const trackedChanges = file.getTrackedChanges().asSorted()
|
2024-05-03 09:27:39 -04:00
|
|
|
const comments = file.getComments().toArray()
|
2024-04-19 09:05:27 -04:00
|
|
|
const docUpdaterCompatibleTrackedChanges = []
|
|
|
|
|
|
|
|
let trackedDeletionOffset = 0
|
|
|
|
for (const trackedChange of trackedChanges) {
|
|
|
|
const isTrackedDeletion = trackedChange.tracking.type === 'delete'
|
|
|
|
const trackedChangeContent = content.slice(
|
|
|
|
trackedChange.range.start,
|
|
|
|
trackedChange.range.end
|
|
|
|
)
|
|
|
|
const tcContent = isTrackedDeletion
|
|
|
|
? { d: trackedChangeContent }
|
|
|
|
: { i: trackedChangeContent }
|
|
|
|
docUpdaterCompatibleTrackedChanges.push({
|
|
|
|
op: {
|
|
|
|
p: trackedChange.range.start - trackedDeletionOffset,
|
|
|
|
...tcContent,
|
|
|
|
},
|
|
|
|
metadata: {
|
|
|
|
ts: trackedChange.tracking.ts.toISOString(),
|
|
|
|
user_id: trackedChange.tracking.userId,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if (isTrackedDeletion) {
|
|
|
|
trackedDeletionOffset += trackedChange.range.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comments are shifted left by the length of any previous tracked deletions.
|
|
|
|
// If they overlap with a tracked deletion, they are truncated.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// { } comment
|
|
|
|
// [ ] tracked deletion
|
|
|
|
// the quic[k {b]rown [fox] jum[ps} ove]r the lazy dog
|
|
|
|
// => rown jum
|
|
|
|
// starting at position 8
|
|
|
|
const trackedDeletions = trackedChanges.filter(
|
|
|
|
tc => tc.tracking.type === 'delete'
|
|
|
|
)
|
|
|
|
const docUpdaterCompatibleComments = []
|
|
|
|
for (const comment of comments) {
|
|
|
|
trackedDeletionOffset = 0
|
|
|
|
let trackedDeletionIndex = 0
|
2024-06-12 08:49:31 -04:00
|
|
|
if (comment.ranges.length === 0) {
|
|
|
|
// Translate detached comments into zero length comments at position 0
|
|
|
|
docUpdaterCompatibleComments.push({
|
|
|
|
op: {
|
|
|
|
p: 0,
|
|
|
|
c: '',
|
|
|
|
t: comment.id,
|
|
|
|
resolved: comment.resolved,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
2024-04-19 09:05:27 -04:00
|
|
|
for (const commentRange of comment.ranges) {
|
|
|
|
let commentRangeContent = ''
|
|
|
|
let offsetFromOverlappingRangeAtStart = 0
|
|
|
|
while (
|
|
|
|
trackedDeletionIndex < trackedDeletions.length &&
|
|
|
|
trackedDeletions[trackedDeletionIndex].range.start <
|
|
|
|
commentRange.start &&
|
|
|
|
trackedDeletions[trackedDeletionIndex].range.end <= commentRange.start
|
|
|
|
) {
|
|
|
|
// Skip over tracked deletions that are before the current comment range
|
|
|
|
trackedDeletionOffset +=
|
|
|
|
trackedDeletions[trackedDeletionIndex].range.length
|
|
|
|
trackedDeletionIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
trackedDeletions[trackedDeletionIndex]?.range.start < commentRange.start
|
|
|
|
) {
|
|
|
|
// There's overlap with a tracked deletion, move the position left and
|
|
|
|
// truncate the overlap
|
|
|
|
offsetFromOverlappingRangeAtStart =
|
|
|
|
commentRange.start -
|
|
|
|
trackedDeletions[trackedDeletionIndex].range.start
|
|
|
|
}
|
|
|
|
|
|
|
|
// The position of the comment in the document after tracked deletions
|
|
|
|
const position =
|
|
|
|
commentRange.start -
|
|
|
|
trackedDeletionOffset -
|
|
|
|
offsetFromOverlappingRangeAtStart
|
|
|
|
|
|
|
|
let cursor = commentRange.start
|
|
|
|
while (cursor < commentRange.end) {
|
|
|
|
const trackedDeletion = trackedDeletions[trackedDeletionIndex]
|
|
|
|
if (
|
|
|
|
!trackedDeletion ||
|
|
|
|
trackedDeletion.range.start >= commentRange.end
|
|
|
|
) {
|
|
|
|
// We've run out of relevant tracked changes
|
|
|
|
commentRangeContent += content.slice(cursor, commentRange.end)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if (trackedDeletion.range.start > cursor) {
|
|
|
|
// There's a gap between the current cursor and the tracked deletion
|
|
|
|
commentRangeContent += content.slice(
|
|
|
|
cursor,
|
|
|
|
trackedDeletion.range.start
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// Skip to the end of the tracked delete
|
|
|
|
cursor = trackedDeletion.range.end
|
|
|
|
trackedDeletionIndex++
|
|
|
|
trackedDeletionOffset += trackedDeletion.range.length
|
|
|
|
}
|
|
|
|
docUpdaterCompatibleComments.push({
|
|
|
|
op: {
|
|
|
|
p: position,
|
|
|
|
c: commentRangeContent,
|
|
|
|
t: comment.id,
|
2024-05-28 09:17:55 -04:00
|
|
|
resolved: comment.resolved,
|
2024-04-19 09:05:27 -04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
changes: docUpdaterCompatibleTrackedChanges,
|
|
|
|
comments: docUpdaterCompatibleComments,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 05:20:13 -04:00
|
|
|
/**
|
|
|
|
* Gets the file metadata at a specific version.
|
|
|
|
*
|
|
|
|
* @param {string} projectId
|
|
|
|
* @param {number} version
|
|
|
|
* @param {string} pathname
|
|
|
|
* @returns {Promise<{metadata: any}>}
|
|
|
|
*/
|
|
|
|
async function getFileMetadataSnapshot(projectId, version, pathname) {
|
|
|
|
const snapshot = await _getSnapshotAtVersion(projectId, version)
|
|
|
|
const file = snapshot.getFile(pathname)
|
|
|
|
if (!file) {
|
|
|
|
throw new Errors.NotFoundError(`${pathname} not found`, {
|
|
|
|
projectId,
|
|
|
|
version,
|
|
|
|
pathname,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const rawMetadata = file.getMetadata()
|
|
|
|
const metadata = _.isEmpty(rawMetadata) ? undefined : rawMetadata
|
|
|
|
|
|
|
|
return { metadata }
|
|
|
|
}
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
// Returns project snapshot containing the document content for files with
|
|
|
|
// text operations in the relevant chunk, and hashes for unmodified/binary
|
|
|
|
// files. Used by git bridge to get the state of the project.
|
2024-04-12 07:52:27 -04:00
|
|
|
async function getProjectSnapshot(projectId, version) {
|
|
|
|
const snapshot = await _getSnapshotAtVersion(projectId, version)
|
|
|
|
const historyId = await WebApiManager.promises.getHistoryId(projectId)
|
|
|
|
await _loadFilesLimit(
|
|
|
|
snapshot,
|
|
|
|
'eager',
|
|
|
|
HistoryStoreManager.getBlobStore(historyId)
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
projectId,
|
|
|
|
files: snapshot.getFileMap().map(file => {
|
|
|
|
if (!file) {
|
|
|
|
return null
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
2024-04-12 07:52:27 -04:00
|
|
|
const content = file.getContent({
|
|
|
|
filterTrackedDeletes: true,
|
|
|
|
})
|
|
|
|
if (content === null) {
|
|
|
|
return { data: { hash: file.getHash() } }
|
|
|
|
}
|
|
|
|
return { data: { content } }
|
|
|
|
}),
|
|
|
|
}
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2024-08-20 09:44:21 -04:00
|
|
|
async function getPathsAtVersion(projectId, version) {
|
|
|
|
const snapshot = await _getSnapshotAtVersion(projectId, version)
|
|
|
|
return {
|
|
|
|
paths: snapshot.getFilePathnames(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 06:27:00 -04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} projectId
|
|
|
|
* @param {number} version
|
|
|
|
*/
|
2024-04-12 07:52:27 -04:00
|
|
|
async function _getSnapshotAtVersion(projectId, version) {
|
|
|
|
const historyId = await WebApiManager.promises.getHistoryId(projectId)
|
|
|
|
const data = await HistoryStoreManager.promises.getChunkAtVersion(
|
|
|
|
projectId,
|
|
|
|
historyId,
|
|
|
|
version
|
|
|
|
)
|
|
|
|
const chunk = Core.Chunk.fromRaw(data.chunk)
|
|
|
|
const snapshot = chunk.getSnapshot()
|
|
|
|
const changes = chunk.getChanges().slice(0, version - chunk.getStartVersion())
|
|
|
|
snapshot.applyAll(changes)
|
|
|
|
return snapshot
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2024-08-21 07:28:21 -04:00
|
|
|
async function getLatestSnapshotFiles(projectId, historyId) {
|
|
|
|
const { snapshot } = await getLatestSnapshot(projectId, historyId)
|
|
|
|
const snapshotFiles = await snapshot.loadFiles(
|
|
|
|
'lazy',
|
|
|
|
HistoryStoreManager.getBlobStore(historyId)
|
|
|
|
)
|
|
|
|
return snapshotFiles
|
|
|
|
}
|
|
|
|
|
2024-04-12 07:52:27 -04:00
|
|
|
async function getLatestSnapshot(projectId, historyId) {
|
|
|
|
const data = await HistoryStoreManager.promises.getMostRecentChunk(
|
|
|
|
projectId,
|
|
|
|
historyId
|
|
|
|
)
|
|
|
|
if (data == null || data.chunk == null) {
|
|
|
|
throw new OError('undefined chunk')
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply all the changes in the chunk to get the current snapshot
|
|
|
|
const chunk = Core.Chunk.fromRaw(data.chunk)
|
|
|
|
const snapshot = chunk.getSnapshot()
|
|
|
|
const changes = chunk.getChanges()
|
|
|
|
snapshot.applyAll(changes)
|
2024-08-21 07:28:21 -04:00
|
|
|
return {
|
|
|
|
snapshot,
|
|
|
|
version: chunk.getEndVersion(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getChangesSince(projectId, historyId, sinceVersion) {
|
|
|
|
const allChanges = []
|
|
|
|
let nextVersion
|
|
|
|
while (true) {
|
|
|
|
let data
|
|
|
|
if (nextVersion) {
|
|
|
|
data = await HistoryStoreManager.promises.getChunkAtVersion(
|
|
|
|
projectId,
|
|
|
|
historyId,
|
|
|
|
nextVersion
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
data = await HistoryStoreManager.promises.getMostRecentChunk(
|
|
|
|
projectId,
|
|
|
|
historyId
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (data == null || data.chunk == null) {
|
|
|
|
throw new OError('undefined chunk')
|
|
|
|
}
|
|
|
|
const chunk = Core.Chunk.fromRaw(data.chunk)
|
|
|
|
if (sinceVersion > chunk.getEndVersion()) {
|
|
|
|
throw new OError('requested version past the end')
|
|
|
|
}
|
|
|
|
const changes = chunk.getChanges()
|
|
|
|
if (chunk.getStartVersion() > sinceVersion) {
|
|
|
|
allChanges.unshift(...changes)
|
|
|
|
nextVersion = chunk.getStartVersion()
|
|
|
|
} else {
|
|
|
|
allChanges.unshift(
|
|
|
|
...changes.slice(sinceVersion - chunk.getStartVersion())
|
|
|
|
)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return allChanges
|
2024-04-12 07:52:27 -04:00
|
|
|
}
|
|
|
|
|
2024-09-23 07:09:01 -04:00
|
|
|
async function getChangesInChunkSince(projectId, historyId, sinceVersion) {
|
|
|
|
const latestChunk = Core.Chunk.fromRaw(
|
|
|
|
(
|
|
|
|
await HistoryStoreManager.promises.getMostRecentChunk(
|
|
|
|
projectId,
|
|
|
|
historyId
|
|
|
|
)
|
|
|
|
).chunk
|
|
|
|
)
|
|
|
|
if (sinceVersion > latestChunk.getEndVersion()) {
|
|
|
|
throw new Errors.BadRequestError(
|
|
|
|
'requested version past the end of the history'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const latestStartVersion = latestChunk.getStartVersion()
|
|
|
|
let chunk = latestChunk
|
|
|
|
if (sinceVersion < latestStartVersion) {
|
|
|
|
chunk = Core.Chunk.fromRaw(
|
|
|
|
(
|
|
|
|
await HistoryStoreManager.promises.getChunkAtVersion(
|
|
|
|
projectId,
|
|
|
|
historyId,
|
|
|
|
sinceVersion
|
|
|
|
)
|
|
|
|
).chunk
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const changes = chunk
|
|
|
|
.getChanges()
|
|
|
|
.slice(sinceVersion - chunk.getStartVersion())
|
|
|
|
return { latestStartVersion, changes }
|
|
|
|
}
|
|
|
|
|
2024-04-12 07:52:27 -04:00
|
|
|
async function _loadFilesLimit(snapshot, kind, blobStore) {
|
|
|
|
await snapshot.fileMap.mapAsync(async file => {
|
|
|
|
// only load changed files or files with tracked changes, others can be
|
|
|
|
// dereferenced from their blobs (this method is only used by the git
|
|
|
|
// bridge which understands how to load blobs).
|
|
|
|
if (!file.isEditable() || (file.getHash() && !file.getRangesHash())) {
|
|
|
|
return
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
2024-04-12 07:52:27 -04:00
|
|
|
await file.load(kind, blobStore)
|
|
|
|
}, MAX_REQUESTS)
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
2024-04-12 07:52:27 -04:00
|
|
|
// EXPORTS
|
|
|
|
|
2024-08-21 07:28:21 -04:00
|
|
|
const getChangesSinceCb = callbackify(getChangesSince)
|
2024-09-23 07:09:01 -04:00
|
|
|
const getChangesInChunkSinceCb = callbackify(getChangesInChunkSince)
|
2024-04-12 07:52:27 -04:00
|
|
|
const getFileSnapshotStreamCb = callbackify(getFileSnapshotStream)
|
|
|
|
const getProjectSnapshotCb = callbackify(getProjectSnapshot)
|
|
|
|
const getLatestSnapshotCb = callbackify(getLatestSnapshot)
|
2024-08-21 07:28:21 -04:00
|
|
|
const getLatestSnapshotFilesCb = callbackify(getLatestSnapshotFiles)
|
2024-04-19 09:05:27 -04:00
|
|
|
const getRangesSnapshotCb = callbackify(getRangesSnapshot)
|
2024-09-06 05:20:13 -04:00
|
|
|
const getFileMetadataSnapshotCb = callbackify(getFileMetadataSnapshot)
|
2024-08-20 09:44:21 -04:00
|
|
|
const getPathsAtVersionCb = callbackify(getPathsAtVersion)
|
2024-04-12 07:52:27 -04:00
|
|
|
|
|
|
|
export {
|
2024-08-21 07:28:21 -04:00
|
|
|
getChangesSinceCb as getChangesSince,
|
2024-09-23 07:09:01 -04:00
|
|
|
getChangesInChunkSinceCb as getChangesInChunkSince,
|
2024-04-12 07:52:27 -04:00
|
|
|
getFileSnapshotStreamCb as getFileSnapshotStream,
|
|
|
|
getProjectSnapshotCb as getProjectSnapshot,
|
2024-09-06 05:20:13 -04:00
|
|
|
getFileMetadataSnapshotCb as getFileMetadataSnapshot,
|
2024-04-12 07:52:27 -04:00
|
|
|
getLatestSnapshotCb as getLatestSnapshot,
|
2024-08-21 07:28:21 -04:00
|
|
|
getLatestSnapshotFilesCb as getLatestSnapshotFiles,
|
2024-04-19 09:05:27 -04:00
|
|
|
getRangesSnapshotCb as getRangesSnapshot,
|
2024-08-20 09:44:21 -04:00
|
|
|
getPathsAtVersionCb as getPathsAtVersion,
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
2024-04-11 08:32:51 -04:00
|
|
|
|
|
|
|
export const promises = {
|
2024-08-21 07:28:21 -04:00
|
|
|
getChangesSince,
|
2024-09-23 07:09:01 -04:00
|
|
|
getChangesInChunkSince,
|
2024-04-12 07:52:27 -04:00
|
|
|
getFileSnapshotStream,
|
|
|
|
getProjectSnapshot,
|
|
|
|
getLatestSnapshot,
|
2024-08-21 07:28:21 -04:00
|
|
|
getLatestSnapshotFiles,
|
2024-04-19 09:05:27 -04:00
|
|
|
getRangesSnapshot,
|
2024-08-20 09:44:21 -04:00
|
|
|
getPathsAtVersion,
|
2024-09-06 05:20:13 -04:00
|
|
|
getFileMetadataSnapshot,
|
2024-04-11 08:32:51 -04:00
|
|
|
}
|