Просмотр исходного кода

Convert template publishing to use HTTP end points

James Allen 12 лет назад
Родитель
Сommit
d27af25bc9

+ 44 - 20
services/web/app/coffee/Features/Templates/TemplatesController.coffee

@@ -2,6 +2,8 @@ path = require('path')
 ProjectUploadManager = require('../Uploads/ProjectUploadManager')
 ProjectOptionsHandler = require("../Project/ProjectOptionsHandler")
 ProjectDetailsHandler = require('../Project/ProjectDetailsHandler')
+ProjectGetter = require('../Project/ProjectGetter')
+EditorController = require('../Editor/EditorController')
 TemplatesPublisher = require("./TemplatesPublisher")
 settings = require('settings-sharelatex')
 fs = require('fs')
@@ -36,28 +38,50 @@ module.exports =
 					delete req.session.templateData
 					res.redirect "/project/#{project._id}"
 
-	publishProject: (user_id, project_id, callback)->
-		logger.log user_id:user_id, project_id:project_id, "receiving request to publish project as template"
-		TemplatesPublisher.publish user_id, project_id, callback
+	publishProject: (req, res, next) ->
+		project_id = req.params.Project_id
+		ProjectGetter.getProject project_id, {owner_ref: 1}, (error, project) ->
+			return callback(error) if error?
+			user_id = project.owner_ref.toString()
+			logger.log user_id:user_id, project_id:project_id, "receiving request to publish project as template"
+			TemplatesPublisher.publish user_id, project_id, (error) ->
+				return next(error) if error?
+				res.send 204
 
-	unPublishProject: (user_id, project_id, callback)->
-		logger.log user_id:user_id, project_id:project_id, "receiving request to unpublish project as template"
-		TemplatesPublisher.unpublish user_id, project_id, callback
-
-	getTemplateDetails: (user_id, project_id, callback)->
-		async.parallel {
-			details: (cb)->
-				TemplatesPublisher.getTemplateDetails user_id, project_id, cb
-			description: (cb)->
-				ProjectDetailsHandler.getProjectDescription project_id, cb
-		}, (err, results)->
-			if err?
-				logger.err err:err, user_id:user_id, project_id:project_id, "something went wrong getting template details"
-				return callback(err)
-			details = results.details
-			details.description = results.description
-			callback(err, details)
+	unpublishProject: (req, res, next) ->
+		project_id = req.params.Project_id
+		ProjectGetter.getProject project_id, {owner_ref: 1}, (error, project) ->
+			return callback(error) if error?
+			user_id = project.owner_ref.toString()
+			logger.log user_id:user_id, project_id:project_id, "receiving request to unpublish project"
+			TemplatesPublisher.unpublish user_id, project_id, (error) ->
+				return next(error) if error?
+				res.send 204
+				
+	updateProjectDescription: (req, res, next) ->
+		project_id = req.params.Project_id
+		{description} = req.body
+		EditorController.updateProjectDescription project_id, description, (error) ->
+			return next(error) if error?
+			res.send 204
 
+	getTemplateDetails: (req, res, next)->
+		project_id = req.params.Project_id
+		ProjectGetter.getProject project_id, {owner_ref: 1}, (error, project) ->
+			return next(error) if error?
+			user_id = project.owner_ref.toString()
+			async.parallel {
+				details: (cb)->
+					TemplatesPublisher.getTemplateDetails user_id, project_id, cb
+				description: (cb)->
+					ProjectDetailsHandler.getProjectDescription project_id, cb
+			}, (err, results)->
+				if err?
+					logger.err err:err, user_id:user_id, project_id:project_id, "something went wrong getting template details"
+					return next(err)
+				details = results.details
+				details.description = results.description
+				res.json details
 
 setCompiler = (project_id, compiler, callback)->
 	if compiler?

+ 6 - 2
services/web/app/coffee/Features/Templates/TemplatesRouter.coffee

@@ -1,5 +1,6 @@
-
+SecurityManager = require("../../managers/SecurityManager")
 TemplatesWebController = require("./TemplatesWebController")
+TemplatesController = require("./TemplatesController")
 middleWear = require("./TemplatesMiddlewear")
 
 module.exports = 
