2014-04-08 10:28:18 -04:00
|
|
|
async = require("async")
|
2014-04-08 09:34:03 -04:00
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
projectDeleter = require("./ProjectDeleter")
|
2014-04-08 09:53:33 -04:00
|
|
|
projectDuplicator = require("./ProjectDuplicator")
|
2014-04-08 10:25:22 -04:00
|
|
|
projectCreationHandler = require("./ProjectCreationHandler")
|
2014-04-28 12:47:47 -04:00
|
|
|
editorController = require("../Editor/EditorController")
|
2014-04-08 09:53:33 -04:00
|
|
|
metrics = require('../../infrastructure/Metrics')
|
2014-04-08 12:44:31 -04:00
|
|
|
User = require('../../models/User').User
|
2014-04-08 11:40:12 -04:00
|
|
|
TagsHandler = require("../Tags/TagsHandler")
|
|
|
|
SubscriptionLocator = require("../Subscription/SubscriptionLocator")
|
2016-01-22 00:41:22 -05:00
|
|
|
NotificationsHandler = require("../Notifications/NotificationsHandler")
|
2014-08-07 10:29:06 -04:00
|
|
|
LimitationsManager = require("../Subscription/LimitationsManager")
|
2014-04-08 11:40:12 -04:00
|
|
|
_ = require("underscore")
|
2014-04-08 12:44:31 -04:00
|
|
|
Settings = require("settings-sharelatex")
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthorizationManager = require("../Authorization/AuthorizationManager")
|
2014-06-24 15:28:53 -04:00
|
|
|
fs = require "fs"
|
2015-08-13 17:40:28 -04:00
|
|
|
InactiveProjectManager = require("../InactiveData/InactiveProjectManager")
|
|
|
|
ProjectUpdateHandler = require("./ProjectUpdateHandler")
|
2016-03-03 12:19:03 -05:00
|
|
|
ProjectGetter = require("./ProjectGetter")
|
2016-03-15 10:35:01 -04:00
|
|
|
PrivilegeLevels = require("../Authorization/PrivilegeLevels")
|
2014-04-08 09:34:03 -04:00
|
|
|
|
2014-06-16 08:34:38 -04:00
|
|
|
module.exports = ProjectController =
|
2014-04-08 09:34:03 -04:00
|
|
|
|
2014-06-25 08:51:02 -04:00
|
|
|
updateProjectSettings: (req, res, next) ->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
|
|
|
|
jobs = []
|
|
|
|
|
|
|
|
if req.body.compiler?
|
|
|
|
jobs.push (callback) ->
|
|
|
|
editorController.setCompiler project_id, req.body.compiler, callback
|
|
|
|
|
|
|
|
if req.body.name?
|
|
|
|
jobs.push (callback) ->
|
|
|
|
editorController.renameProject project_id, req.body.name, callback
|
|
|
|
|
|
|
|
if req.body.spellCheckLanguage?
|
|
|
|
jobs.push (callback) ->
|
|
|
|
editorController.setSpellCheckLanguage project_id, req.body.spellCheckLanguage, callback
|
|
|
|
|
|
|
|
if req.body.rootDocId?
|
|
|
|
jobs.push (callback) ->
|
|
|
|
editorController.setRootDoc project_id, req.body.rootDocId, callback
|
|
|
|
|
2016-03-10 06:13:57 -05:00
|
|
|
async.series jobs, (error) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.sendStatus(204)
|
|
|
|
|
|
|
|
updateProjectAdminSettings: (req, res, next) ->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
|
|
|
|
jobs = []
|
2014-06-25 08:51:02 -04:00
|
|
|
if req.body.publicAccessLevel?
|
|
|
|
jobs.push (callback) ->
|
|
|
|
editorController.setPublicAccessLevel project_id, req.body.publicAccessLevel, callback
|
|
|
|
|
|
|
|
async.series jobs, (error) ->
|
|
|
|
return next(error) if error?
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus(204)
|
2014-06-25 08:51:02 -04:00
|
|
|
|
2014-06-03 12:35:44 -04:00
|
|
|
deleteProject: (req, res) ->
|
2014-04-08 09:34:03 -04:00
|
|
|
project_id = req.params.Project_id
|
2014-06-04 07:45:04 -04:00
|
|
|
forever = req.query?.forever?
|
2016-03-08 09:38:23 -05:00
|
|
|
logger.log project_id: project_id, forever: forever, "received request to archive project"
|
2014-06-04 07:45:04 -04:00
|
|
|
|
|
|
|
if forever
|
|
|
|
doDelete = projectDeleter.deleteProject
|
|
|
|
else
|
|
|
|
doDelete = projectDeleter.archiveProject
|
|
|
|
|
|
|
|
doDelete project_id, (err)->
|
2014-06-03 12:35:44 -04:00
|
|
|
if err?
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 500
|
2014-06-03 12:35:44 -04:00
|
|
|
else
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 200
|
2014-06-03 12:35:44 -04:00
|
|
|
|
|
|
|
restoreProject: (req, res) ->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
logger.log project_id:project_id, "received request to restore project"
|
|
|
|
projectDeleter.restoreProject project_id, (err)->
|
2014-04-08 09:34:03 -04:00
|
|
|
if err?
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 500
|
2014-04-08 09:34:03 -04:00
|
|
|
else
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 200
|
2014-04-08 09:53:33 -04:00
|
|
|
|
2014-11-10 06:23:07 -05:00
|
|
|
cloneProject: (req, res, next)->
|
2014-04-08 09:53:33 -04:00
|
|
|
metrics.inc "cloned-project"
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
projectName = req.body.projectName
|
|
|
|
logger.log project_id:project_id, projectName:projectName, "cloning project"
|
|
|
|
if !req.session.user?
|
|
|
|
return res.send redir:"/register"
|
|
|
|
projectDuplicator.duplicate req.session.user, project_id, projectName, (err, project)->
|
|
|
|
if err?
|
|
|
|
logger.error err:err, project_id: project_id, user_id: req.session.user._id, "error cloning project"
|
|
|
|
return next(err)
|
2014-04-08 10:25:22 -04:00
|
|
|
res.send(project_id:project._id)
|
|
|
|
|
|
|
|
|
|
|
|
newProject: (req, res)->
|
|
|
|
user = req.session.user
|
2014-07-09 06:05:00 -04:00
|
|
|
projectName = req.body.projectName?.trim()
|
|
|
|
template = req.body.template
|
2014-10-20 09:22:31 -04:00
|
|
|
logger.log user: user, projectType: template, name: projectName, "creating project"
|
2014-04-08 10:28:18 -04:00
|
|
|
async.waterfall [
|
|
|
|
(cb)->
|
|
|
|
if template == 'example'
|
|
|
|
projectCreationHandler.createExampleProject user._id, projectName, cb
|
2014-04-08 10:25:22 -04:00
|
|
|
else
|
2014-04-08 10:28:18 -04:00
|
|
|
projectCreationHandler.createBasicProject user._id, projectName, cb
|
|
|
|
], (err, project)->
|
|
|
|
if err?
|
2014-10-20 09:22:31 -04:00
|
|
|
logger.error err: err, project: project, user: user, name: projectName, templateType: template, "error creating project"
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 500
|
2014-04-08 10:28:18 -04:00
|
|
|
else
|
2014-10-20 09:22:31 -04:00
|
|
|
logger.log project: project, user: user, name: projectName, templateType: template, "created project"
|
2014-04-08 11:40:12 -04:00
|
|
|
res.send {project_id:project._id}
|
|
|
|
|
|
|
|
|
2014-04-28 12:47:47 -04:00
|
|
|
renameProject: (req, res)->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
newName = req.body.newProjectName
|
2014-10-07 06:14:02 -04:00
|
|
|
if newName.length > 150
|
2015-07-08 11:56:38 -04:00
|
|
|
return res.sendStatus 400
|
2014-04-28 12:47:47 -04:00
|
|
|
editorController.renameProject project_id, newName, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, newName:newName, "problem renaming project"
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 500
|
2014-04-28 12:47:47 -04:00
|
|
|
else
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 200
|
2014-04-28 12:47:47 -04:00
|
|
|
|
2014-04-08 11:40:12 -04:00
|
|
|
projectListPage: (req, res, next)->
|
|
|
|
timer = new metrics.Timer("project-list")
|
|
|
|
user_id = req.session.user._id
|
2014-04-08 11:56:34 -04:00
|
|
|
async.parallel {
|
|
|
|
tags: (cb)->
|
|
|
|
TagsHandler.getAllTags user_id, cb
|
2016-01-21 15:42:50 -05:00
|
|
|
notifications: (cb)->
|
|
|
|
NotificationsHandler.getUserNotifications user_id, cb
|
2014-04-08 11:56:34 -04:00
|
|
|
projects: (cb)->
|
2016-03-03 12:19:03 -05:00
|
|
|
ProjectGetter.findAllUsersProjects user_id, 'name lastUpdated publicAccesLevel archived owner_ref', cb
|
2014-08-07 10:29:06 -04:00
|
|
|
hasSubscription: (cb)->
|
|
|
|
LimitationsManager.userHasSubscriptionOrIsGroupMember req.session.user, cb
|
2014-10-07 08:31:13 -04:00
|
|
|
user: (cb) ->
|
|
|
|
User.findById user_id, "featureSwitches", cb
|
2014-04-08 11:56:34 -04:00
|
|
|
}, (err, results)->
|
2014-04-08 16:06:01 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, "error getting data for project list page"
|
2014-06-16 08:34:38 -04:00
|
|
|
return next(err)
|
2014-04-08 11:56:34 -04:00
|
|
|
logger.log results:results, user_id:user_id, "rendering project list"
|
2014-06-16 08:34:38 -04:00
|
|
|
tags = results.tags[0]
|
2016-01-22 15:08:39 -05:00
|
|
|
notifications = require("underscore").map results.notifications, (notification)->
|
|
|
|
notification.html = req.i18n.translate(notification.templateKey, notification.messageOpts)
|
|
|
|
return notification
|
2014-06-16 08:34:38 -04:00
|
|
|
projects = ProjectController._buildProjectList results.projects[0], results.projects[1], results.projects[2]
|
2014-10-07 08:31:13 -04:00
|
|
|
user = results.user
|
2014-06-16 08:34:38 -04:00
|
|
|
ProjectController._injectProjectOwners projects, (error, projects) ->
|
|
|
|
return next(error) if error?
|
2014-06-17 11:19:40 -04:00
|
|
|
|
|
|
|
viewModel = {
|
2014-08-21 08:02:28 -04:00
|
|
|
title:'your_projects'
|
2014-06-16 08:34:38 -04:00
|
|
|
priority_title: true
|
|
|
|
projects: projects
|
|
|
|
tags: tags
|
2016-02-11 07:10:30 -05:00
|
|
|
notifications: notifications or []
|
2014-10-07 08:31:13 -04:00
|
|
|
user: user
|
2014-10-27 11:00:46 -04:00
|
|
|
hasSubscription: results.hasSubscription[0]
|
2014-06-16 08:34:38 -04:00
|
|
|
}
|
2014-06-17 11:19:40 -04:00
|
|
|
|
2014-07-10 11:05:18 -04:00
|
|
|
if Settings?.algolia?.app_id? and Settings?.algolia?.read_only_api_key?
|
2014-06-17 11:19:40 -04:00
|
|
|
viewModel.showUserDetailsArea = true
|
2014-07-16 11:37:58 -04:00
|
|
|
viewModel.algolia_api_key = Settings.algolia.read_only_api_key
|
2014-07-10 11:05:18 -04:00
|
|
|
viewModel.algolia_app_id = Settings.algolia.app_id
|
2014-06-17 11:19:40 -04:00
|
|
|
else
|
|
|
|
viewModel.showUserDetailsArea = false
|
|
|
|
|
|
|
|
res.render 'project/list', viewModel
|
2014-06-16 08:34:38 -04:00
|
|
|
timer.done()
|
2014-04-08 11:56:34 -04:00
|
|
|
|
|
|
|
|
2014-04-10 12:49:39 -04:00
|
|
|
loadEditor: (req, res, next)->
|
2014-04-08 12:44:31 -04:00
|
|
|
timer = new metrics.Timer("load-editor")
|
|
|
|
if !Settings.editorIsOpen
|
2014-08-01 08:47:14 -04:00
|
|
|
return res.render("general/closed", {title:"updating_site"})
|
2014-04-08 12:44:31 -04:00
|
|
|
|
2014-04-10 12:49:39 -04:00
|
|
|
if req.session.user?
|
2016-01-14 09:53:08 -05:00
|
|
|
user_id = req.session.user._id
|
2014-04-10 12:49:39 -04:00
|
|
|
anonymous = false
|
|
|
|
else
|
|
|
|
anonymous = true
|
2016-03-22 05:39:25 -04:00
|
|
|
user_id = null
|
2016-01-14 09:53:08 -05:00
|
|
|
|
2014-04-10 12:49:39 -04:00
|
|
|
project_id = req.params.Project_id
|
2015-07-21 20:06:23 -04:00
|
|
|
logger.log project_id:project_id, "loading editor"
|
2016-01-14 09:53:08 -05:00
|
|
|
|
2014-04-08 12:59:29 -04:00
|
|
|
async.parallel {
|
|
|
|
project: (cb)->
|
2016-03-15 10:12:43 -04:00
|
|
|
ProjectGetter.getProject project_id, { name: 1, lastUpdated: 1}, cb
|
2014-04-08 12:59:29 -04:00
|
|
|
user: (cb)->
|
2016-03-22 05:39:25 -04:00
|
|
|
if !user_id?
|
2014-04-10 12:49:39 -04:00
|
|
|
cb null, defaultSettingsForAnonymousUser(user_id)
|
|
|
|
else
|
2015-07-22 05:38:28 -04:00
|
|
|
User.findById user_id, (err, user)->
|
|
|
|
logger.log project_id:project_id, user_id:user_id, "got user"
|
|
|
|
cb err, user
|
2014-04-08 12:59:29 -04:00
|
|
|
subscription: (cb)->
|
2016-03-22 05:39:25 -04:00
|
|
|
if !user_id?
|
2014-04-10 12:49:39 -04:00
|
|
|
return cb()
|
2014-04-08 12:59:29 -04:00
|
|
|
SubscriptionLocator.getUsersSubscription user_id, cb
|
2015-08-13 17:40:28 -04:00
|
|
|
activate: (cb)->
|
|
|
|
InactiveProjectManager.reactivateProjectIfRequired project_id, cb
|
2016-01-14 09:53:08 -05:00
|
|
|
markAsOpened: (cb)->
|
2015-08-14 06:27:11 -04:00
|
|
|
#don't need to wait for this to complete
|
|
|
|
ProjectUpdateHandler.markAsOpened project_id, ->
|
|
|
|
cb()
|
2014-04-08 12:59:29 -04:00
|
|
|
}, (err, results)->
|
|
|
|
if err?
|
2014-04-10 12:49:39 -04:00
|
|
|
logger.err err:err, "error getting details for project page"
|
|
|
|
return next err
|
2014-04-08 12:59:29 -04:00
|
|
|
project = results.project
|
|
|
|
user = results.user
|
|
|
|
subscription = results.subscription
|
2014-04-08 16:06:01 -04:00
|
|
|
|
2016-01-14 09:53:08 -05:00
|
|
|
|
2015-07-22 05:38:48 -04:00
|
|
|
daysSinceLastUpdated = (new Date() - project.lastUpdated) /86400000
|
|
|
|
logger.log project_id:project_id, daysSinceLastUpdated:daysSinceLastUpdated, "got db results for loading editor"
|
|
|
|
|
2016-03-14 13:06:57 -04:00
|
|
|
AuthorizationManager.getPrivilegeLevelForProject user_id, project_id, (error, privilegeLevel)->
|
2016-03-10 12:17:26 -05:00
|
|
|
return next(error) if error?
|
2016-03-15 10:35:01 -04:00
|
|
|
if !privilegeLevel? or privilegeLevel == PrivilegeLevels.NONE
|
2015-07-08 11:56:38 -04:00
|
|
|
return res.sendStatus 401
|
2014-04-08 12:59:29 -04:00
|
|
|
|
|
|
|
if subscription? and subscription.freeTrial? and subscription.freeTrial.expiresAt?
|
|
|
|
allowedFreeTrial = !!subscription.freeTrial.allowed || true
|
2016-01-14 09:53:08 -05:00
|
|
|
|
2015-07-21 20:06:23 -04:00
|
|
|
logger.log project_id:project_id, "rendering editor page"
|
2014-04-08 12:59:29 -04:00
|
|
|
res.render 'project/editor',
|
|
|
|
title: project.name
|
|
|
|
priority_title: true
|
|
|
|
bodyClasses: ["editor"]
|
2014-06-21 17:20:37 -04:00
|
|
|
project_id : project._id
|
2014-07-09 06:05:00 -04:00
|
|
|
user : {
|
2016-03-31 06:53:59 -04:00
|
|
|
id : user_id
|
2014-04-08 12:59:29 -04:00
|
|
|
email : user.email
|
|
|
|
first_name : user.first_name
|
|
|
|
last_name : user.last_name
|
|
|
|
referal_id : user.referal_id
|
|
|
|
subscription :
|
|
|
|
freeTrial: {allowed: allowedFreeTrial}
|
2014-10-07 08:31:13 -04:00
|
|
|
featureSwitches: user.featureSwitches
|
2016-03-31 12:10:49 -04:00
|
|
|
features: user.features
|
2014-07-09 06:05:00 -04:00
|
|
|
}
|
|
|
|
userSettings: {
|
2014-04-08 12:59:29 -04:00
|
|
|
mode : user.ace.mode
|
|
|
|
theme : user.ace.theme
|
|
|
|
fontSize : user.ace.fontSize
|
|
|
|
autoComplete: user.ace.autoComplete
|
|
|
|
pdfViewer : user.ace.pdfViewer
|
2014-07-09 06:05:00 -04:00
|
|
|
}
|
2014-04-08 12:59:29 -04:00
|
|
|
privilegeLevel: privilegeLevel
|
|
|
|
chatUrl: Settings.apis.chat.url
|
|
|
|
anonymous: anonymous
|
|
|
|
languages: Settings.languages
|
2014-06-24 15:28:53 -04:00
|
|
|
themes: THEME_LIST
|
2015-11-06 07:51:43 -05:00
|
|
|
maxDocLength: Settings.max_doc_length
|
|
|
|
timer.done()
|
2014-04-08 12:59:29 -04:00
|
|
|
|
2014-06-16 08:34:38 -04:00
|
|
|
_buildProjectList: (ownedProjects, sharedProjects, readOnlyProjects)->
|
|
|
|
projects = []
|
|
|
|
for project in ownedProjects
|
|
|
|
projects.push ProjectController._buildProjectViewModel(project, "owner")
|
|
|
|
for project in sharedProjects
|
|
|
|
projects.push ProjectController._buildProjectViewModel(project, "readWrite")
|
|
|
|
for project in readOnlyProjects
|
|
|
|
projects.push ProjectController._buildProjectViewModel(project, "readOnly")
|
|
|
|
|
|
|
|
return projects
|
|
|
|
|
|
|
|
_buildProjectViewModel: (project, accessLevel) ->
|
|
|
|
{
|
|
|
|
id: project._id
|
|
|
|
name: project.name
|
|
|
|
lastUpdated: project.lastUpdated
|
|
|
|
publicAccessLevel: project.publicAccesLevel
|
|
|
|
accessLevel: accessLevel
|
|
|
|
archived: !!project.archived
|
|
|
|
owner_ref: project.owner_ref
|
|
|
|
}
|
|
|
|
|
|
|
|
_injectProjectOwners: (projects, callback = (error, projects) ->) ->
|
|
|
|
users = {}
|
|
|
|
for project in projects
|
|
|
|
if project.owner_ref?
|
|
|
|
users[project.owner_ref.toString()] = true
|
|
|
|
|
|
|
|
jobs = []
|
|
|
|
for user_id, _ of users
|
|
|
|
do (user_id) ->
|
|
|
|
jobs.push (callback) ->
|
|
|
|
User.findById user_id, "first_name last_name", (error, user) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
users[user_id] = user
|
|
|
|
callback()
|
|
|
|
|
|
|
|
async.series jobs, (error) ->
|
|
|
|
for project in projects
|
|
|
|
if project.owner_ref?
|
|
|
|
project.owner = users[project.owner_ref.toString()]
|
|
|
|
callback null, projects
|
|
|
|
|
2014-04-10 12:49:39 -04:00
|
|
|
defaultSettingsForAnonymousUser = (user_id)->
|
2014-04-08 12:59:29 -04:00
|
|
|
id : user_id
|
|
|
|
ace:
|
|
|
|
mode:'none'
|
|
|
|
theme:'textmate'
|
|
|
|
fontSize: '12'
|
|
|
|
autoComplete: true
|
|
|
|
spellCheckLanguage: ""
|
|
|
|
pdfViewer: ""
|
|
|
|
subscription:
|
|
|
|
freeTrial:
|
|
|
|
allowed: true
|
|
|
|
featureSwitches:
|
2014-10-07 08:31:13 -04:00
|
|
|
github: false
|
2014-04-08 12:44:31 -04:00
|
|
|
|
2014-06-24 15:28:53 -04:00
|
|
|
THEME_LIST = []
|
|
|
|
do generateThemeList = () ->
|
2014-07-09 12:59:04 -04:00
|
|
|
files = fs.readdirSync __dirname + '/../../../../public/js/ace'
|
2014-06-24 15:28:53 -04:00
|
|
|
for file in files
|
2014-07-09 12:59:04 -04:00
|
|
|
if file.slice(-2) == "js" and file.match(/^theme-/)
|
|
|
|
cleanName = file.slice(0,-3).slice(6)
|
2016-01-22 00:41:22 -05:00
|
|
|
THEME_LIST.push cleanName
|