| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /* eslint-disable
- camelcase,
- node/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
- * 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')
- const settings = require('@overleaf/settings')
- const HistoryManager = require('../History/HistoryManager')
- const ProjectEntityUpdateHandler = require('./ProjectEntityUpdateHandler')
- const { promisifyAll } = require('../../util/promises')
- const ProjectHistoryHandler = {
- setHistoryId(project_id, history_id, callback) {
- // reject invalid history ids
- if (callback == null) {
- callback = function (err) {}
- }
- 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
- return Project.updateOne(
- { _id: project_id, 'overleaf.history.id': { $exists: false } },
- { 'overleaf.history.id': history_id },
- function (err, result) {
- 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) {
- callback = function (err, result) {}
- }
- 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,
- __guard__(
- __guard__(
- project != null ? project.overleaf : undefined,
- x1 => x1.history
- ),
- x => x.id
- )
- )
- }
- )
- },
- upgradeHistory(project_id, callback) {
- // project must have an overleaf.history.id before allowing display of new history
- if (callback == null) {
- callback = function (err, result) {}
- }
- return Project.updateOne(
- { _id: project_id, 'overleaf.history.id': { $exists: true } },
- {
- 'overleaf.history.display': true,
- 'overleaf.history.upgradedAt': new Date(),
- },
- function (err, result) {
- 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) {
- callback = function (err, result) {}
- }
- return Project.updateOne(
- { _id: project_id, 'overleaf.history.upgradedAt': { $exists: true } },
- {
- 'overleaf.history.display': false,
- $unset: { 'overleaf.history.upgradedAt': 1 },
- },
- function (err, result) {
- 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) {
- callback = function (err) {}
- }
- return ProjectHistoryHandler.getHistoryId(
- project_id,
- function (err, history_id) {
- if (err != null) {
- return callback(err)
- }
- if (history_id != null) {
- return callback()
- } // history already exists, success
- return HistoryManager.initializeProject(function (err, history) {
- if (err != null) {
- return callback(err)
- }
- 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)
- }
- return ProjectEntityUpdateHandler.resyncProjectHistory(
- project_id,
- function (err) {
- if (err != null) {
- return callback(err)
- }
- return HistoryManager.flushProject(project_id, callback)
- }
- )
- }
- )
- })
- }
- )
- },
- }
- function __guard__(value, transform) {
- return typeof value !== 'undefined' && value !== null
- ? transform(value)
- : undefined
- }
- ProjectHistoryHandler.promises = promisifyAll(ProjectHistoryHandler)
- module.exports = ProjectHistoryHandler
|