@@ -16,4 +17,7 @@ module.exports =
 
 		app.get "/templates/:template_id/v/:version/:file_type", TemplatesWebController.proxyToTemplatesApi
 
-
+		app.post "/project/:Project_id/template/publish", SecurityManager.requestIsOwner, TemplatesController.publishProject
+		app.post "/project/:Project_id/template/unpublish", SecurityManager.requestIsOwner, TemplatesController.unpublishProject
+		app.post "/project/:Project_id/template/description", SecurityManager.requestCanModifyProject, TemplatesController.updateProjectDescription
+		app.get  "/project/:Project_id/template", SecurityManager.requestCanAccessProject, TemplatesController.getTemplateDetails

+ 15 - 15
services/web/app/coffee/router.coffee

@@ -269,19 +269,19 @@ module.exports = class Router
 				AuthorizationManager.ensureClientCanAdminProject client, (error, project_id) =>
 					dropboxHandler.getUserRegistrationStatus owner_id, callback
 
-			client.on 'publishProjectAsTemplate', (user_id, callback)->
-				AuthorizationManager.ensureClientCanAdminProject client, (error, project_id) =>
-					TemplatesController.publishProject user_id, project_id, callback
-
-			client.on 'unPublishProjectAsTemplate', (user_id, callback)->
-				AuthorizationManager.ensureClientCanAdminProject client, (error, project_id) =>
-					TemplatesController.unPublishProject user_id, project_id, callback
-
-			client.on 'updateProjectDescription', (description, callback)->
-				AuthorizationManager.ensureClientCanEditProject client, (error, project_id) =>
-					EditorController.updateProjectDescription project_id, description, callback
-
-			client.on "getPublishedDetails", (user_id, callback)->
-				AuthorizationManager.ensureClientCanViewProject client, (error, project_id) =>
-					TemplatesController.getTemplateDetails user_id, project_id, callback
+			# client.on 'publishProjectAsTemplate', (user_id, callback)->
+			# 	AuthorizationManager.ensureClientCanAdminProject client, (error, project_id) =>
+			# 		TemplatesController.publishProject user_id, project_id, callback
+			# 
+			# client.on 'unPublishProjectAsTemplate', (user_id, callback)->
+			# 	AuthorizationManager.ensureClientCanAdminProject client, (error, project_id) =>
+			# 		TemplatesController.unPublishProject user_id, project_id, callback
+			# 
+			# client.on 'updateProjectDescription', (description, callback)->
+			# 	AuthorizationManager.ensureClientCanEditProject client, (error, project_id) =>
+			# 		EditorController.updateProjectDescription project_id, description, callback
+			# 
+			# client.on "getPublishedDetails", (user_id, callback)->
+			# 	AuthorizationManager.ensureClientCanViewProject client, (error, project_id) =>
+			# 		TemplatesController.getTemplateDetails user_id, project_id, callback
 

+ 33 - 16
services/web/public/coffee/ide/templates/controllers/TemplatesController.coffee

@@ -17,7 +17,7 @@ define [
 			}
 			modal.result.then(resetState, resetState)
 
-	App.controller "PublishProjectAsTemplateModalController", ($scope, $modalInstance, ide) ->
+	App.controller "PublishProjectAsTemplateModalController", ($scope, $modalInstance, ide, $http) ->
 		user_id = ide.$scope.user.id
 		$scope.templateDetails = {exists:false}
 
