overleaf/services/web/app/coffee/Features/Editor/EditorHttpController.coffee

141 lines
5.4 KiB
CoffeeScript
Raw Normal View History

2014-06-05 15:18:25 +00:00
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
ProjectDeleter = require "../Project/ProjectDeleter"
2014-06-05 15:18:25 +00:00
logger = require "logger-sharelatex"
EditorRealTimeController = require "./EditorRealTimeController"
2014-06-22 11:10:42 +00:00
EditorController = require "./EditorController"
2015-02-05 16:37:37 +00:00
ProjectGetter = require('../Project/ProjectGetter')
UserGetter = require('../User/UserGetter')
AuthorizationManager = require("../Authorization/AuthorizationManager")
2015-02-05 16:37:37 +00:00
ProjectEditorHandler = require('../Project/ProjectEditorHandler')
Metrics = require('../../infrastructure/Metrics')
CollaboratorsHandler = require("../Collaborators/CollaboratorsHandler")
CollaboratorsInviteHandler = require("../Collaborators/CollaboratorsInviteHandler")
PrivilegeLevels = require "../Authorization/PrivilegeLevels"
2014-06-05 15:18:25 +00:00
module.exports = EditorHttpController =
joinProject: (req, res, next) ->
project_id = req.params.Project_id
user_id = req.query.user_id
if user_id == "anonymous-user"
user_id = null
logger.log {user_id, project_id}, "join project request"
Metrics.inc "editor.join-project"
2015-02-05 16:37:37 +00:00
EditorHttpController._buildJoinProjectView project_id, user_id, (error, project, privilegeLevel) ->
return next(error) if error?
res.json {
project: project
privilegeLevel: privilegeLevel
}
# Only show the 'renamed or deleted' message once
2014-12-12 10:27:14 +00:00
if project?.deletedByExternalDataSource
ProjectDeleter.unmarkAsDeletedByExternalSource project_id
2015-02-05 16:37:37 +00:00
_buildJoinProjectView: (project_id, user_id, callback = (error, project, privilegeLevel) ->) ->
ProjectGetter.getProjectWithoutDocLines project_id, (error, project) ->
return callback(error) if error?
return callback(new Error("not found")) if !project?
CollaboratorsHandler.getMembersWithPrivilegeLevels project, (error, members) ->
2015-02-05 16:37:37 +00:00
return callback(error) if error?
UserGetter.getUser user_id, { isAdmin: true }, (error, user) ->
return callback(error) if error?
AuthorizationManager.getPrivilegeLevelForProject user_id, project_id, (error, privilegeLevel) ->
2015-02-05 16:37:37 +00:00
return callback(error) if error?
CollaboratorsInviteHandler.getAllInvites project_id, (error, invites) ->
return callback(error) if error?
if !privilegeLevel? or privilegeLevel == PrivilegeLevels.NONE
callback null, null, false
else
callback(null,
ProjectEditorHandler.buildProjectModelView(project, members, invites),
privilegeLevel
)
2015-02-05 16:37:37 +00:00
2014-06-05 15:18:25 +00:00
restoreDoc: (req, res, next) ->
project_id = req.params.Project_id
doc_id = req.params.doc_id
name = req.body.name
if !name?
2015-07-08 15:56:38 +00:00
return res.sendStatus 400 # Malformed request
2014-06-05 15:18:25 +00:00
logger.log project_id: project_id, doc_id: doc_id, "restoring doc"
ProjectEntityHandler.restoreDoc project_id, doc_id, name, (err, doc, folder_id) =>
return next(error) if error?
EditorRealTimeController.emitToRoom(project_id, 'reciveNewDoc', folder_id, doc)
res.json {
doc_id: doc._id
}
_nameIsAcceptableLength: (name)->
return name? and name.length < 150 and name.length != 0
2014-06-22 13:39:38 +00:00
2014-06-22 11:10:42 +00:00
addDoc: (req, res, next) ->
project_id = req.params.Project_id
name = req.body.name
parent_folder_id = req.body.parent_folder_id
logger.log project_id:project_id, name:name, parent_folder_id:parent_folder_id, "getting request to add doc to project"
if !EditorHttpController._nameIsAcceptableLength(name)
2015-07-08 15:56:38 +00:00
return res.sendStatus 400
EditorController.addDoc project_id, parent_folder_id, name, [], "editor", (error, doc) ->
if error == "project_has_to_many_files"
res.status(400).json(req.i18n.translate("project_has_to_many_files"))
else if error?
next(error)
else
res.json doc
2014-06-22 13:39:38 +00:00
addFolder: (req, res, next) ->
project_id = req.params.Project_id
name = req.body.name
parent_folder_id = req.body.parent_folder_id
if !EditorHttpController._nameIsAcceptableLength(name)
2015-07-08 15:56:38 +00:00
return res.sendStatus 400
EditorController.addFolder project_id, parent_folder_id, name, "editor", (error, doc) ->
if error == "project_has_to_many_files"
res.status(400).json(req.i18n.translate("project_has_to_many_files"))
else if error?
next(error)
else
res.json doc
2014-06-22 19:08:56 +00:00
renameEntity: (req, res, next) ->
project_id = req.params.Project_id
entity_id = req.params.entity_id
entity_type = req.params.entity_type
name = req.body.name
if !EditorHttpController._nameIsAcceptableLength(name)
2015-07-08 15:56:38 +00:00
return res.sendStatus 400
2014-06-22 19:08:56 +00:00
EditorController.renameEntity project_id, entity_id, entity_type, name, (error) ->
return next(error) if error?
2015-07-08 15:56:38 +00:00
res.sendStatus 204
2014-06-22 19:08:56 +00:00
moveEntity: (req, res, next) ->
project_id = req.params.Project_id
entity_id = req.params.entity_id
entity_type = req.params.entity_type
folder_id = req.body.folder_id
EditorController.moveEntity project_id, entity_id, folder_id, entity_type, (error) ->
return next(error) if error?
2015-07-08 15:56:38 +00:00
res.sendStatus 204
2014-06-22 19:08:56 +00:00
deleteDoc: (req, res, next)->
req.params.entity_type = "doc"
EditorHttpController.deleteEntity(req, res, next)
deleteFile: (req, res, next)->
req.params.entity_type = "file"
EditorHttpController.deleteEntity(req, res, next)
deleteFolder: (req, res, next)->
req.params.entity_type = "folder"
EditorHttpController.deleteEntity(req, res, next)
2014-06-22 19:08:56 +00:00
deleteEntity: (req, res, next) ->
project_id = req.params.Project_id
entity_id = req.params.entity_id
entity_type = req.params.entity_type
EditorController.deleteEntity project_id, entity_id, entity_type, "editor", (error) ->
2014-06-22 19:08:56 +00:00
return next(error) if error?
2015-07-08 15:56:38 +00:00
res.sendStatus 204