overleaf/services/web/app/coffee/Features/Project/ProjectController.coffee

496 lines
18 KiB
CoffeeScript
Raw Normal View History

2014-04-08 10:28:18 -04:00
async = require("async")
moment = require('moment')
logger = require("logger-sharelatex")
2017-10-25 04:13:17 -04:00
Settings = require('settings-sharelatex')
projectDeleter = require("./ProjectDeleter")
projectDuplicator = require("./ProjectDuplicator")
projectCreationHandler = require("./ProjectCreationHandler")
2014-04-28 12:47:47 -04:00
editorController = require("../Editor/EditorController")
metrics = require('metrics-sharelatex')
User = require('../../models/User').User
TagsHandler = require("../Tags/TagsHandler")
SubscriptionLocator = require("../Subscription/SubscriptionLocator")
2016-01-22 00:41:22 -05:00
NotificationsHandler = require("../Notifications/NotificationsHandler")
LimitationsManager = require("../Subscription/LimitationsManager")
2017-02-14 09:26:36 -05:00
underscore = require("underscore")
Settings = require("settings-sharelatex")
AuthorizationManager = require("../Authorization/AuthorizationManager")
2014-06-24 15:28:53 -04:00
fs = require "fs"
InactiveProjectManager = require("../InactiveData/InactiveProjectManager")
ProjectUpdateHandler = require("./ProjectUpdateHandler")
ProjectGetter = require("./ProjectGetter")
OlProjectGetter = require("./OlProjectGetter")
2016-03-15 10:35:01 -04:00
PrivilegeLevels = require("../Authorization/PrivilegeLevels")
2016-09-05 10:58:31 -04:00
AuthenticationController = require("../Authentication/AuthenticationController")
PackageVersions = require("../../infrastructure/PackageVersions")
AnalyticsManager = require "../Analytics/AnalyticsManager"
2017-10-12 05:57:11 -04:00
Sources = require "../Authorization/Sources"
TokenAccessHandler = require '../TokenAccess/TokenAccessHandler'
2017-10-25 05:34:18 -04:00
CollaboratorsHandler = require '../Collaborators/CollaboratorsHandler'
crypto = require 'crypto'
2014-06-16 08:34:38 -04:00
module.exports = ProjectController =
_isInPercentageRollout: (rolloutName, objectId, percentage) ->
2017-11-10 10:59:11 -05:00
if Settings.bypassPercentageRollouts == true
return true
data = "#{rolloutName}:#{objectId.toString()}"
md5hash = crypto.createHash('md5').update(data).digest('hex')
counter = parseInt(md5hash.slice(26, 32), 16)
return (counter % 100) < percentage
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
async.series jobs, (error) ->
return next(error) if error?
res.sendStatus(204)
2016-09-02 11:17:37 -04:00
updateProjectAdminSettings: (req, res, next) ->
project_id = req.params.Project_id
2016-09-02 11:17:37 -04:00
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
deleteProject: (req, res) ->
project_id = req.params.Project_id
2014-06-04 07:45:04 -04:00
forever = req.query?.forever?
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)->
if err?
2015-07-08 11:56:38 -04:00
res.sendStatus 500
else
2015-07-08 11:56:38 -04:00
res.sendStatus 200
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)->
if err?
2015-07-08 11:56:38 -04:00
res.sendStatus 500
else
2015-07-08 11:56:38 -04:00
res.sendStatus 200
2014-11-10 06:23:07 -05:00
cloneProject: (req, res, next)->
metrics.inc "cloned-project"
project_id = req.params.Project_id
projectName = req.body.projectName
logger.log project_id:project_id, projectName:projectName, "cloning project"
2016-09-23 11:21:07 -04:00
if !AuthenticationController.isUserLoggedIn(req)
return res.send redir:"/register"
currentUser = AuthenticationController.getSessionUser(req)
projectDuplicator.duplicate currentUser, project_id, projectName, (err, project)->
if err?
logger.error err:err, project_id: project_id, user_id: currentUser._id, "error cloning project"
return next(err)
res.send(project_id:project._id)
newProject: (req, res, next)->
2016-09-05 10:58:31 -04:00
user_id = AuthenticationController.getLoggedInUserId(req)
2014-07-09 06:05:00 -04:00
projectName = req.body.projectName?.trim()
template = req.body.template
2016-09-05 10:58:31 -04:00
logger.log user: user_id, projectType: template, name: projectName, "creating project"
2014-04-08 10:28:18 -04:00
async.waterfall [
(cb)->
if template == 'example'
2016-09-05 10:58:31 -04:00
projectCreationHandler.createExampleProject user_id, projectName, cb
else
2016-09-05 10:58:31 -04:00
projectCreationHandler.createBasicProject user_id, projectName, cb
2014-04-08 10:28:18 -04:00
], (err, project)->
return next(err) if err?
logger.log project: project, user: user_id, name: projectName, templateType: template, "created project"
res.send {project_id:project._id}
renameProject: (req, res, next)->
2014-04-28 12:47:47 -04:00
project_id = req.params.Project_id
newName = req.body.newProjectName
editorController.renameProject project_id, newName, (err)->
return next(err) if err?
res.sendStatus 200
2014-04-28 12:47:47 -04:00
projectListPage: (req, res, next)->
timer = new metrics.Timer("project-list")
2016-09-05 10:58:31 -04:00
user_id = AuthenticationController.getLoggedInUserId(req)
currentUser = AuthenticationController.getSessionUser(req)
async.parallel {
tags: (cb)->
TagsHandler.getAllTags user_id, cb
notifications: (cb)->
NotificationsHandler.getUserNotifications user_id, cb
projects: (cb)->
ProjectGetter.findAllUsersProjects user_id, 'name lastUpdated publicAccesLevel archived owner_ref tokens', cb
2017-10-25 04:13:17 -04:00
olProjects: (cb) ->
if Settings.brandPrefix == "ol-"
OlProjectGetter.findAllUsersProjects user_id, cb
else
cb()
hasSubscription: (cb)->
LimitationsManager.userHasSubscriptionOrIsGroupMember currentUser, cb
user: (cb) ->
User.findById user_id, "featureSwitches", cb
}, (err, results)->
if err?
logger.err err:err, "error getting data for project list page"
return next(err)
logger.log results:results, user_id:user_id, "rendering project list"
tags = results.tags[0]
notifications = require("underscore").map results.notifications, (notification)->
notification.html = req.i18n.translate(notification.templateKey, notification.messageOpts)
return notification
projects = ProjectController._buildProjectList results.projects, results.olProjects?.projects
user = results.user
ProjectController._injectProjectOwners projects, (error, projects) ->
return next(error) if error?
viewModel = {
title:'your_projects'
priority_title: true
projects: projects
tags: tags
notifications: notifications or []
user: user
hasSubscription: results.hasSubscription[0]
}
if Settings?.algolia?.app_id? and Settings?.algolia?.read_only_api_key?
viewModel.showUserDetailsArea = true
viewModel.algolia_api_key = Settings.algolia.read_only_api_key
viewModel.algolia_app_id = Settings.algolia.app_id
else
viewModel.showUserDetailsArea = false
res.render 'project/list', viewModel
timer.done()
loadEditor: (req, res, next)->
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"})
2016-09-07 11:40:49 -04:00
if AuthenticationController.isUserLoggedIn(req)
2016-09-05 10:58:31 -04:00
user_id = AuthenticationController.getLoggedInUserId(req)
anonymous = false
else
anonymous = true
user_id = null
project_id = req.params.Project_id
logger.log project_id:project_id, anonymous:anonymous, user_id:user_id, "loading editor"
2014-04-08 12:59:29 -04:00
async.parallel {
project: (cb)->
ProjectGetter.getProject(
project_id,
{ name: 1, lastUpdated: 1, track_changes: 1, owner_ref: 1 },
cb
)
2014-04-08 12:59:29 -04:00
user: (cb)->
if !user_id?
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)->
if !user_id?
return cb()
2014-04-08 12:59:29 -04:00
SubscriptionLocator.getUsersSubscription user_id, cb
activate: (cb)->
InactiveProjectManager.reactivateProjectIfRequired project_id, cb
markAsOpened: (cb)->
#don't need to wait for this to complete
ProjectUpdateHandler.markAsOpened project_id, ->
cb()
showTrackChangesOnboarding: (cb) ->
2017-02-14 09:26:36 -05:00
cb = underscore.once(cb)
if !user_id?
return cb()
timestamp = user_id.toString().substring(0,8)
userSignupDate = new Date( parseInt( timestamp, 16 ) * 1000 )
if userSignupDate > new Date("2017-03-09") # 8th March
# Don't show for users who registered after it was released
return cb(null, false)
timeout = setTimeout cb, 500
AnalyticsManager.getLastOccurance user_id, "shown-track-changes-onboarding-2", (error, event) ->
clearTimeout timeout
if error?
return cb(null, false)
else if event?
return cb(null, false)
else
2017-10-19 11:02:40 -04:00
logger.log { user_id, event }, "track changes onboarding not shown yet to this user"
return cb(null, true)
showPerUserTCNotice: (cb) ->
cb = underscore.once(cb)
if !user_id?
return cb()
timestamp = user_id.toString().substring(0,8)
userSignupDate = new Date( parseInt( timestamp, 16 ) * 1000 )
2017-08-09 08:02:40 -04:00
if userSignupDate > new Date("2017-08-09")
# Don't show for users who registered after it was released
return cb(null, false)
timeout = setTimeout cb, 500
AnalyticsManager.getLastOccurance user_id, "shown-per-user-tc-notice", (error, event) ->
clearTimeout timeout
if error?
return cb(null, false)
else if event?
return cb(null, false)
else
2017-10-19 11:02:40 -04:00
logger.log { user_id, event }, "per user track changes notice not shown yet to this user"
return cb(null, true)
2017-10-25 05:34:18 -04:00
isTokenMember: (cb) ->
cb = underscore.once(cb)
if !user_id?
return cb()
CollaboratorsHandler.userIsTokenMember user_id, project_id, cb
2017-09-26 09:50:45 -04:00
showAutoCompileOnboarding: (cb) ->
cb = underscore.once(cb)
# Force autocompile rollout if query param set
2017-11-16 10:58:32 -05:00
if req.query?.ac == 't'
return cb(null, { enabled: true, showOnboarding: true })
if !user_id?
return cb()
# Extract data from user's ObjectId
timestamp = parseInt(user_id.toString().substring(0, 8), 16)
2017-11-16 05:36:35 -05:00
rolloutPercentage = 5 # Percentage of users to roll out to
if !ProjectController._isInPercentageRollout('autocompile', user_id, rolloutPercentage)
# Don't show if user is not part of roll out
return cb(null, { enabled: false, showOnboarding: false })
userSignupDate = new Date(timestamp * 1000)
2017-10-16 05:28:42 -04:00
if userSignupDate > new Date("2017-10-16")
# Don't show for users who registered after it was released
return cb(null, { enabled: true, showOnboarding: false })
timeout = setTimeout cb, 500
2017-11-08 06:01:36 -05:00
AnalyticsManager.getLastOccurance user_id, "shown-autocompile-onboarding-2", (error, event) ->
clearTimeout timeout
if error?
return cb(null, { enabled: true, showOnboarding: false })
else if event?
return cb(null, { enabled: true, showOnboarding: false })
else
2017-10-19 11:02:40 -04:00
logger.log { user_id, event }, "autocompile onboarding not shown yet to this user"
return cb(null, { enabled: true, showOnboarding: true })
2017-11-09 09:28:11 -05:00
couldShowLinkSharingOnboarding: (cb) ->
cb = underscore.once(cb)
if !user_id?
return cb()
# Extract data from user's ObjectId
timestamp = parseInt(user_id.toString().substring(0, 8), 16)
userSignupDate = new Date(timestamp * 1000)
2017-11-13 05:27:27 -05:00
if userSignupDate > new Date("2017-11-13")
2017-11-09 09:28:11 -05:00
# Don't show for users who registered after it was released
return cb(null, false)
timeout = setTimeout cb, 500
AnalyticsManager.getLastOccurance user_id, "shown-linksharing-onboarding", (error, event) ->
clearTimeout timeout
if error? || event?
return cb(null, false)
else
return cb(null, true)
2014-04-08 12:59:29 -04:00
}, (err, results)->
if err?
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
2017-09-26 09:50:45 -04:00
{ showTrackChangesOnboarding, showPerUserTCNotice, showAutoCompileOnboarding } = results
2017-09-26 09:50:45 -04:00
daysSinceLastUpdated = (new Date() - project.lastUpdated) / 86400000
logger.log project_id:project_id, daysSinceLastUpdated:daysSinceLastUpdated, "got db results for loading editor"
token = TokenAccessHandler.getRequestToken(req, project_id)
2017-10-25 05:34:18 -04:00
isTokenMember = results.isTokenMember
2017-11-09 09:28:11 -05:00
# Roll out token-access based on Project owner
enableTokenAccessUI = ProjectController._isInPercentageRollout(
'linksharing',
project.owner_ref,
2017-11-15 05:25:17 -05:00
40
)
2017-11-09 09:28:11 -05:00
showLinkSharingOnboarding = enableTokenAccessUI && results.couldShowLinkSharingOnboarding
AuthorizationManager.getPrivilegeLevelForProject user_id, project_id, token, (error, privilegeLevel)->
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
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 : {
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
signUpDate : user.signUpDate
2014-04-08 12:59:29 -04:00
subscription :
freeTrial: {allowed: allowedFreeTrial}
featureSwitches: user.featureSwitches
features: user.features
2016-04-01 06:36:19 -04:00
refProviders: user.refProviders
betaProgram: user.betaProgram
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
autoPairDelimiters: user.ace.autoPairDelimiters
2014-04-08 12:59:29 -04:00
pdfViewer : user.ace.pdfViewer
2016-10-06 06:51:24 -04:00
syntaxValidation: user.ace.syntaxValidation
2014-07-09 06:05:00 -04:00
}
trackChangesState: project.track_changes
showTrackChangesOnboarding: !!showTrackChangesOnboarding
showPerUserTCNotice: !!showPerUserTCNotice
autoCompileEnabled: !!showAutoCompileOnboarding?.enabled
showAutoCompileOnboarding: !!showAutoCompileOnboarding?.showOnboarding
2014-04-08 12:59:29 -04:00
privilegeLevel: privilegeLevel
chatUrl: Settings.apis.chat.url
anonymous: anonymous
anonymousAccessToken: req._anonymousAccessToken
2017-10-25 05:34:18 -04:00
isTokenMember: isTokenMember
2014-04-08 12:59:29 -04:00
languages: Settings.languages
2014-06-24 15:28:53 -04:00
themes: THEME_LIST
maxDocLength: Settings.max_doc_length
enableTokenAccessUI: enableTokenAccessUI
2017-11-09 09:28:11 -05:00
showLinkSharingOnboarding: showLinkSharingOnboarding
timer.done()
2014-04-08 12:59:29 -04:00
_buildProjectList: (allProjects, olProjects = [])->
{owned, readAndWrite, readOnly, tokenReadAndWrite, tokenReadOnly} = allProjects
2014-06-16 08:34:38 -04:00
projects = []
for project in owned
2017-10-12 05:57:11 -04:00
projects.push ProjectController._buildProjectViewModel(project, "owner", Sources.OWNER)
# Invite-access
for project in readAndWrite
2017-10-12 05:57:11 -04:00
projects.push ProjectController._buildProjectViewModel(project, "readWrite", Sources.INVITE)
for project in readOnly
2017-10-12 05:57:11 -04:00
projects.push ProjectController._buildProjectViewModel(project, "readOnly", Sources.INVITE)
for project in olProjects
projects.push ProjectController._buildOlProjectViwModel(project)
# Token-access
# Only add these projects if they're not already present, this gives us cascading access
# from 'owner' => 'token-read-only'
for project in tokenReadAndWrite
if projects.filter((p) -> p.id.toString() == project._id.toString()).length == 0
projects.push ProjectController._buildProjectViewModel(project, "readAndWrite", Sources.TOKEN)
for project in tokenReadOnly
if projects.filter((p) -> p.id.toString() == project._id.toString()).length == 0
projects.push ProjectController._buildProjectViewModel(project, "readOnly", Sources.TOKEN)
2014-06-16 08:34:38 -04:00
return projects
2017-10-12 05:57:11 -04:00
_buildProjectViewModel: (project, accessLevel, source) ->
TokenAccessHandler.protectTokens(project, accessLevel)
model = {
2014-06-16 08:34:38 -04:00
id: project._id
name: project.name
lastUpdated: project.lastUpdated
publicAccessLevel: project.publicAccesLevel
accessLevel: accessLevel
2017-10-12 05:57:11 -04:00
source: source
2014-06-16 08:34:38 -04:00
archived: !!project.archived
owner_ref: project.owner_ref
tokens: project.tokens
2014-06-16 08:34:38 -04:00
}
return model
2014-06-16 08:34:38 -04:00
_buildOlProjectViewModel: (project) ->
{
id: project.id
name: project.title
lastUpdated: moment.unix(project.updated_at)
# publicAccessLevel:
# accessLevel:
archived: project.archived
# owner_ref:
isOLProject: true
}
2014-06-16 08:34:38 -04:00
_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
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: ""
2016-10-06 06:51:24 -04:00
syntaxValidation: true
2014-04-08 12:59:29 -04:00
subscription:
freeTrial:
allowed: true
featureSwitches:
github: false
2014-06-24 15:28:53 -04:00
THEME_LIST = []
do generateThemeList = () ->
files = fs.readdirSync __dirname + '/../../../../public/js/' + PackageVersions.lib('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-09-02 11:17:37 -04:00
THEME_LIST.push cleanName