Merge pull request #10989 from overleaf/bg-clean-up-project-history-handler

clean up ProjectHistoryHandler

GitOrigin-RevId: 82e6510a23934a3f3a1ede0fabbd2e393f74ad03
This commit is contained in:
Brian Gough
2023-01-17 09:04:35 +00:00
committed by Copybot
parent f21f255deb
commit 3e15a8f3fe
@@ -1,190 +1,147 @@
/* eslint-disable
camelcase,
n/handle-callback-err,
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
* 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 { Project } = require('../../models/Project')
const ProjectDetailsHandler = require('./ProjectDetailsHandler') const ProjectDetailsHandler = require('./ProjectDetailsHandler')
const logger = require('@overleaf/logger')
const settings = require('@overleaf/settings')
const HistoryManager = require('../History/HistoryManager') const HistoryManager = require('../History/HistoryManager')
const ProjectEntityUpdateHandler = require('./ProjectEntityUpdateHandler') const ProjectEntityUpdateHandler = require('./ProjectEntityUpdateHandler')
const { promisifyAll } = require('../../util/promises') const { promisifyAll } = require('../../util/promises')
const ProjectHistoryHandler = { const ProjectHistoryHandler = {
setHistoryId(project_id, history_id, callback) { setHistoryId(projectId, historyId, callback) {
// reject invalid history ids // reject invalid history ids
if (callback == null) { if (historyId == null) {
callback = function () {}
}
if (history_id == null) {
return callback(new Error('missing history id')) return callback(new Error('missing history id'))
} }
// use $exists:false to prevent overwriting any existing history id, atomically // use $exists:false to prevent overwriting any existing history id, atomically
return Project.updateOne( Project.updateOne(
{ _id: project_id, 'overleaf.history.id': { $exists: false } }, { _id: projectId, 'overleaf.history.id': { $exists: false } },
{ 'overleaf.history.id': history_id }, { 'overleaf.history.id': historyId },
function (err, result) { function (err, result) {
if (err != null) { if (err) {
return callback(err) return callback(err)
} }
if ((result != null ? result.n : undefined) === 0) { if (result.n === 0) {
return callback(new Error('history exists')) return callback(new Error('history exists'))
} }
return callback() callback()
} }
) )
}, },
getHistoryId(project_id, callback) { getHistoryId(projectId, callback) {
if (callback == null) { ProjectDetailsHandler.getDetails(projectId, function (err, project) {
callback = function () {} if (err) {
} return callback(err)
return ProjectDetailsHandler.getDetails( } // n.b. getDetails returns an error if the project doesn't exist
project_id, callback(null, project?.overleaf?.history?.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, project?.overleaf?.history?.id)
}
)
}, },
unsetHistory(project_id, callback) { unsetHistory(projectId, callback) {
return Project.updateOne( Project.updateOne(
{ _id: project_id }, { _id: projectId },
{ $unset: { 'overleaf.history': true } }, { $unset: { 'overleaf.history': true } },
callback callback
) )
}, },
upgradeHistory(project_id, allowDowngrade, callback) { upgradeHistory(projectId, allowDowngrade, callback) {
// project must have an overleaf.history.id before allowing display of new history // project must have an overleaf.history.id before allowing display of new history
if (callback == null) { Project.updateOne(
callback = function () {} { _id: projectId, 'overleaf.history.id': { $exists: true } },
}
return Project.updateOne(
{ _id: project_id, 'overleaf.history.id': { $exists: true } },
{ {
'overleaf.history.display': true, 'overleaf.history.display': true,
'overleaf.history.upgradedAt': new Date(), 'overleaf.history.upgradedAt': new Date(),
'overleaf.history.allowDowngrade': allowDowngrade, 'overleaf.history.allowDowngrade': allowDowngrade,
}, },
function (err, result) { function (err, result) {
if (err != null) { if (err) {
return callback(err) return callback(err)
} }
// return an error if overleaf.history.id wasn't present // return an error if overleaf.history.id wasn't present
if ((result != null ? result.n : undefined) === 0) { if (result.n === 0) {
return callback(new Error('history not upgraded')) return callback(new Error('history not upgraded'))
} }
return callback() callback()
} }
) )
}, },
downgradeHistory(project_id, callback) { downgradeHistory(projectId, callback) {
if (callback == null) { Project.updateOne(
callback = function () {} { _id: projectId, 'overleaf.history.upgradedAt': { $exists: true } },
}
return Project.updateOne(
{ _id: project_id, 'overleaf.history.upgradedAt': { $exists: true } },
{ {
'overleaf.history.display': false, 'overleaf.history.display': false,
$unset: { 'overleaf.history.upgradedAt': 1 }, $unset: { 'overleaf.history.upgradedAt': 1 },
}, },
function (err, result) { function (err, result) {
if (err != null) { if (err) {
return callback(err) return callback(err)
} }
if ((result != null ? result.n : undefined) === 0) { if (result.n === 0) {
return callback(new Error('history not downgraded')) return callback(new Error('history not downgraded'))
} }
return callback() callback()
} }
) )
}, },
setMigrationArchiveFlag(project_id, callback) { setMigrationArchiveFlag(projectId, callback) {
if (callback == null) {
callback = function () {}
}
Project.updateOne( Project.updateOne(
{ _id: project_id, version: { $exists: true } }, { _id: projectId, version: { $exists: true } },
{ {
'overleaf.history.zipFileArchivedInProject': true, 'overleaf.history.zipFileArchivedInProject': true,
}, },
function (err, result) { function (err, result) {
if (err != null) { if (err) {
return callback(err) return callback(err)
} }
if ((result != null ? result.n : undefined) === 0) { if (result.n === 0) {
return callback(new Error('migration flag not set')) return callback(new Error('migration flag not set'))
} }
return callback() callback()
} }
) )
}, },
ensureHistoryExistsForProject(project_id, callback) { ensureHistoryExistsForProject(projectId, callback) {
// We can only set a history id for a project that doesn't have one. The // 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 // history id is cached in the project history service, and changing an
// existing value corrupts the history, leaving it in an irrecoverable // existing value corrupts the history, leaving it in an irrecoverable
// state. Setting a history id when one wasn't present before is ok, // state. Setting a history id when one wasn't present before is ok,
// because undefined history ids aren't cached. // because undefined history ids aren't cached.
if (callback == null) { ProjectHistoryHandler.getHistoryId(projectId, function (err, historyId) {
callback = function () {} if (err) {
} return callback(err)
return ProjectHistoryHandler.getHistoryId( }
project_id, if (historyId != null) {
function (err, history_id) { return callback()
if (err != null) { } // history already exists, success
HistoryManager.initializeProject(projectId, function (err, historyId) {
if (err) {
return callback(err) return callback(err)
} }
if (history_id != null) { if (historyId == null) {
return callback() return callback(new Error('failed to initialize history id'))
} // history already exists, success }
return HistoryManager.initializeProject( ProjectHistoryHandler.setHistoryId(
project_id, projectId,
function (err, historyId) { historyId,
if (err != null) { function (err) {
if (err) {
return callback(err) return callback(err)
} }
if (historyId == null) { ProjectEntityUpdateHandler.resyncProjectHistory(
return callback(new Error('failed to initialize history id')) projectId,
}
return ProjectHistoryHandler.setHistoryId(
project_id,
historyId,
function (err) { function (err) {
if (err != null) { if (err) {
return callback(err) return callback(err)
} }
return ProjectEntityUpdateHandler.resyncProjectHistory( HistoryManager.flushProject(projectId, callback)
project_id,
function (err) {
if (err != null) {
return callback(err)
}
return HistoryManager.flushProject(project_id, callback)
}
)
} }
) )
} }
) )
} })
) })
}, },
} }