2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
2020-12-15 05:23:54 -05:00
|
|
|
node/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const { Project } = require('../../models/Project')
|
|
|
|
const ProjectDetailsHandler = require('./ProjectDetailsHandler')
|
|
|
|
const logger = require('logger-sharelatex')
|
2021-07-07 05:38:56 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2019-05-29 05:21:06 -04:00
|
|
|
const HistoryManager = require('../History/HistoryManager')
|
|
|
|
const ProjectEntityUpdateHandler = require('./ProjectEntityUpdateHandler')
|
2019-10-23 08:24:01 -04:00
|
|
|
const { promisifyAll } = require('../../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-10-23 08:24:01 -04:00
|
|
|
const ProjectHistoryHandler = {
|
2019-05-29 05:21:06 -04:00
|
|
|
setHistoryId(project_id, history_id, callback) {
|
|
|
|
// reject invalid history ids
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
if (!history_id || typeof history_id !== 'number') {
|
|
|
|
return callback(new Error('invalid history id'))
|
|
|
|
}
|
|
|
|
// use $exists:false to prevent overwriting any existing history id, atomically
|
2020-11-03 04:19:05 -05:00
|
|
|
return Project.updateOne(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ _id: project_id, 'overleaf.history.id': { $exists: false } },
|
|
|
|
{ 'overleaf.history.id': history_id },
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, result) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if ((result != null ? result.n : undefined) === 0) {
|
|
|
|
return callback(new Error('history exists'))
|
|
|
|
}
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getHistoryId(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err, result) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return ProjectDetailsHandler.getDetails(
|
|
|
|
project_id,
|
|
|
|
function (err, project) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
} // n.b. getDetails returns an error if the project doesn't exist
|
|
|
|
return callback(
|
|
|
|
null,
|
2019-05-29 05:21:06 -04:00
|
|
|
__guard__(
|
2021-04-14 09:17:21 -04:00
|
|
|
__guard__(
|
|
|
|
project != null ? project.overleaf : undefined,
|
|
|
|
x1 => x1.history
|
|
|
|
),
|
|
|
|
x => x.id
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2021-04-14 09:17:21 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
upgradeHistory(project_id, callback) {
|
|
|
|
// project must have an overleaf.history.id before allowing display of new history
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err, result) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-11-03 04:19:05 -05:00
|
|
|
return Project.updateOne(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ _id: project_id, 'overleaf.history.id': { $exists: true } },
|
|
|
|
{
|
|
|
|
'overleaf.history.display': true,
|
2021-04-27 03:52:58 -04:00
|
|
|
'overleaf.history.upgradedAt': new Date(),
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, result) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
// return an error if overleaf.history.id wasn't present
|
|
|
|
if ((result != null ? result.n : undefined) === 0) {
|
|
|
|
return callback(new Error('history not upgraded'))
|
|
|
|
}
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
downgradeHistory(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err, result) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-11-03 04:19:05 -05:00
|
|
|
return Project.updateOne(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ _id: project_id, 'overleaf.history.upgradedAt': { $exists: true } },
|
|
|
|
{
|
|
|
|
'overleaf.history.display': false,
|
2021-04-27 03:52:58 -04:00
|
|
|
$unset: { 'overleaf.history.upgradedAt': 1 },
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, result) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if ((result != null ? result.n : undefined) === 0) {
|
|
|
|
return callback(new Error('history not downgraded'))
|
|
|
|
}
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
ensureHistoryExistsForProject(project_id, callback) {
|
|
|
|
// We can only set a history id for a project that doesn't have one. The
|
|
|
|
// history id is cached in the project history service, and changing an
|
|
|
|
// existing value corrupts the history, leaving it in an irrecoverable
|
|
|
|
// state. Setting a history id when one wasn't present before is ok,
|
|
|
|
// because undefined history ids aren't cached.
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return ProjectHistoryHandler.getHistoryId(
|
|
|
|
project_id,
|
|
|
|
function (err, history_id) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
if (history_id != null) {
|
|
|
|
return callback()
|
|
|
|
} // history already exists, success
|
2021-08-12 11:19:56 -04:00
|
|
|
return HistoryManager.initializeProject(function (err, history) {
|
2021-04-14 09:17:21 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-08-12 11:19:56 -04:00
|
|
|
if (!(history != null ? history.overleaf_id : undefined)) {
|
|
|
|
return callback(new Error('failed to initialize history id'))
|
|
|
|
}
|
|
|
|
return ProjectHistoryHandler.setHistoryId(
|
|
|
|
project_id,
|
|
|
|
history.overleaf_id,
|
|
|
|
function (err) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
2021-08-12 10:07:34 -04:00
|
|
|
}
|
2021-08-12 11:19:56 -04:00
|
|
|
return ProjectEntityUpdateHandler.resyncProjectHistory(
|
|
|
|
project_id,
|
|
|
|
function (err) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
return HistoryManager.flushProject(project_id, callback)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function __guard__(value, transform) {
|
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|
2019-10-23 08:24:01 -04:00
|
|
|
|
|
|
|
ProjectHistoryHandler.promises = promisifyAll(ProjectHistoryHandler)
|
|
|
|
module.exports = ProjectHistoryHandler
|