mirror of
https://github.com/overleaf/overleaf.git
synced 2024-09-16 02:52:31 -04:00
Merge pull request #81 from sharelatex/hof-init-project-history
initialize project history on project creation
This commit is contained in:
commit
79d9756bea
5 changed files with 157 additions and 31 deletions
|
@ -4,6 +4,29 @@ settings = require "settings-sharelatex"
|
||||||
AuthenticationController = require "../Authentication/AuthenticationController"
|
AuthenticationController = require "../Authentication/AuthenticationController"
|
||||||
|
|
||||||
module.exports = HistoryController =
|
module.exports = HistoryController =
|
||||||
|
initializeProject: (callback = (error, history_id) ->) ->
|
||||||
|
return callback() if !settings.apis.project_history?.enabled
|
||||||
|
request.post {
|
||||||
|
url: "#{settings.apis.project_history.url}/project"
|
||||||
|
}, (error, res, body)->
|
||||||
|
return callback(error) if error?
|
||||||
|
|
||||||
|
if res.statusCode >= 200 and res.statusCode < 300
|
||||||
|
try
|
||||||
|
project = JSON.parse(body)
|
||||||
|
catch error
|
||||||
|
return callback(error)
|
||||||
|
|
||||||
|
overleaf_id = project?.project?.id
|
||||||
|
if !overleaf_id
|
||||||
|
error = new Error("project-history did not provide an id", project)
|
||||||
|
return callback(error)
|
||||||
|
|
||||||
|
callback null, { overleaf_id }
|
||||||
|
else
|
||||||
|
error = new Error("project-history returned a non-success status code: #{res.statusCode}")
|
||||||
|
callback error
|
||||||
|
|
||||||
proxyToHistoryApi: (req, res, next = (error) ->) ->
|
proxyToHistoryApi: (req, res, next = (error) ->) ->
|
||||||
user_id = AuthenticationController.getLoggedInUserId req
|
user_id = AuthenticationController.getLoggedInUserId req
|
||||||
url = HistoryController.buildHistoryServiceUrl() + req.url
|
url = HistoryController.buildHistoryServiceUrl() + req.url
|
||||||
|
|
|
@ -7,6 +7,7 @@ Project = require('../../models/Project').Project
|
||||||
Folder = require('../../models/Folder').Folder
|
Folder = require('../../models/Folder').Folder
|
||||||
ProjectEntityHandler = require('./ProjectEntityHandler')
|
ProjectEntityHandler = require('./ProjectEntityHandler')
|
||||||
ProjectDetailsHandler = require('./ProjectDetailsHandler')
|
ProjectDetailsHandler = require('./ProjectDetailsHandler')
|
||||||
|
HistoryController = require('../History/HistoryController')
|
||||||
User = require('../../models/User').User
|
User = require('../../models/User').User
|
||||||
fs = require('fs')
|
fs = require('fs')
|
||||||
Path = require "path"
|
Path = require "path"
|
||||||
|
@ -14,15 +15,28 @@ _ = require "underscore"
|
||||||
|
|
||||||
module.exports = ProjectCreationHandler =
|
module.exports = ProjectCreationHandler =
|
||||||
|
|
||||||
createBlankProject : (owner_id, projectName, callback = (error, project) ->)->
|
createBlankProject : (owner_id, projectName, projectHistoryId, callback = (error, project) ->)->
|
||||||
metrics.inc("project-creation")
|
metrics.inc("project-creation")
|
||||||
|
if arguments.length == 3
|
||||||
|
callback = projectHistoryId
|
||||||
|
projectHistoryId = null
|
||||||
|
|
||||||
ProjectDetailsHandler.validateProjectName projectName, (error) ->
|
ProjectDetailsHandler.validateProjectName projectName, (error) ->
|
||||||
return callback(error) if error?
|
return callback(error) if error?
|
||||||
logger.log owner_id:owner_id, projectName:projectName, "creating blank project"
|
logger.log owner_id:owner_id, projectName:projectName, "creating blank project"
|
||||||
|
if projectHistoryId?
|
||||||
|
ProjectCreationHandler._createBlankProject owner_id, projectName, projectHistoryId, callback
|
||||||
|
else
|
||||||
|
HistoryController.initializeProject (error, history) ->
|
||||||
|
return callback(error) if error?
|
||||||
|
ProjectCreationHandler._createBlankProject owner_id, projectName, history?.overleaf_id, callback
|
||||||
|
|
||||||
|
_createBlankProject : (owner_id, projectName, projectHistoryId, callback = (error, project) ->)->
|
||||||
rootFolder = new Folder {'name':'rootFolder'}
|
rootFolder = new Folder {'name':'rootFolder'}
|
||||||
project = new Project
|
project = new Project
|
||||||
owner_ref : new ObjectId(owner_id)
|
owner_ref : new ObjectId(owner_id)
|
||||||
name : projectName
|
name : projectName
|
||||||
|
project.overleaf.history.id = projectHistoryId
|
||||||
if Settings.currentImageName?
|
if Settings.currentImageName?
|
||||||
project.imageName = Settings.currentImageName
|
project.imageName = Settings.currentImageName
|
||||||
project.rootFolder[0] = rootFolder
|
project.rootFolder[0] = rootFolder
|
||||||
|
|
|
@ -38,6 +38,8 @@ ProjectSchema = new Schema
|
||||||
imported_at_ver_id : { type: Number }
|
imported_at_ver_id : { type: Number }
|
||||||
token : { type: String }
|
token : { type: String }
|
||||||
read_token : { type: String }
|
read_token : { type: String }
|
||||||
|
history :
|
||||||
|
id : { type: Number }
|
||||||
|
|
||||||
ProjectSchema.statics.getProject = (project_or_id, fields, callback)->
|
ProjectSchema.statics.getProject = (project_or_id, fields, callback)->
|
||||||
if project_or_id._id?
|
if project_or_id._id?
|
||||||
|
|
|
@ -6,6 +6,7 @@ SandboxedModule = require('sandboxed-module')
|
||||||
|
|
||||||
describe "HistoryController", ->
|
describe "HistoryController", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
|
@callback = sinon.stub()
|
||||||
@user_id = "user-id-123"
|
@user_id = "user-id-123"
|
||||||
@AuthenticationController =
|
@AuthenticationController =
|
||||||
getLoggedInUserId: sinon.stub().returns(@user_id)
|
getLoggedInUserId: sinon.stub().returns(@user_id)
|
||||||
|
@ -14,18 +15,18 @@ describe "HistoryController", ->
|
||||||
"settings-sharelatex": @settings = {}
|
"settings-sharelatex": @settings = {}
|
||||||
"logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()}
|
"logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()}
|
||||||
"../Authentication/AuthenticationController": @AuthenticationController
|
"../Authentication/AuthenticationController": @AuthenticationController
|
||||||
|
|
||||||
describe "proxyToHistoryApi", ->
|
|
||||||
beforeEach ->
|
|
||||||
@req = { url: "/mock/url", method: "POST" }
|
|
||||||
@res = "mock-res"
|
|
||||||
@next = sinon.stub()
|
|
||||||
@settings.apis =
|
@settings.apis =
|
||||||
trackchanges:
|
trackchanges:
|
||||||
enabled: false
|
enabled: false
|
||||||
url: "http://trackchanges.example.com"
|
url: "http://trackchanges.example.com"
|
||||||
project_history:
|
project_history:
|
||||||
url: "http://project_history.example.com"
|
url: "http://project_history.example.com"
|
||||||
|
|
||||||
|
describe "proxyToHistoryApi", ->
|
||||||
|
beforeEach ->
|
||||||
|
@req = { url: "/mock/url", method: "POST" }
|
||||||
|
@res = "mock-res"
|
||||||
|
@next = sinon.stub()
|
||||||
@proxy =
|
@proxy =
|
||||||
events: {}
|
events: {}
|
||||||
pipe: sinon.stub()
|
pipe: sinon.stub()
|
||||||
|
@ -80,3 +81,68 @@ describe "HistoryController", ->
|
||||||
|
|
||||||
it "should pass the error up the call chain", ->
|
it "should pass the error up the call chain", ->
|
||||||
@next.calledWith(@error).should.equal true
|
@next.calledWith(@error).should.equal true
|
||||||
|
|
||||||
|
describe "initializeProject", ->
|
||||||
|
describe "with project history enabled", ->
|
||||||
|
beforeEach ->
|
||||||
|
@settings.apis.project_history.enabled = true
|
||||||
|
|
||||||
|
describe "project history returns a successful response", ->
|
||||||
|
beforeEach ->
|
||||||
|
@overleaf_id = 1234
|
||||||
|
@res = statusCode: 200
|
||||||
|
@body = JSON.stringify(project: id: @overleaf_id)
|
||||||
|
@request.post = sinon.stub().callsArgWith(1, null, @res, @body)
|
||||||
|
|
||||||
|
@HistoryController.initializeProject @callback
|
||||||
|
|
||||||
|
it "should call the project history api", ->
|
||||||
|
@request.post.calledWith(
|
||||||
|
url: "#{@settings.apis.project_history.url}/project"
|
||||||
|
).should.equal true
|
||||||
|
|
||||||
|
it "should return the callback with the overleaf id", ->
|
||||||
|
@callback.calledWithExactly(null, { @overleaf_id }).should.equal true
|
||||||
|
|
||||||
|
describe "project history returns a response without the project id", ->
|
||||||
|
beforeEach ->
|
||||||
|
@res = statusCode: 200
|
||||||
|
@body = JSON.stringify(project: {})
|
||||||
|
@request.post = sinon.stub().callsArgWith(1, null, @res, @body)
|
||||||
|
|
||||||
|
@HistoryController.initializeProject @callback
|
||||||
|
|
||||||
|
it "should return the callback with an error", ->
|
||||||
|
@callback
|
||||||
|
.calledWith(sinon.match.has("message", "project-history did not provide an id"))
|
||||||
|
.should.equal true
|
||||||
|
|
||||||
|
describe "project history returns a unsuccessful response", ->
|
||||||
|
beforeEach ->
|
||||||
|
@res = statusCode: 404
|
||||||
|
@request.post = sinon.stub().callsArgWith(1, null, @res)
|
||||||
|
|
||||||
|
@HistoryController.initializeProject @callback
|
||||||
|
|
||||||
|
it "should return the callback with an error", ->
|
||||||
|
@callback
|
||||||
|
.calledWith(sinon.match.has("message", "project-history returned a non-success status code: 404"))
|
||||||
|
.should.equal true
|
||||||
|
|
||||||
|
describe "project history errors", ->
|
||||||
|
beforeEach ->
|
||||||
|
@error = sinon.stub()
|
||||||
|
@request.post = sinon.stub().callsArgWith(1, @error)
|
||||||
|
|
||||||
|
@HistoryController.initializeProject @callback
|
||||||
|
|
||||||
|
it "should return the callback with the error", ->
|
||||||
|
@callback.calledWithExactly(@error).should.equal true
|
||||||
|
|
||||||
|
describe "with project history disabled", ->
|
||||||
|
beforeEach ->
|
||||||
|
@settings.apis.project_history.enabled = false
|
||||||
|
@HistoryController.initializeProject @callback
|
||||||
|
|
||||||
|
it "should return the callback", ->
|
||||||
|
@callback.calledWithExactly().should.equal true
|
||||||
|
|
|
@ -22,6 +22,8 @@ describe 'ProjectCreationHandler', ->
|
||||||
@._id = project_id
|
@._id = project_id
|
||||||
@owner_ref = options.owner_ref
|
@owner_ref = options.owner_ref
|
||||||
@name = options.name
|
@name = options.name
|
||||||
|
@overleaf =
|
||||||
|
history: {}
|
||||||
save: sinon.stub().callsArg(0)
|
save: sinon.stub().callsArg(0)
|
||||||
rootFolder:[{
|
rootFolder:[{
|
||||||
_id: rootFolderId
|
_id: rootFolderId
|
||||||
|
@ -36,6 +38,8 @@ describe 'ProjectCreationHandler', ->
|
||||||
setRootDoc: sinon.stub().callsArg(2)
|
setRootDoc: sinon.stub().callsArg(2)
|
||||||
@ProjectDetailsHandler =
|
@ProjectDetailsHandler =
|
||||||
validateProjectName: sinon.stub().yields()
|
validateProjectName: sinon.stub().yields()
|
||||||
|
@HistoryController =
|
||||||
|
initializeProject: sinon.stub().callsArg(0)
|
||||||
|
|
||||||
@user =
|
@user =
|
||||||
first_name:"first name here"
|
first_name:"first name here"
|
||||||
|
@ -49,6 +53,7 @@ describe 'ProjectCreationHandler', ->
|
||||||
'../../models/User': User:@User
|
'../../models/User': User:@User
|
||||||
'../../models/Project':{Project:@ProjectModel}
|
'../../models/Project':{Project:@ProjectModel}
|
||||||
'../../models/Folder':{Folder:@FolderModel}
|
'../../models/Folder':{Folder:@FolderModel}
|
||||||
|
'../History/HistoryController': @HistoryController
|
||||||
'./ProjectEntityHandler':@ProjectEntityHandler
|
'./ProjectEntityHandler':@ProjectEntityHandler
|
||||||
"./ProjectDetailsHandler":@ProjectDetailsHandler
|
"./ProjectDetailsHandler":@ProjectDetailsHandler
|
||||||
"settings-sharelatex": @Settings = {}
|
"settings-sharelatex": @Settings = {}
|
||||||
|
@ -60,10 +65,11 @@ describe 'ProjectCreationHandler', ->
|
||||||
|
|
||||||
describe 'Creating a Blank project', ->
|
describe 'Creating a Blank project', ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
|
@overleaf_id = 1234
|
||||||
|
@HistoryController.initializeProject = sinon.stub().callsArgWith(0, null, { @overleaf_id })
|
||||||
@ProjectModel::save = sinon.stub().callsArg(0)
|
@ProjectModel::save = sinon.stub().callsArg(0)
|
||||||
|
|
||||||
describe "successfully", ->
|
describe "successfully", ->
|
||||||
|
|
||||||
it "should save the project", (done)->
|
it "should save the project", (done)->
|
||||||
@handler.createBlankProject ownerId, projectName, =>
|
@handler.createBlankProject ownerId, projectName, =>
|
||||||
@ProjectModel::save.called.should.equal true
|
@ProjectModel::save.called.should.equal true
|
||||||
|
@ -75,6 +81,21 @@ describe 'ProjectCreationHandler', ->
|
||||||
(project.owner_ref + "").should.equal ownerId
|
(project.owner_ref + "").should.equal ownerId
|
||||||
done()
|
done()
|
||||||
|
|
||||||
|
it "should initialize the project overleaf if history id not provided", (done)->
|
||||||
|
@handler.createBlankProject ownerId, projectName, done
|
||||||
|
@HistoryController.initializeProject.calledWith().should.equal true
|
||||||
|
|
||||||
|
it "should set the overleaf id if overleaf id not provided", (done)->
|
||||||
|
@handler.createBlankProject ownerId, projectName, (err, project)=>
|
||||||
|
project.overleaf.history.id.should.equal @overleaf_id
|
||||||
|
done()
|
||||||
|
|
||||||
|
it "should set the overleaf id if overleaf id provided", (done)->
|
||||||
|
overleaf_id = 2345
|
||||||
|
@handler.createBlankProject ownerId, projectName, overleaf_id, (err, project)->
|
||||||
|
project.overleaf.history.id.should.equal overleaf_id
|
||||||
|
done()
|
||||||
|
|
||||||
it "should set the language from the user", (done)->
|
it "should set the language from the user", (done)->
|
||||||
@handler.createBlankProject ownerId, projectName, (err, project)->
|
@handler.createBlankProject ownerId, projectName, (err, project)->
|
||||||
project.spellCheckLanguage.should.equal "de"
|
project.spellCheckLanguage.should.equal "de"
|
||||||
|
|
Loading…
Reference in a new issue