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')
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler = require('../Project/ProjectEntityUpdateHandler')
|
2014-02-12 05:23:40 -05:00
|
|
|
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')
|
2017-10-04 11:31:24 -04:00
|
|
|
PublicAccessLevels = require("../Authorization/PublicAccessLevels")
|
2014-02-12 05:23:40 -05:00
|
|
|
_ = require('underscore')
|
|
|
|
|
|
|
|
module.exports = EditorController =
|
2017-11-23 10:40:14 -05:00
|
|
|
addDoc: (project_id, folder_id, docName, docLines, source, user_id, 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"
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.addDoc project_id, folder_id, docName, docLines, user_id, (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)
|
|
|
|
|
2018-02-14 10:12:46 -05:00
|
|
|
addFile: (project_id, folder_id, fileName, fsPath, linkedFileData, source, user_id, callback = (error, file)->)->
|
2014-07-09 06:05:00 -04:00
|
|
|
fileName = fileName.trim()
|
2018-02-14 10:12:46 -05:00
|
|
|
logger.log {project_id, folder_id, fileName, fsPath, linkedFileData, source, user_id}, "sending new file to project"
|
2014-02-12 05:23:40 -05:00
|
|
|
Metrics.inc "editor.add-file"
|
2018-02-14 10:12:46 -05:00
|
|
|
ProjectEntityUpdateHandler.addFile project_id, folder_id, fileName, fsPath, linkedFileData, user_id, (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)
|
2018-02-20 06:49:02 -05:00
|
|
|
EditorRealTimeController.emitToRoom(project_id, 'reciveNewFile', folder_id, fileRef, source, linkedFileData)
|
2014-02-12 05:23:40 -05:00
|
|
|
callback(err, fileRef)
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
upsertDoc: (project_id, folder_id, docName, docLines, source, user_id, callback = (err)->)->
|
|
|
|
ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, docName, docLines, source, user_id, (err, doc, didAddNewDoc) ->
|
|
|
|
if didAddNewDoc
|
|
|
|
EditorRealTimeController.emitToRoom(project_id, 'reciveNewDoc', folder_id, doc, source)
|
|
|
|
callback err, doc
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-02-14 10:12:46 -05:00
|
|
|
upsertFile: (project_id, folder_id, fileName, fsPath, linkedFileData, source, user_id, callback = (err, file) ->) ->
|
2018-03-27 07:08:30 -04:00
|
|
|
ProjectEntityUpdateHandler.upsertFile project_id, folder_id, fileName, fsPath, linkedFileData, user_id, (err, newFile, didAddFile, existingFile) ->
|
2018-02-01 10:31:42 -05:00
|
|
|
return callback(err) if err?
|
2018-03-27 07:08:30 -04:00
|
|
|
if not didAddFile # replacement, so remove the existing file from the client
|
|
|
|
EditorRealTimeController.emitToRoom project_id, 'removeEntity', existingFile._id, source
|
|
|
|
# now add the new file on the client
|
|
|
|
EditorRealTimeController.emitToRoom project_id, 'reciveNewFile', folder_id, newFile, source, linkedFileData
|
|
|
|
callback null, newFile
|
2018-02-01 10:31:42 -05:00
|
|
|
|
|
|
|
upsertDocWithPath: (project_id, elementPath, docLines, source, user_id, callback) ->
|
|
|
|
ProjectEntityUpdateHandler.upsertDocWithPath project_id, elementPath, docLines, source, user_id, (err, doc, didAddNewDoc, newFolders, lastFolder) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
EditorController._notifyProjectUsersOfNewFolders project_id, newFolders, (err) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
if didAddNewDoc
|
|
|
|
EditorRealTimeController.emitToRoom project_id, 'reciveNewDoc', lastFolder._id, doc, source
|
|
|
|
callback()
|
|
|
|
|
2018-02-14 10:12:46 -05:00
|
|
|
upsertFileWithPath: (project_id, elementPath, fsPath, linkedFileData, source, user_id, callback) ->
|
2018-03-27 07:08:30 -04:00
|
|
|
ProjectEntityUpdateHandler.upsertFileWithPath project_id, elementPath, fsPath, linkedFileData, user_id, (err, newFile, didAddFile, existingFile, newFolders, lastFolder) ->
|
2018-02-01 10:31:42 -05:00
|
|
|
return callback(err) if err?
|
|
|
|
EditorController._notifyProjectUsersOfNewFolders project_id, newFolders, (err) ->
|
|
|
|
return callback(err) if err?
|
2018-03-27 07:08:30 -04:00
|
|
|
if not didAddFile # replacement, so remove the existing file from the client
|
|
|
|
EditorRealTimeController.emitToRoom project_id, 'removeEntity', existingFile._id, source
|
|
|
|
# now add the new file on the client
|
|
|
|
EditorRealTimeController.emitToRoom project_id, 'reciveNewFile', lastFolder._id, newFile, source, linkedFileData
|
2018-02-01 10:31:42 -05:00
|
|
|
callback()
|
2014-11-26 09:07:04 -05:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
addFolder : (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"
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.addFolder project_id, folder_id, folderName, (err, folder, folder_id)=>
|
2016-02-29 08:05:37 -05:00
|
|
|
if err?
|
2018-02-12 11:05:34 -05:00
|
|
|
logger.err {err, project_id, folder_id, folderName, source}, "could not add folder"
|
2016-02-29 08:05:37 -05:00
|
|
|
return callback(err)
|
2018-02-01 10:31:42 -05:00
|
|
|
EditorController._notifyProjectUsersOfNewFolder project_id, folder_id, folder, (err) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
callback null, folder
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-01-12 05:53:36 -05:00
|
|
|
mkdirp : (project_id, path, callback = (error, newFolders, lastFolder)->)->
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.log project_id:project_id, path:path, "making directories if they don't exist"
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.mkdirp project_id, path, (err, newFolders, lastFolder)=>
|
2016-02-29 08:05:37 -05:00
|
|
|
if err?
|
2018-02-12 11:05:34 -05:00
|
|
|
logger.err err:err, project_id:project_id, path:path, "could not mkdirp"
|
2016-02-29 08:05:37 -05:00
|
|
|
return callback(err)
|
2018-02-01 10:31:42 -05:00
|
|
|
|
|
|
|
EditorController._notifyProjectUsersOfNewFolders project_id, newFolders, (err) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
callback null, newFolders, lastFolder
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-01-12 05:53:36 -05:00
|
|
|
deleteEntity : (project_id, entity_id, entityType, source, userId, callback = (error)->)->
|
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"
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.deleteEntity project_id, entity_id, entityType, userId, (err)->
|
2016-02-29 08:05:37 -05:00
|
|
|
if err?
|
2018-02-01 10:31:42 -05:00
|
|
|
logger.err {err, project_id, entity_id, entityType}, "could not delete entity"
|
2016-02-29 08:05:37 -05:00
|
|
|
return callback(err)
|
2018-02-01 10:31:42 -05:00
|
|
|
logger.log {project_id, entity_id, entityType}, "telling users entity has been deleted"
|
2014-10-15 10:18:31 -04:00
|
|
|
EditorRealTimeController.emitToRoom(project_id, 'removeEntity', entity_id, source)
|
2018-02-01 10:31:42 -05:00
|
|
|
callback()
|
|
|
|
|
|
|
|
deleteEntityWithPath: (project_id, path, source, user_id, callback) ->
|
|
|
|
ProjectEntityUpdateHandler.deleteEntityWithPath project_id, path, user_id, (err, entity_id) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
EditorRealTimeController.emitToRoom(project_id, 'removeEntity', entity_id, source)
|
|
|
|
callback null, entity_id
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-01-12 05:53:36 -05:00
|
|
|
renameEntity: (project_id, entity_id, entityType, newName, userId, callback = (error) ->)->
|
2014-04-04 11:35:02 -04:00
|
|
|
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"
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.renameEntity project_id, entity_id, entityType, newName, userId, (err) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, entity_id:entity_id, entityType:entityType, newName:newName, "error renaming entity"
|
|
|
|
return callback(err)
|
|
|
|
if newName.length > 0
|
|
|
|
EditorRealTimeController.emitToRoom project_id, 'reciveEntityRename', entity_id, newName
|
|
|
|
callback()
|
2016-02-29 08:05:37 -05:00
|
|
|
|
2018-01-12 05:53:36 -05:00
|
|
|
moveEntity: (project_id, entity_id, folder_id, entityType, userId, callback = (error) ->)->
|
2014-04-04 11:40:53 -04:00
|
|
|
Metrics.inc "editor.move-entity"
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.moveEntity project_id, entity_id, folder_id, entityType, userId, (err) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, entity_id:entity_id, folder_id:folder_id, "error moving entity"
|
|
|
|
return callback(err)
|
|
|
|
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()
|
|
|
|
|
2018-08-13 05:22:23 -04:00
|
|
|
setImageName : (project_id, imageName, callback = (err) ->) ->
|
|
|
|
ProjectOptionsHandler.setImageName project_id, imageName, (err) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
logger.log imageName:imageName, project_id:project_id, "setting imageName"
|
|
|
|
EditorRealTimeController.emitToRoom project_id, 'imageNameUpdated', imageName
|
|
|
|
callback()
|
|
|
|
|
2014-06-25 08:51:02 -04:00
|
|
|
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?
|
2017-10-04 11:31:24 -04:00
|
|
|
EditorRealTimeController.emitToRoom(
|
|
|
|
project_id,
|
|
|
|
'project:publicAccessLevel:changed',
|
|
|
|
{newAccessLevel}
|
|
|
|
)
|
|
|
|
if newAccessLevel == PublicAccessLevels.TOKEN_BASED
|
|
|
|
ProjectDetailsHandler.ensureTokensArePresent project_id, (err, tokens) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
EditorRealTimeController.emitToRoom(
|
|
|
|
project_id,
|
|
|
|
'project:tokens:changed',
|
|
|
|
{tokens}
|
|
|
|
)
|
|
|
|
callback()
|
|
|
|
else
|
|
|
|
callback()
|
2014-04-04 11:53:59 -04:00
|
|
|
|
2014-06-25 08:51:02 -04:00
|
|
|
setRootDoc: (project_id, newRootDocID, callback = (err) ->) ->
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.setRootDoc project_id, newRootDocID, (err) ->
|
2014-06-25 08:51:02 -04:00
|
|
|
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
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
_notifyProjectUsersOfNewFolders: (project_id, folders, callback = (error)->)->
|
|
|
|
async.eachSeries folders,
|
|
|
|
(folder, cb) -> EditorController._notifyProjectUsersOfNewFolder project_id, folder.parentFolder_id, folder, cb
|
|
|
|
callback
|
|
|
|
|
|
|
|
_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()
|