2020-05-06 06:09:15 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:08:21 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS201: Simplify complex destructure assignments
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-06 06:09:33 -04:00
|
|
|
let ProjectHistoryRedisManager
|
2021-07-12 12:47:15 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-05-06 06:09:33 -04:00
|
|
|
const projectHistoryKeys = __guard__(
|
|
|
|
Settings.redis != null ? Settings.redis.project_history : undefined,
|
2021-07-13 07:04:42 -04:00
|
|
|
x => x.key_schema
|
2020-05-06 06:09:33 -04:00
|
|
|
)
|
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')
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
module.exports = ProjectHistoryRedisManager = {
|
|
|
|
queueOps(project_id, ...rest) {
|
|
|
|
// Record metric for ops pushed onto queue
|
2021-10-27 05:49:18 -04:00
|
|
|
const callback = rest.pop()
|
|
|
|
const ops = rest
|
2020-05-06 06:09:33 -04:00
|
|
|
for (const op of Array.from(ops)) {
|
|
|
|
metrics.summary('redis.projectHistoryOps', op.length, { status: 'push' })
|
|
|
|
}
|
|
|
|
const multi = rclient.multi()
|
|
|
|
// Push the ops onto the project history queue
|
|
|
|
multi.rpush(
|
|
|
|
projectHistoryKeys.projectHistoryOps({ project_id }),
|
|
|
|
...Array.from(ops)
|
|
|
|
)
|
|
|
|
// To record the age of the oldest op on the queue set a timestamp if not
|
|
|
|
// already present (SETNX).
|
|
|
|
multi.setnx(
|
|
|
|
projectHistoryKeys.projectHistoryFirstOpTimestamp({ project_id }),
|
|
|
|
Date.now()
|
|
|
|
)
|
|
|
|
return multi.exec(function (error, result) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
// return the number of entries pushed onto the project history queue
|
|
|
|
return callback(null, result[0])
|
|
|
|
})
|
|
|
|
},
|
2018-07-20 05:43:31 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
queueRenameEntity(
|
|
|
|
project_id,
|
|
|
|
projectHistoryId,
|
|
|
|
entity_type,
|
|
|
|
entity_id,
|
|
|
|
user_id,
|
|
|
|
projectUpdate,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
projectUpdate = {
|
|
|
|
pathname: projectUpdate.pathname,
|
|
|
|
new_pathname: projectUpdate.newPathname,
|
|
|
|
meta: {
|
|
|
|
user_id,
|
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
|
|
|
}
|
|
|
|
projectUpdate[entity_type] = entity_id
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2020-05-06 06:09:33 -04:00
|
|
|
{ project_id, projectUpdate },
|
|
|
|
'queue rename operation to project-history'
|
|
|
|
)
|
|
|
|
const jsonUpdate = JSON.stringify(projectUpdate)
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
return ProjectHistoryRedisManager.queueOps(project_id, jsonUpdate, callback)
|
|
|
|
},
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
queueAddEntity(
|
|
|
|
project_id,
|
|
|
|
projectHistoryId,
|
|
|
|
entity_type,
|
|
|
|
entitiy_id,
|
|
|
|
user_id,
|
|
|
|
projectUpdate,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
projectUpdate = {
|
|
|
|
pathname: projectUpdate.pathname,
|
|
|
|
docLines: projectUpdate.docLines,
|
|
|
|
url: projectUpdate.url,
|
|
|
|
meta: {
|
|
|
|
user_id,
|
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
|
|
|
}
|
|
|
|
projectUpdate[entity_type] = entitiy_id
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2020-05-06 06:09:33 -04:00
|
|
|
{ project_id, projectUpdate },
|
|
|
|
'queue add operation to project-history'
|
|
|
|
)
|
|
|
|
const jsonUpdate = JSON.stringify(projectUpdate)
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
return ProjectHistoryRedisManager.queueOps(project_id, jsonUpdate, callback)
|
|
|
|
},
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
queueResyncProjectStructure(
|
|
|
|
project_id,
|
|
|
|
projectHistoryId,
|
|
|
|
docs,
|
|
|
|
files,
|
|
|
|
callback
|
|
|
|
) {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug({ project_id, 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)
|
|
|
|
return ProjectHistoryRedisManager.queueOps(project_id, jsonUpdate, callback)
|
|
|
|
},
|
2018-03-09 09:14:14 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
queueResyncDocContent(
|
|
|
|
project_id,
|
|
|
|
projectHistoryId,
|
|
|
|
doc_id,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
pathname,
|
|
|
|
callback
|
|
|
|
) {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2020-05-06 06:09:33 -04:00
|
|
|
{ project_id, doc_id, lines, version, pathname },
|
|
|
|
'queue doc content resync'
|
|
|
|
)
|
|
|
|
const projectUpdate = {
|
|
|
|
resyncDocContent: {
|
|
|
|
content: lines.join('\n'),
|
2021-07-13 07:04:42 -04:00
|
|
|
version,
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
projectHistoryId,
|
|
|
|
path: pathname,
|
|
|
|
doc: doc_id,
|
|
|
|
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)
|
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)) {
|
|
|
|
const err = new Error(
|
|
|
|
'blocking resync doc content insert into project history queue: doc is too large'
|
|
|
|
)
|
|
|
|
logger.error({ project_id, doc_id, err, docSize: sizeBound }, err.message)
|
|
|
|
return callback(err)
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return ProjectHistoryRedisManager.queueOps(project_id, jsonUpdate, callback)
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
|
|
|
function __guard__(value, transform) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|