2024-04-08 11:07:41 -04:00
|
|
|
// @ts-check
|
|
|
|
|
2021-07-12 12:47:15 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2024-04-03 10:08:16 -04:00
|
|
|
const { callbackifyAll } = require('@overleaf/promise-utils')
|
2022-07-28 04:23:50 -04:00
|
|
|
const projectHistoryKeys = Settings.redis?.project_history?.key_schema
|
2020-11-10 06:32:04 -05:00
|
|
|
const rclient = require('@overleaf/redis-wrapper').createClient(
|
2020-05-06 06:09:33 -04:00
|
|
|
Settings.redis.project_history
|
|
|
|
)
|
2021-10-06 05:10:28 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-05-06 06:09:33 -04:00
|
|
|
const metrics = require('./Metrics')
|
2021-11-25 04:25:28 -05:00
|
|
|
const { docIsTooLarge } = require('./Limits')
|
2024-06-17 08:54:11 -04:00
|
|
|
const { addTrackedDeletesToContent, extractOriginOrSource } = require('./Utils')
|
2024-05-02 11:12:13 -04:00
|
|
|
const HistoryConversions = require('./HistoryConversions')
|
2024-04-03 10:08:16 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2024-04-08 11:07:41 -04:00
|
|
|
/**
|
2024-09-20 09:52:23 -04:00
|
|
|
* @import { Ranges } from './types'
|
2024-04-08 11:07:41 -04:00
|
|
|
*/
|
|
|
|
|
2024-01-30 10:35:54 -05:00
|
|
|
const ProjectHistoryRedisManager = {
|
2024-04-03 10:08:16 -04:00
|
|
|
async queueOps(projectId, ...ops) {
|
2020-05-06 06:09:33 -04:00
|
|
|
// Record metric for ops pushed onto queue
|
2022-08-25 08:01:39 -04:00
|
|
|
for (const op of ops) {
|
2020-05-06 06:09:33 -04:00
|
|
|
metrics.summary('redis.projectHistoryOps', op.length, { status: 'push' })
|
|
|
|
}
|
2024-05-29 11:50:34 -04:00
|
|
|
|
|
|
|
// Make sure that this MULTI operation only operates on project
|
|
|
|
// specific keys, i.e. keys that have the project id in curly braces.
|
|
|
|
// The curly braces identify a hash key for Redis and ensures that
|
|
|
|
// the MULTI's operations are all done on the same node in a
|
|
|
|
// cluster environment.
|
2020-05-06 06:09:33 -04:00
|
|
|
const multi = rclient.multi()
|
|
|
|
// Push the ops onto the project history queue
|
|
|
|
multi.rpush(
|
2022-08-25 08:01:39 -04:00
|
|
|
projectHistoryKeys.projectHistoryOps({ project_id: projectId }),
|
|
|
|
...ops
|
2020-05-06 06:09:33 -04:00
|
|
|
)
|
|
|
|
// To record the age of the oldest op on the queue set a timestamp if not
|
|
|
|
// already present (SETNX).
|
|
|
|
multi.setnx(
|
2022-08-25 08:01:39 -04:00
|
|
|
projectHistoryKeys.projectHistoryFirstOpTimestamp({
|
|
|
|
project_id: projectId,
|
|
|
|
}),
|
2020-05-06 06:09:33 -04:00
|
|
|
Date.now()
|
|
|
|
)
|
2024-04-03 10:08:16 -04:00
|
|
|
const result = await multi.exec()
|
|
|
|
return result[0]
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
2018-07-20 05:43:31 -04:00
|
|
|
|
2024-04-03 10:08:16 -04:00
|
|
|
async queueRenameEntity(
|
2022-08-25 08:01:39 -04:00
|
|
|
projectId,
|
2020-05-06 06:09:33 -04:00
|
|
|
projectHistoryId,
|
2022-08-25 08:01:39 -04:00
|
|
|
entityType,
|
|
|
|
entityId,
|
|
|
|
userId,
|
2020-05-06 06:09:33 -04:00
|
|
|
projectUpdate,
|
2024-06-17 08:54:11 -04:00
|
|
|
originOrSource
|
2020-05-06 06:09:33 -04:00
|
|
|
) {
|
|
|
|
projectUpdate = {
|
|
|
|
pathname: projectUpdate.pathname,
|
|
|
|
new_pathname: projectUpdate.newPathname,
|
|
|
|
meta: {
|
2022-08-25 08:01:39 -04:00
|
|
|
user_id: userId,
|
2021-07-13 07:04:42 -04:00
|
|
|
ts: new Date(),
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
version: projectUpdate.version,
|
2021-07-13 07:04:42 -04:00
|
|
|
projectHistoryId,
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2022-08-25 08:01:39 -04:00
|
|
|
projectUpdate[entityType] = entityId
|
2024-06-17 08:54:11 -04:00
|
|
|
|
|
|
|
const { origin, source } = extractOriginOrSource(originOrSource)
|
|
|
|
|
|
|
|
if (origin != null) {
|
|
|
|
projectUpdate.meta.origin = origin
|
|
|
|
if (origin.kind !== 'editor') {
|
|
|
|
projectUpdate.meta.type = 'external'
|
|
|
|
}
|
|
|
|
} else if (source != null) {
|
2022-08-25 08:01:39 -04:00
|
|
|
projectUpdate.meta.source = source
|
|
|
|
if (source !== 'editor') {
|
|
|
|
projectUpdate.meta.type = 'external'
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2022-08-25 08:01:39 -04:00
|
|
|
{ projectId, projectUpdate },
|
2020-05-06 06:09:33 -04:00
|
|
|
'queue rename operation to project-history'
|
|
|
|
)
|
|
|
|
const jsonUpdate = JSON.stringify(projectUpdate)
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2024-04-03 10:08:16 -04:00
|
|
|
return await ProjectHistoryRedisManager.queueOps(projectId, jsonUpdate)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2024-04-03 10:08:16 -04:00
|
|
|
async queueAddEntity(
|
2022-08-25 08:01:39 -04:00
|
|
|
projectId,
|
2020-05-06 06:09:33 -04:00
|
|
|
projectHistoryId,
|
2022-08-25 08:01:39 -04:00
|
|
|
entityType,
|
|
|
|
entityId,
|
|
|
|
userId,
|
2020-05-06 06:09:33 -04:00
|
|
|
projectUpdate,
|
2024-06-17 08:54:11 -04:00
|
|
|
originOrSource
|
2020-05-06 06:09:33 -04:00
|
|
|
) {
|
2024-05-28 09:17:55 -04:00
|
|
|
let docLines = projectUpdate.docLines
|
|
|
|
let ranges
|
|
|
|
if (projectUpdate.historyRangesSupport && projectUpdate.ranges) {
|
|
|
|
docLines = addTrackedDeletesToContent(
|
|
|
|
docLines,
|
|
|
|
projectUpdate.ranges.changes ?? []
|
|
|
|
)
|
|
|
|
ranges = HistoryConversions.toHistoryRanges(projectUpdate.ranges)
|
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
projectUpdate = {
|
|
|
|
pathname: projectUpdate.pathname,
|
2024-05-28 09:17:55 -04:00
|
|
|
docLines,
|
2020-05-06 06:09:33 -04:00
|
|
|
url: projectUpdate.url,
|
|
|
|
meta: {
|
2022-08-25 08:01:39 -04:00
|
|
|
user_id: userId,
|
2021-07-13 07:04:42 -04:00
|
|
|
ts: new Date(),
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
version: projectUpdate.version,
|
2024-08-05 04:52:40 -04:00
|
|
|
hash: projectUpdate.hash,
|
2024-08-05 04:52:23 -04:00
|
|
|
metadata: projectUpdate.metadata,
|
2021-07-13 07:04:42 -04:00
|
|
|
projectHistoryId,
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2024-05-28 09:17:55 -04:00
|
|
|
if (ranges) {
|
|
|
|
projectUpdate.ranges = ranges
|
|
|
|
}
|
2022-08-25 08:01:39 -04:00
|
|
|
projectUpdate[entityType] = entityId
|
2024-06-17 08:54:11 -04:00
|
|
|
|
|
|
|
const { origin, source } = extractOriginOrSource(originOrSource)
|
|
|
|
|
|
|
|
if (origin != null) {
|
|
|
|
projectUpdate.meta.origin = origin
|
|
|
|
if (origin.kind !== 'editor') {
|
|
|
|
projectUpdate.meta.type = 'external'
|
|
|
|
}
|
|
|
|
} else if (source != null) {
|
2022-08-25 08:01:39 -04:00
|
|
|
projectUpdate.meta.source = source
|
|
|
|
if (source !== 'editor') {
|
|
|
|
projectUpdate.meta.type = 'external'
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2022-08-25 08:01:39 -04:00
|
|
|
{ projectId, projectUpdate },
|
2020-05-06 06:09:33 -04:00
|
|
|
'queue add operation to project-history'
|
|
|
|
)
|
|
|
|
const jsonUpdate = JSON.stringify(projectUpdate)
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2024-04-03 10:08:16 -04:00
|
|
|
return await ProjectHistoryRedisManager.queueOps(projectId, jsonUpdate)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2024-04-03 10:08:16 -04:00
|
|
|
async queueResyncProjectStructure(projectId, projectHistoryId, docs, files) {
|
2022-08-25 08:01:39 -04:00
|
|
|
logger.debug({ projectId, docs, files }, 'queue project structure resync')
|
2020-05-06 06:09:33 -04:00
|
|
|
const projectUpdate = {
|
|
|
|
resyncProjectStructure: { docs, files },
|
|
|
|
projectHistoryId,
|
|
|
|
meta: {
|
2021-07-13 07:04:42 -04:00
|
|
|
ts: new Date(),
|
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
const jsonUpdate = JSON.stringify(projectUpdate)
|
2024-04-03 10:08:16 -04:00
|
|
|
return await ProjectHistoryRedisManager.queueOps(projectId, jsonUpdate)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2024-04-08 11:07:41 -04:00
|
|
|
/**
|
|
|
|
* Add a resync doc update to the project-history queue
|
|
|
|
*
|
|
|
|
* @param {string} projectId
|
|
|
|
* @param {string} projectHistoryId
|
|
|
|
* @param {string} docId
|
|
|
|
* @param {string[]} lines
|
|
|
|
* @param {Ranges} ranges
|
2024-05-28 07:24:06 -04:00
|
|
|
* @param {string[]} resolvedCommentIds
|
2024-04-08 11:07:41 -04:00
|
|
|
* @param {number} version
|
|
|
|
* @param {string} pathname
|
|
|
|
* @param {boolean} historyRangesSupport
|
|
|
|
* @return {Promise<number>} the number of ops added
|
|
|
|
*/
|
2024-04-03 10:08:16 -04:00
|
|
|
async queueResyncDocContent(
|
2022-08-25 08:01:39 -04:00
|
|
|
projectId,
|
2020-05-06 06:09:33 -04:00
|
|
|
projectHistoryId,
|
2022-08-25 08:01:39 -04:00
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
lines,
|
2024-04-08 11:07:41 -04:00
|
|
|
ranges,
|
2024-05-28 07:24:06 -04:00
|
|
|
resolvedCommentIds,
|
2020-05-06 06:09:33 -04:00
|
|
|
version,
|
2024-04-08 11:07:41 -04:00
|
|
|
pathname,
|
|
|
|
historyRangesSupport
|
2020-05-06 06:09:33 -04:00
|
|
|
) {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2022-08-25 08:01:39 -04:00
|
|
|
{ projectId, docId, lines, version, pathname },
|
2020-05-06 06:09:33 -04:00
|
|
|
'queue doc content resync'
|
|
|
|
)
|
2024-04-08 11:07:41 -04:00
|
|
|
|
|
|
|
let content = lines.join('\n')
|
|
|
|
if (historyRangesSupport) {
|
|
|
|
content = addTrackedDeletesToContent(content, ranges.changes ?? [])
|
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const projectUpdate = {
|
2024-04-08 11:07:41 -04:00
|
|
|
resyncDocContent: { content, version },
|
2020-05-06 06:09:33 -04:00
|
|
|
projectHistoryId,
|
|
|
|
path: pathname,
|
2022-08-25 08:01:39 -04:00
|
|
|
doc: docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
meta: {
|
2021-07-13 07:04:42 -04:00
|
|
|
ts: new Date(),
|
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2024-04-08 11:07:48 -04:00
|
|
|
|
|
|
|
if (historyRangesSupport) {
|
2024-05-02 11:12:13 -04:00
|
|
|
projectUpdate.resyncDocContent.ranges =
|
|
|
|
HistoryConversions.toHistoryRanges(ranges)
|
2024-05-28 07:24:06 -04:00
|
|
|
projectUpdate.resyncDocContent.resolvedCommentIds = resolvedCommentIds
|
2024-04-08 11:07:48 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const jsonUpdate = JSON.stringify(projectUpdate)
|
2021-11-25 04:25:28 -05:00
|
|
|
// Do an optimised size check on the docLines using the serialised
|
|
|
|
// project update length as an upper bound
|
|
|
|
const sizeBound = jsonUpdate.length
|
|
|
|
if (docIsTooLarge(sizeBound, lines, Settings.max_doc_length)) {
|
2024-04-03 10:08:16 -04:00
|
|
|
throw new OError(
|
|
|
|
'blocking resync doc content insert into project history queue: doc is too large',
|
|
|
|
{ projectId, docId, docSize: sizeBound }
|
2021-11-25 04:25:28 -05:00
|
|
|
)
|
|
|
|
}
|
2024-04-03 10:08:16 -04:00
|
|
|
return await ProjectHistoryRedisManager.queueOps(projectId, jsonUpdate)
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2024-01-30 10:35:54 -05:00
|
|
|
|
2024-04-03 10:08:16 -04:00
|
|
|
module.exports = {
|
|
|
|
...callbackifyAll(ProjectHistoryRedisManager),
|
|
|
|
promises: ProjectHistoryRedisManager,
|
|
|
|
}
|