2019-05-29 05:21:06 -04:00
|
|
|
const { Project } = require('../../models/Project')
|
|
|
|
const ProjectDetailsHandler = require('./ProjectDetailsHandler')
|
|
|
|
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 = {
|
2023-01-16 06:30:42 -05:00
|
|
|
setHistoryId(projectId, historyId, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
// reject invalid history ids
|
2023-01-16 06:30:42 -05:00
|
|
|
if (historyId == null) {
|
2022-12-06 07:57:39 -05:00
|
|
|
return callback(new Error('missing history id'))
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
// use $exists:false to prevent overwriting any existing history id, atomically
|
2023-01-16 06:30:42 -05:00
|
|
|
Project.updateOne(
|
|
|
|
{ _id: projectId, 'overleaf.history.id': { $exists: false } },
|
|
|
|
{ 'overleaf.history.id': historyId },
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, result) {
|
2023-01-16 06:30:42 -05:00
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
2023-02-28 08:26:18 -05:00
|
|
|
if (result.matchedCount === 0) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(new Error('history exists'))
|
|
|
|
}
|
2023-01-16 06:30:42 -05:00
|
|
|
callback()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2023-01-16 06:30:42 -05:00
|
|
|
getHistoryId(projectId, callback) {
|
|
|
|
ProjectDetailsHandler.getDetails(projectId, function (err, project) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
} // n.b. getDetails returns an error if the project doesn't exist
|
|
|
|
callback(null, project?.overleaf?.history?.id)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2023-01-16 06:30:42 -05:00
|
|
|
unsetHistory(projectId, callback) {
|
|
|
|
Project.updateOne(
|
|
|
|
{ _id: projectId },
|
2021-11-02 05:13:34 -04:00
|
|
|
{ $unset: { 'overleaf.history': true } },
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2023-01-16 06:30:42 -05:00
|
|
|
upgradeHistory(projectId, allowDowngrade, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
// project must have an overleaf.history.id before allowing display of new history
|
2023-01-16 06:30:42 -05:00
|
|
|
Project.updateOne(
|
|
|
|
{ _id: projectId, 'overleaf.history.id': { $exists: true } },
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
'overleaf.history.display': true,
|
2021-04-27 03:52:58 -04:00
|
|
|
'overleaf.history.upgradedAt': new Date(),
|
2021-11-02 05:13:34 -04:00
|
|
|
'overleaf.history.allowDowngrade': allowDowngrade,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, result) {
|
2023-01-16 06:30:42 -05:00
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
// return an error if overleaf.history.id wasn't present
|
2023-02-28 08:26:18 -05:00
|
|
|
if (result.matchedCount === 0) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(new Error('history not upgraded'))
|
|
|
|
}
|
2023-01-16 06:30:42 -05:00
|
|
|
callback()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2023-01-16 06:30:42 -05:00
|
|
|
downgradeHistory(projectId, callback) {
|
|
|
|
Project.updateOne(
|
|
|
|
{ _id: projectId, 'overleaf.history.upgradedAt': { $exists: true } },
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
'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) {
|
2023-01-16 06:30:42 -05:00
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
2023-02-28 08:26:18 -05:00
|
|
|
if (result.matchedCount === 0) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(new Error('history not downgraded'))
|
|
|
|
}
|
2023-01-16 06:30:42 -05:00
|
|
|
callback()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2023-01-16 06:30:42 -05:00
|
|
|
setMigrationArchiveFlag(projectId, callback) {
|
2022-06-30 04:21:50 -04:00
|
|
|
Project.updateOne(
|
2023-01-16 06:30:42 -05:00
|
|
|
{ _id: projectId, version: { $exists: true } },
|
2022-06-30 04:21:50 -04:00
|
|
|
{
|
|
|
|
'overleaf.history.zipFileArchivedInProject': true,
|
|
|
|
},
|
|
|
|
function (err, result) {
|
2023-01-16 06:30:42 -05:00
|
|
|
if (err) {
|
2022-06-30 04:21:50 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
2023-02-28 08:26:18 -05:00
|
|
|
if (result.matchedCount === 0) {
|
2022-06-30 04:21:50 -04:00
|
|
|
return callback(new Error('migration flag not set'))
|
|
|
|
}
|
2023-01-16 06:30:42 -05:00
|
|
|
callback()
|
2022-06-30 04:21:50 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2023-01-16 06:30:42 -05:00
|
|
|
ensureHistoryExistsForProject(projectId, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
// 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.
|
2023-01-16 06:30:42 -05:00
|
|
|
ProjectHistoryHandler.getHistoryId(projectId, function (err, historyId) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (historyId != null) {
|
|
|
|
return callback()
|
|
|
|
} // history already exists, success
|
|
|
|
HistoryManager.initializeProject(projectId, function (err, historyId) {
|
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
2023-01-16 06:30:42 -05:00
|
|
|
if (historyId == null) {
|
|
|
|
return callback(new Error('failed to initialize history id'))
|
|
|
|
}
|
|
|
|
ProjectHistoryHandler.setHistoryId(
|
|
|
|
projectId,
|
|
|
|
historyId,
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
2022-12-06 07:57:39 -05:00
|
|
|
return callback(err)
|
2021-08-12 11:19:56 -04:00
|
|
|
}
|
2023-01-16 06:30:42 -05:00
|
|
|
ProjectEntityUpdateHandler.resyncProjectHistory(
|
|
|
|
projectId,
|
2022-12-06 07:57:39 -05:00
|
|
|
function (err) {
|
2023-01-16 06:30:42 -05:00
|
|
|
if (err) {
|
2022-12-06 07:57:39 -05:00
|
|
|
return callback(err)
|
|
|
|
}
|
2023-01-16 06:30:42 -05:00
|
|
|
HistoryManager.flushProject(projectId, callback)
|
2022-12-06 07:57:39 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2023-01-16 06:30:42 -05:00
|
|
|
})
|
|
|
|
})
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:24:01 -04:00
|
|
|
ProjectHistoryHandler.promises = promisifyAll(ProjectHistoryHandler)
|
|
|
|
module.exports = ProjectHistoryHandler
|