2014-02-12 05:23:40 -05:00
|
|
|
logger = require('logger-sharelatex')
|
2017-04-03 11:18:30 -04:00
|
|
|
Metrics = require('metrics-sharelatex')
|
2014-03-31 11:46:28 -04:00
|
|
|
sanitize = require('sanitizer')
|
2014-02-12 05:23:40 -05:00
|
|
|
ProjectEntityHandler = require('../Project/ProjectEntityHandler')
|
|
|
|
ProjectOptionsHandler = require('../Project/ProjectOptionsHandler')
|
|
|
|
ProjectDetailsHandler = require('../Project/ProjectDetailsHandler')
|
2014-04-07 08:47:10 -04:00
|
|
|
ProjectDeleter = require("../Project/ProjectDeleter")
|
2014-02-12 05:23:40 -05:00
|
|
|
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
|
|
|
|
EditorRealTimeController = require("./EditorRealTimeController")
|
|
|
|
async = require('async')
|
2014-11-26 09:07:04 -05:00
|
|
|
LockManager = require("../../infrastructure/LockManager")
|
2014-02-12 05:23:40 -05:00
|
|
|
_ = require('underscore')
|
|
|
|
|
|
|
|
module.exports = EditorController =
|
2016-02-04 09:26:50 -05:00
|
|
|
setDoc: (project_id, doc_id, user_id, docLines, source, callback = (err)->)->
|
|
|
|
DocumentUpdaterHandler.setDocument project_id, doc_id, user_id, docLines, source, (err)=>
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.log project_id:project_id, doc_id:doc_id, "notifying users that the document has been updated"
|
2014-05-08 10:47:50 -04:00
|
|
|
DocumentUpdaterHandler.flushDocToMongo project_id, doc_id, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-11-26 09:07:04 -05:00
|
|
|
|
2014-10-15 10:18:31 -04:00
|
|
|
addDoc: (project_id, folder_id, docName, docLines, source, callback = (error, doc)->)->
|
2014-11-26 09:07:04 -05:00
|
|
|
LockManager.getLock project_id, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, source:source, "could not get lock to addDoc"
|
|
|
|
return callback(err)
|
|
|
|
EditorController.addDocWithoutLock project_id, folder_id, docName, docLines, source, (error, doc)->
|
|
|
|
LockManager.releaseLock project_id, ->
|
|
|
|
callback(error, doc)
|
|
|
|
|
|
|
|
addDocWithoutLock: (project_id, folder_id, docName, docLines, source, callback = (error, doc)->)->
|
2014-07-09 06:05:00 -04:00
|
|
|
docName = docName.trim()
|
2014-10-15 10:18:31 -04:00
|
|
|
logger.log {project_id, folder_id, docName, source}, "sending new doc to project"
|
2014-02-12 05:23:40 -05:00
|
|
|
Metrics.inc "editor.add-doc"
|
2014-10-15 09:11:02 -04:00
|
|
|
ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, (err, doc, folder_id)=>
|
2016-02-29 08:05:37 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, docName:docName, "error adding doc without lock"
|
|
|
|
return callback(err)
|
2014-10-15 10:18:31 -04:00
|
|
|
EditorRealTimeController.emitToRoom(project_id, 'reciveNewDoc', folder_id, doc, source)
|
2014-02-12 05:23:40 -05:00
|
|
|
callback(err, doc)
|
|
|
|
|
2014-10-15 10:18:31 -04:00
|
|
|
addFile: (project_id, folder_id, fileName, path, source, callback = (error, file)->)->
|
2014-11-26 09:07:04 -05:00
|
|
|
LockManager.getLock project_id, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, source:source, "could not get lock to addFile"
|
|
|
|
return callback(err)
|
|
|
|
EditorController.addFileWithoutLock project_id, folder_id, fileName, path, source, (error, file)->
|
|
|
|
LockManager.releaseLock project_id, ->
|
|
|
|
callback(error, file)
|
|
|
|
|
|
|
|
addFileWithoutLock: (project_id, folder_id, fileName, path, source, callback = (error, file)->)->
|
2014-07-09 06:05:00 -04:00
|
|
|
fileName = fileName.trim()
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log {project_id, folder_id, fileName, path}, "sending new file to project"
|
2014-02-12 05:23:40 -05:00
|
|
|
Metrics.inc "editor.add-file"
|
|
|
|
ProjectEntityHandler.addFile project_id, folder_id, fileName, path, (err, fileRef, folder_id)=>
|
2016-02-29 08:05:37 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, folder_id:folder_id, fileName:fileName, "error adding file without lock"
|
|
|
|
return callback(err)
|
2014-10-15 10:18:31 -04:00
|
|
|
EditorRealTimeController.emitToRoom(project_id, 'reciveNewFile', folder_id, fileRef, source)
|
2014-02-12 05:23:40 -05:00
|
|
|
callback(err, fileRef)
|
|
|
|
|
2014-10-15 10:18:31 -04:00
|
|
|
replaceFile: (project_id, file_id, fsPath, source, callback = (error) ->)->
|
2014-10-15 09:11:02 -04:00
|
|
|
ProjectEntityHandler.replaceFile project_id, file_id, fsPath, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-11-26 09:07:04 -05:00
|
|
|
addFolder : (project_id, folder_id, folderName, source, callback = (error, folder)->)->
|
|
|
|
LockManager.getLock project_id, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, source:source, "could not get lock to addFolder"
|
|
|
|
return callback(err)
|
|
|
|
EditorController.addFolderWithoutLock project_id, folder_id, folderName, source, (error, folder)->
|
|
|
|
LockManager.releaseLock project_id, ->
|
|
|
|
callback(error, folder)
|
|
|
|
|
|
|
|
addFolderWithoutLock: (project_id, folder_id, folderName, source, callback = (error, folder)->)->
|
2014-07-09 06:05:00 -04:00
|
|
|
folderName = folderName.trim()
|
2014-10-16 06:26:57 -04:00
|
|
|
logger.log {project_id, folder_id, folderName, source}, "sending new folder to project"
|
2014-02-12 05:23:40 -05:00
|
|
|
Metrics.inc "editor.add-folder"
|
|
|
|
ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, folder_id)=>
|
2016-02-29 08:05:37 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, folder_id:folder_id, folderName:folderName, "error adding folder without lock"
|
|
|
|
return callback(err)
|
2014-02-12 05:23:40 -05:00
|
|
|
@p.notifyProjectUsersOfNewFolder project_id, folder_id, folder, (error) ->
|
|
|
|
callback error, folder
|
|
|
|
|
2014-11-26 09:07:04 -05:00
|
|
|
|
|
|
|
mkdirp : (project_id, path, callback)->
|
|
|
|
LockManager.getLock project_id, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, "could not get lock to mkdirp"
|
|
|
|
return callback(err)
|
|
|
|
EditorController.mkdirpWithoutLock project_id, path, (err, newFolders, lastFolder)->
|
|
|
|
LockManager.releaseLock project_id, ->
|
|
|
|
callback(err, newFolders, lastFolder)
|
|
|
|
|
|
|
|
mkdirpWithoutLock: (project_id, path, callback)->
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.log project_id:project_id, path:path, "making directories if they don't exist"
|
|
|
|
ProjectEntityHandler.mkdirp project_id, path, (err, newFolders, lastFolder)=>
|
2016-02-29 08:05:37 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, path:path, "error mkdirp without lock"
|
|
|
|
return callback(err)
|
2014-02-12 05:23:40 -05:00
|
|
|
self = @
|
|
|
|
jobs = _.map newFolders, (folder, index)->
|
|
|
|
return (cb)->
|
|
|
|
self.p.notifyProjectUsersOfNewFolder project_id, folder.parentFolder_id, folder, cb
|
|
|
|
async.series jobs, (err)->
|
|
|
|
callback err, newFolders, lastFolder
|
|
|
|
|
2014-11-26 09:07:04 -05:00
|
|
|
deleteEntity : (project_id, entity_id, entityType, source, callback)->
|
|
|
|
LockManager.getLock project_id, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, "could not get lock to deleteEntity"
|
|
|
|
return callback(err)
|
|
|
|
EditorController.deleteEntityWithoutLock project_id, entity_id, entityType, source, (err)->
|
|
|
|
LockManager.releaseLock project_id, ()->
|
|
|
|
callback(err)
|
|
|
|
|
|
|
|
deleteEntityWithoutLock: (project_id, entity_id, entityType, source, callback)->
|
2014-10-15 10:18:31 -04:00
|
|
|
logger.log {project_id, entity_id, entityType, source}, "start delete process of entity"
|
2014-02-12 05:23:40 -05:00
|
|
|
Metrics.inc "editor.delete-entity"
|
2016-02-29 08:05:37 -05:00
|
|
|
ProjectEntityHandler.deleteEntity project_id, entity_id, entityType, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, entity_id:entity_id, entityType:entityType, "error deleting entity"
|
|
|
|
return callback(err)
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log project_id:project_id, entity_id:entity_id, entityType:entityType, "telling users entity has been deleted"
|
2014-10-15 10:18:31 -04:00
|
|
|
EditorRealTimeController.emitToRoom(project_id, 'removeEntity', entity_id, source)
|
2014-02-12 05:23:40 -05:00
|
|
|
if callback?
|
|
|
|
callback()
|
|
|
|
|
|
|
|
getListOfDocPaths: (project_id, callback)->
|
|
|
|
ProjectEntityHandler.getAllDocs project_id, (err, docs)->
|
|
|
|
docList = _.map docs, (doc, path)->
|
|
|
|
return {_id:doc._id, path:path.substring(1)}
|
|
|
|
callback(null, docList)
|
|
|
|
|
|
|
|
notifyUsersProjectHasBeenDeletedOrRenamed: (project_id, callback)->
|
|
|
|
EditorRealTimeController.emitToRoom(project_id, 'projectRenamedOrDeletedByExternalSource')
|
|
|
|
callback()
|
|
|
|
|
|
|
|
updateProjectDescription: (project_id, description, callback = ->)->
|
|
|
|
logger.log project_id:project_id, description:description, "updating project description"
|
|
|
|
ProjectDetailsHandler.setProjectDescription project_id, description, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, description:description, "something went wrong setting the project description"
|
|
|
|
return callback(err)
|
|
|
|
EditorRealTimeController.emitToRoom(project_id, 'projectDescriptionUpdated', description)
|
|
|
|
callback()
|
|
|
|
|
2014-04-04 11:21:20 -04:00
|
|
|
deleteProject: (project_id, callback)->
|
|
|
|
Metrics.inc "editor.delete-project"
|
|
|
|
logger.log project_id:project_id, "recived message to delete project"
|
2014-04-07 08:47:10 -04:00
|
|
|
ProjectDeleter.deleteProject project_id, callback
|
2014-04-04 11:21:20 -04:00
|
|
|
|
2014-04-04 11:35:02 -04:00
|
|
|
renameEntity: (project_id, entity_id, entityType, newName, callback)->
|
|
|
|
newName = sanitize.escape(newName)
|
|
|
|
Metrics.inc "editor.rename-entity"
|
|
|
|
logger.log entity_id:entity_id, entity_id:entity_id, entity_id:entity_id, "reciving new name for entity for project"
|
2016-02-29 08:05:37 -05:00
|
|
|
ProjectEntityHandler.renameEntity project_id, entity_id, entityType, newName, ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, entity_id:entity_id, entityType:entityType, newName:newName, "error renaming entity"
|
|
|
|
return callback(err)
|
2014-04-04 11:35:02 -04:00
|
|
|
if newName.length > 0
|
|
|
|
EditorRealTimeController.emitToRoom project_id, 'reciveEntityRename', entity_id, newName
|
|
|
|
callback?()
|
2016-02-29 08:05:37 -05:00
|
|
|
|
2014-04-04 11:40:53 -04:00
|
|
|
moveEntity: (project_id, entity_id, folder_id, entityType, callback)->
|
|
|
|
Metrics.inc "editor.move-entity"
|
2016-03-15 08:29:41 -04:00
|
|
|
LockManager.getLock project_id, (err)->
|
2016-02-29 08:05:37 -05:00
|
|
|
if err?
|
2016-03-15 08:29:41 -04:00
|
|
|
logger.err err:err, project_id:project_id, "could not get lock for move entity"
|
2016-02-29 08:05:37 -05:00
|
|
|
return callback(err)
|
2016-03-15 08:29:41 -04:00
|
|
|
ProjectEntityHandler.moveEntity project_id, entity_id, folder_id, entityType, =>
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, entity_id:entity_id, folder_id:folder_id, "error moving entity"
|
|
|
|
return callback(err)
|
|
|
|
LockManager.releaseLock project_id, ->
|
|
|
|
EditorRealTimeController.emitToRoom project_id, 'reciveEntityMove', entity_id, folder_id
|
|
|
|
callback?()
|
2014-04-04 11:35:02 -04:00
|
|
|
|
2014-06-25 08:51:02 -04:00
|
|
|
renameProject: (project_id, newName, callback = (err) ->) ->
|
2017-05-19 11:21:02 -04:00
|
|
|
ProjectDetailsHandler.renameProject project_id, newName, (err) ->
|
2016-02-29 08:05:37 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, newName:newName, "error renaming project"
|
|
|
|
return callback(err)
|
2014-04-07 10:37:40 -04:00
|
|
|
EditorRealTimeController.emitToRoom project_id, 'projectNameUpdated', newName
|
2014-06-25 08:51:02 -04:00
|
|
|
callback()
|
|
|
|
|
|
|
|
setCompiler : (project_id, compiler, callback = (err) ->) ->
|
|
|
|
ProjectOptionsHandler.setCompiler project_id, compiler, (err) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
logger.log compiler:compiler, project_id:project_id, "setting compiler"
|
|
|
|
EditorRealTimeController.emitToRoom project_id, 'compilerUpdated', compiler
|
|
|
|
callback()
|
|
|
|
|
|
|
|
setSpellCheckLanguage : (project_id, languageCode, callback = (err) ->) ->
|
|
|
|
ProjectOptionsHandler.setSpellCheckLanguage project_id, languageCode, (err) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
logger.log languageCode:languageCode, project_id:project_id, "setting languageCode for spell check"
|
|
|
|
EditorRealTimeController.emitToRoom project_id, 'spellCheckLanguageUpdated', languageCode
|
|
|
|
callback()
|
2014-04-04 11:49:44 -04:00
|
|
|
|
2014-06-25 08:51:02 -04:00
|
|
|
setPublicAccessLevel : (project_id, newAccessLevel, callback = (err) ->) ->
|
|
|
|
ProjectDetailsHandler.setPublicAccessLevel project_id, newAccessLevel, (err) ->
|
|
|
|
return callback(err) if err?
|
2014-04-04 11:53:59 -04:00
|
|
|
EditorRealTimeController.emitToRoom project_id, 'publicAccessLevelUpdated', newAccessLevel
|
2014-06-25 08:51:02 -04:00
|
|
|
callback()
|
2014-04-04 11:53:59 -04:00
|
|
|
|
2014-06-25 08:51:02 -04:00
|
|
|
setRootDoc: (project_id, newRootDocID, callback = (err) ->) ->
|
|
|
|
ProjectEntityHandler.setRootDoc project_id, newRootDocID, (err) ->
|
|
|
|
return callback(err) if err?
|
2014-04-04 11:59:45 -04:00
|
|
|
EditorRealTimeController.emitToRoom project_id, 'rootDocUpdated', newRootDocID
|
2014-06-25 08:51:02 -04:00
|
|
|
callback()
|
2014-04-04 11:59:45 -04:00
|
|
|
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
p:
|
|
|
|
notifyProjectUsersOfNewFolder: (project_id, folder_id, folder, callback = (error)->)->
|
|
|
|
logger.log project_id:project_id, folder:folder, parentFolder_id:folder_id, "sending newly created folder out to users"
|
|
|
|
EditorRealTimeController.emitToRoom(project_id, "reciveNewFolder", folder_id, folder)
|
|
|
|
callback()
|
|
|
|
|