[web] Add current history tab to admin panel (#17482)

* [web] Add current history tab to admin panel

GitOrigin-RevId: 95a479d01bdf2ca65cd9b18077823d895cc12501
This commit is contained in:
Mathias Jakobsen 2024-03-28 09:17:01 +00:00 committed by Copybot
parent 0409347679
commit 48f8fe9dd6
2 changed files with 46 additions and 0 deletions

View file

@ -3,6 +3,7 @@ const { fetchJson, fetchNothing } = require('@overleaf/fetch-utils')
const settings = require('@overleaf/settings')
const OError = require('@overleaf/o-error')
const UserGetter = require('../User/UserGetter')
const { Project } = require('../../models/Project')
async function initializeProject(projectId) {
const body = await fetchJson(`${settings.apis.project_history.url}/project`, {
@ -107,6 +108,34 @@ async function _deleteProjectInFullProjectHistory(historyId) {
}
}
/**
* Warning: Don't use this method for large projects. It will eagerly load all
* the history data and apply all operations.
* @param {string} projectId
* @returns Promise<object>
*/
async function getCurrentContent(projectId) {
const project = await Project.findById(projectId).exec()
const historyId = project?.overleaf?.history?.id
if (!historyId) {
throw new OError('project does not have a history id', { projectId })
}
try {
return await fetchJson(
`${settings.apis.v1_history.url}/projects/${historyId}/latest/content`,
{
method: 'GET',
basicAuth: {
user: settings.apis.v1_history.user,
password: settings.apis.v1_history.pass,
},
}
)
} catch (err) {
throw OError.tag(err, 'failed to load project history', { historyId })
}
}
async function injectUserDetails(data) {
// data can be either:
// {
@ -192,6 +221,7 @@ module.exports = {
deleteProject: callbackify(deleteProject),
deleteProjectHistory: callbackify(deleteProjectHistory),
injectUserDetails: callbackify(injectUserDetails),
getCurrentContent: callbackify(getCurrentContent),
promises: {
initializeProject,
flushProject,
@ -199,5 +229,6 @@ module.exports = {
deleteProject,
injectUserDetails,
deleteProjectHistory,
getCurrentContent,
},
}

View file

@ -40,11 +40,26 @@ describe('HistoryManager', function () {
},
}
this.project = {
overleaf: {
history: {
id: this.historyId,
},
},
}
this.ProjectModel = {
Project: {
findById: sinon.stub().returns(sinon.stub().resolves(this.project)),
},
}
this.HistoryManager = SandboxedModule.require(MODULE_PATH, {
requires: {
'@overleaf/fetch-utils': this.FetchUtils,
'@overleaf/settings': this.settings,
'../User/UserGetter': this.UserGetter,
'../../models/Project': this.ProjectModel,
},
})
})