소스 검색

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

* [web] Add current history tab to admin panel

GitOrigin-RevId: 95a479d01bdf2ca65cd9b18077823d895cc12501
Mathias Jakobsen 2 년 전
부모
커밋
48f8fe9dd6
2개의 변경된 파일과 46개의 추가작업 그리고 0개의 파일을 삭제
  1. 31 0
      services/web/app/src/Features/History/HistoryManager.js
  2. 15 0
      services/web/test/unit/src/History/HistoryManagerTests.js

+ 31 - 0
services/web/app/src/Features/History/HistoryManager.js

@@ -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,
   },
 }

+ 15 - 0
services/web/test/unit/src/History/HistoryManagerTests.js

@@ -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,
       },
     })
   })