From 4d2f0218afd34436777424a05f997ae68e2e0fc5 Mon Sep 17 00:00:00 2001 From: Hayden Faulds Date: Wed, 28 Feb 2018 17:31:26 +0000 Subject: [PATCH] add resync project history endpoint --- .../DocumentUpdaterHandler.coffee | 29 +++++++++++++++++++ .../Features/History/HistoryController.coffee | 7 +++++ services/web/app/coffee/router.coffee | 1 + 3 files changed, 37 insertions(+) diff --git a/services/web/app/coffee/Features/DocumentUpdater/DocumentUpdaterHandler.coffee b/services/web/app/coffee/Features/DocumentUpdater/DocumentUpdaterHandler.coffee index 04c11c6419..e3cbf4ce8e 100644 --- a/services/web/app/coffee/Features/DocumentUpdater/DocumentUpdaterHandler.coffee +++ b/services/web/app/coffee/Features/DocumentUpdater/DocumentUpdaterHandler.coffee @@ -5,7 +5,9 @@ _ = require 'underscore' async = require 'async' logger = require('logger-sharelatex') metrics = require('metrics-sharelatex') +FileStoreHandler = require("../FileStore/FileStoreHandler") Project = require("../../models/Project").Project +ProjectGetter = require "../Project/ProjectGetter" module.exports = DocumentUpdaterHandler = flushProjectToMongo: (project_id, callback = (error) ->)-> @@ -203,6 +205,33 @@ module.exports = DocumentUpdaterHandler = logger.error {project_id, doc_id, thread_id}, "doc updater returned a non-success status code: #{res.statusCode}" callback new Error("doc updater returned a non-success status code: #{res.statusCode}") + resyncProject: (project_id, callback) -> + ProjectEntityHandler = require "../Project/ProjectEntityHandler" + ProjectGetter.getProject project_id, rootFolder: true, (error, project) -> + return callback(error) if error? + ProjectEntityHandler.getAllEntitiesFromProject project, (error, docs, files) -> + return callback(error) if error? + + docs = _.map docs, (doc) -> + doc: doc.doc._id + path: doc.path + + files = _.map files, (file) -> + file: file.file._id + path: file.path + url: FileStoreHandler._buildUrl(project_id, file.file._id) + + request.post + url: "#{settings.apis.documentupdater.url}/project/#{project_id}/resync" + json: { docs, files } + , (error, res, body) -> + if error? + logger.error {error, project_id}, "error resyncing project in doc updater" + callback(error) + else if res.statusCode >= 200 and res.statusCode < 300 + logger.log {project_id}, "resynced project in doc updater" + callback() + updateProjectStructure : (project_id, userId, changes, callback = (error) ->)-> return callback() if !settings.apis.project_history?.sendProjectStructureOps Project.findOne {_id: project_id}, {version:true}, (err, currentProject) -> diff --git a/services/web/app/coffee/Features/History/HistoryController.coffee b/services/web/app/coffee/Features/History/HistoryController.coffee index d158cd86c6..5ce0338306 100644 --- a/services/web/app/coffee/Features/History/HistoryController.coffee +++ b/services/web/app/coffee/Features/History/HistoryController.coffee @@ -2,6 +2,7 @@ logger = require "logger-sharelatex" request = require "request" settings = require "settings-sharelatex" AuthenticationController = require "../Authentication/AuthenticationController" +DocumentUpdaterHandler = require "../DocumentUpdater/DocumentUpdaterHandler" ProjectDetailsHandler = require "../Project/ProjectDetailsHandler" HistoryManager = require "./HistoryManager" @@ -62,3 +63,9 @@ module.exports = HistoryController = return settings.apis.project_history.url else return settings.apis.trackchanges.url + + resyncProject: (req, res, next = (error) ->) -> + project_id = req.params.Project_id + DocumentUpdaterHandler.resyncProject project_id, (error) -> + return next(error) if error? + res.sendStatus 204 diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index 1241e16684..fb2a7cb756 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -201,6 +201,7 @@ module.exports = class Router webRouter.get "/project/:Project_id/doc/:doc_id/diff", AuthorizationMiddlewear.ensureUserCanReadProject, HistoryController.selectHistoryApi, HistoryController.proxyToHistoryApi webRouter.get "/project/:Project_id/diff", AuthorizationMiddlewear.ensureUserCanReadProject, HistoryController.selectHistoryApi, HistoryController.proxyToHistoryApiAndInjectUserDetails webRouter.post "/project/:Project_id/doc/:doc_id/version/:version_id/restore", AuthorizationMiddlewear.ensureUserCanReadProject, HistoryController.selectHistoryApi, HistoryController.proxyToHistoryApi + privateApiRouter.post "/project/:Project_id/history/resync", AuthenticationController.httpAuth, HistoryController.resyncProject webRouter.get '/Project/:Project_id/download/zip', AuthorizationMiddlewear.ensureUserCanReadProject, ProjectDownloadsController.downloadProject webRouter.get '/project/download/zip', AuthorizationMiddlewear.ensureUserCanReadMultipleProjects, ProjectDownloadsController.downloadMultipleProjects