@@ -29,11 +29,13 @@ define [
 			$scope.problemTalkingToTemplateApi = true
 
 		refreshPublishedStatus = ->
-			ide.socket.emit "getPublishedDetails", user_id, (err, data)->
-				if !data? or err? then return problemTalkingToTemplateApi()
-				$scope.templateDetails = data
-				$scope.templateDetails.publishedDate = moment(data.publishedDate).format("Do MMM YYYY, h:mm a")
-				$scope.templateDetails.description = data.description
+			$http.get("/project/#{ide.project_id}/template")
+				.success (data) ->
+					$scope.templateDetails = data
+					$scope.templateDetails.publishedDate = moment(data.publishedDate).format("Do MMM YYYY, h:mm a")
+					$scope.templateDetails.description = data.description
+				.error () ->
+					problemTalkingToTemplateApi()
 
 		refreshPublishedStatus()
 		$scope.$watch $scope.problemTalkingToTemplateApi, refreshPublishedStatus
@@ -41,22 +43,37 @@ define [
 		$scope.updateProjectDescription = ->
 			description = $scope.templateDetails.description
 			if description?
-				ide.socket.emit 'updateProjectDescription', description, (err) => 
-					if err? then return problemTalkingToTemplateApi()
+				$http
+					.post("/project/#{ide.project_id}/template/description", {
+						description: description
+						_csrf: window.csrfToken
+					})
+					.error () ->
+						problemTalkingToTemplateApi()
 
 		$scope.publishTemplate = ->
 			$scope.state.publishInflight = true
-			ide.socket.emit 'publishProjectAsTemplate', user_id, (error) =>
-				if err? then return problemTalkingToTemplateApi()
-				refreshPublishedStatus()
-				$scope.state.publishInflight = false
+			$http
+				.post("/project/#{ide.project_id}/template/publish", {
+					_csrf: window.csrfToken
+				})
+				.success () ->
+					refreshPublishedStatus()
+					$scope.state.publishInflight = false
+				.error () ->
+					problemTalkingToTemplateApi()
 
 		$scope.unpublishTemplate = ->
 			$scope.state.unpublishInflight = true
-			ide.socket.emit 'unPublishProjectAsTemplate', user_id, (error) =>
-				if err? then return problemTalkingToTemplateApi()
-				refreshPublishedStatus()
-				$scope.state.unpublishInflight = false
+			$http
+				.post("/project/#{ide.project_id}/template/unpublish", {
+					_csrf: window.csrfToken
+				})
+				.success () ->
+					refreshPublishedStatus()
+					$scope.state.unpublishInflight = false
+				.error () ->
+					problemTalkingToTemplateApi()
 
 		$scope.cancel = () ->
 			$modalInstance.dismiss()

+ 95 - 56
services/web/test/UnitTests/coffee/Templates/TemplatesControllerTests.coffee

@@ -31,6 +31,8 @@ describe 'TemplatesController', ->
 			'../Uploads/ProjectUploadManager':@ProjectUploadManager
 			'../Project/ProjectOptionsHandler':@ProjectOptionsHandler
 			'../Project/ProjectDetailsHandler':@ProjectDetailsHandler
+			'../Project/ProjectGetter':@ProjectGetter = {}
+			'../Editor/EditorController': @EditorController = {}
 			'./TemplatesPublisher':@TemplatesPublisher
 			"logger-sharelatex": 
 				log:->
@@ -86,42 +88,57 @@ describe 'TemplatesController', ->
 			res = redirect:redirect
 			@controller.createProjectFromZipTemplate @req, res
 
-	describe 'publishProject', (done)->
+	describe 'publishProject', ->
 		beforeEach ->
-			@user_id = "sdijdlsakjlkajdklaj"
-			@project_id = "23213kl2j13lk1"
-
-		it 'should pass the user id and project id to the handler', (done)->
-			@TemplatesPublisher.publish.callsArgWith(2)
-			@controller.publishProject @user_id, @project_id, =>
-				@TemplatesPublisher.publish.calledWith(@user_id, @project_id).should.equal true
-				done()
-
-		it 'should pass the error back', (done)->
-			error = "error"
-			@TemplatesPublisher.publish.callsArgWith(2, error)
-			@controller.publishProject @user_id, @project_id, (passedError)=>
-				passedError.should.equal error
-				done()
-
-	describe 'unpublish Project', (done)->
+			@user_id = "user-id-123"
+			@project_id = "project-id-123"
+			@res =
+				send: sinon.stub()
+			@req.params =
+				Project_id: @project_id
+				
+			@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
+			@TemplatesPublisher.publish = sinon.stub().callsArgWith(2)
+			@controller.publishProject @req, @res
+			
+		it "should look up the project owner", ->
+			@ProjectGetter.getProject
+				.calledWith(@project_id, { owner_ref: 1 })
+				.should.equal true
+				
+		it "should publish the template", ->
+			@TemplatesPublisher.publish
+				.calledWith(@user_id, @project_id)
+				.should.equal true
+				
+		it "should return a success status", ->
+			@res.send.calledWith(204).should.equal true
+
+	describe 'unpublishProject', ->
 		beforeEach ->
-			@user_id = "sdijdlsakjlkajdklaj"
-			@project_id = "23213kl2j13lk1"
-
-		it 'should pass the user id and project id to the handler', (done)->
-			@TemplatesPublisher.unpublish.callsArgWith(2)
-			@controller.unPublishProject @user_id, @project_id, =>
-				@TemplatesPublisher.unpublish.calledWith(@user_id, @project_id).should.equal true
-				done()
-
-		it 'should pass the error back', (done)->
-			error = "error"
-			@TemplatesPublisher.unpublish.callsArgWith(2, error)
-			@controller.unPublishProject @user_id, @project_id, (passedError)=>
-				passedError.should.equal error
-				done()
-
+			@user_id = "user-id-123"
+			@project_id = "project-id-123"
+			@res =
+				send: sinon.stub()
+			@req.params =
+				Project_id: @project_id
+				
+			@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
+			@TemplatesPublisher.unpublish = sinon.stub().callsArgWith(2)
+			@controller.unpublishProject @req, @res
+			
+		it "should look up the project owner", ->
+			@ProjectGetter.getProject
+				.calledWith(@project_id, { owner_ref: 1 })
+				.should.equal true
+				
+		it "should publish the template", ->
+			@TemplatesPublisher.unpublish
+				.calledWith(@user_id, @project_id)
+				.should.equal true
+				
+		it "should return a success status", ->
+			@res.send.calledWith(204).should.equal true
 
 	describe 'settings the compiler from the query string', ->
 		it 'should use the said compiler', (done)->
@@ -139,29 +156,51 @@ describe 'TemplatesController', ->
 			res = redirect:redirect
 			@controller.createProjectFromZipTemplate @req, res
 
+	describe "updateProjectDescription", ->
+		beforeEach ->
+			@EditorController.updateProjectDescription = sinon.stub().callsArg(2)
+			@res =
+				send: sinon.stub()
+			@req.params =
+				Project_id: @project_id = "project-id-123"
+			@req.body =
+				description: @description = "test description"
+				
+			@controller.updateProjectDescription @req, @res
+			
+		it "should update the project description", ->
+			@EditorController.updateProjectDescription
+				.calledWith(@project_id, @description)
+				.should.equal true
+				
+		it "should return a success code", ->
+			@res.send.calledWith(204).should.equal true
 
 	describe 'getTemplateDetails', ->
 		beforeEach ->
-			@description = "this project is nice"
-			@ProjectDetailsHandler.getProjectDescription.callsArgWith(1, null, @description)
-
-		it "should return an error the templatePublisher", (done)->
-			error = "error"
-			@TemplatesPublisher.getTemplateDetails.callsArgWith(2, error)
-			@controller.getTemplateDetails @user_id, @project_id, (passedError)=>
-				passedError.should.equal error
-				done()
-
-		it "should return the details", (done)->
-			details = {exists:true}
-			@TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, details)
-			@controller.getTemplateDetails @user_id, @project_id, (err, passedDetails)=>
-				details.should.equal passedDetails
-				done()
-
-		it "should get the template description", (done)->
-			@TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, {})
-			@controller.getTemplateDetails @user_id, @project_id, (err, passedDetails)=>
-				passedDetails.description.should.equal @description
-				done()
+			@user_id = "user-id-123"
+			@project_id = "project-id-123"
+			@res =
+				json: sinon.stub()
+			@req.params =
+				Project_id: @project_id
+				
+			@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
+			@TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, @details = {exists: true})
+			@ProjectDetailsHandler.getProjectDescription.callsArgWith(1, null, @description = "test description")
+			
+			@controller.getTemplateDetails @req, @res
+
+		it "should get the template details for the user_id and project_id", ->
+			@TemplatesPublisher.getTemplateDetails
+				.calledWith(@user_id, @project_id)
+				.should.equal true
+				
+		it "should return the details and description", ->
+			@res.json
+				.calledWith({
+					exists: @details.exists
+					description: @description
+				})
+				.should.equal true