<commit_before>User = require('../models/User').User
Project = require('../models/Project').Project
sanitize = require('validator').sanitize
path = require "path"
logger = require('logger-sharelatex')
_ = require('underscore')
fs = require('fs')
ProjectHandler = require '../handlers/ProjectHandler'
SecurityManager = require '../managers/SecurityManager'
GuidManager = require '../managers/GuidManager'
Settings = require('settings-sharelatex')
projectCreationHandler = require '../Features/Project/ProjectCreationHandler'
projectDuplicator = require('../Features/Project/ProjectDuplicator')
ProjectZipStreamManager = require '../Features/Downloads/ProjectZipStreamManager'
metrics = require('../infrastructure/Metrics')
TagsHandler = require('../Features/Tags/TagsHandler')
SubscriptionLocator = require("../Features/Subscription/SubscriptionLocator")
SubscriptionFormatters = require("../Features/Subscription/SubscriptionFormatters")
FileStoreHandler = require("../Features/FileStore/FileStoreHandler")

module.exports = class ProjectController
	constructor: (@collaberationManager)->
		ProjectHandler = new ProjectHandler()

	list: (req, res, next)->
		timer = new metrics.Timer("project-list")
		user_id = req.session.user._id
		startTime = new Date()
		User.findById user_id, (error, user) ->
			logger.log user_id: user_id, duration: (new Date() - startTime), "project list timer - User.findById"
			startTime = new Date()
			# TODO: Remove this one month after the ability to start free trials was removed
			SubscriptionLocator.getUsersSubscription user._id, (err, subscription)->
				logger.log user_id: user_id, duration: (new Date() - startTime), "project list timer - Subscription.getUsersSubscription"
				startTime = new Date()
				return next(error) if error?
				# TODO: Remove this one month after the ability to start free trials was removed
				if subscription? and subscription.freeTrial? and subscription.freeTrial.expiresAt?
					freeTrial =
						expired: !!subscription.freeTrial.downgraded
						expiresAt: SubscriptionFormatters.formatDate(subscription.freeTrial.expiresAt)
				TagsHandler.getAllTags user_id, (err, tags, tagsGroupedByProject)->
					logger.log user_id: user_id, duration: (new Date() - startTime), "project list timer - TagsHandler.getAllTags"
					startTime = new Date()
					Project.findAllUsersProjects user_id, 'name lastUpdated publicAccesLevel', (projects, collabertions, readOnlyProjects)->
						logger.log user_id: user_id, duration: (new Date() - startTime), "project list timer - Project.findAllUsersProjects"
						startTime = new Date()
						for project in projects
							project.accessLevel = "owner"
						for project in collabertions
							project.accessLevel = "readWrite"
						for project in readOnlyProjects
							project.accessLevel = "readOnly"
						projects = projects.concat(collabertions).concat(readOnlyProjects)
						projects = projects.map (project)->
							project.tags = tagsGroupedByProject[project._id] || []
							return project
						tags = _.sortBy tags, (tag)->
							-tag.project_ids.length
						logger.log projects:projects, collabertions:collabertions, readOnlyProjects:readOnlyProjects, user_id:user_id, "rendering project list"
						sortedProjects = _.sortBy projects, (project)->
							return - project.lastUpdated
						res.render 'project/list',
							title:'Your Projects'
							priority_title: true
							projects: sortedProjects
							freeTrial: freeTrial
							tags:tags
							projectTabActive: true
						logger.log user_id: user_id, duration: (new Date() - startTime), "project list timer - Finished"
						timer.done()

	apiNewProject: (req, res)->
		user = req.session.user
		projectName = sanitize(req.body.projectName).xss()
		template = sanitize(req.body.template).xss()
		logger.log user: user, type: template, name: projectName, "creating project"
		if template == 'example'
			projectCreationHandler.createExampleProject user._id, projectName, (err, project)->
				if err?
					logger.error err: err, project: project, user: user, name: projectName, type: "example", "error creating project"
					res.send 500
				else
					logger.log project: project, user: user, name: projectName, type: "example", "created project"
					res.send {project_id:project._id}
		else
			projectCreationHandler.createBasicProject user._id, projectName, (err, project)->
				if err?
					logger.error err: err, project: project, user: user, name: projectName, type: "basic", "error creating project"
					res.send 500
				else
					logger.log project: project, user: user, name: projectName, type: "basic", "created project"
					res.send {project_id:project._id}
	
	loadEditor: (req, res)->
		timer = new metrics.Timer("load-editor")
		if !Settings.editorIsOpen
			res.render("general/closed", {title:"updating site"})
		else
			if req.session.user?
				user_id = req.session.user._id
			else
				user_id = 'openUser'
			project_id = req.params.Project_id
			Project.findPopulatedById project_id, (err, project)->
				User.findById user_id, (err, user)->
					if user_id == 'openUser'
						anonymous = true
						user =
							id : user_id
							ace:
								mode:'none'
								theme:'textmate'
								fontSize: '12'
								autoComplete: true
								spellCheckLanguage: ""
								pdfViewer: ""
							subscription:
								freeTrial:
									allowed: true
							featureSwitches:
								dropbox: false
								trackChanges: false
					else
						anonymous = false
					SubscriptionLocator.getUsersSubscription user._id, (err, subscription)->
						SecurityManager.userCanAccessProject user, project, (canAccess, privlageLevel)->
							allowedFreeTrial = true
							if subscription? and subscription.freeTrial? and subscription.freeTrial.expiresAt?
								allowedFreeTrial = !!subscription.freeTrial.allowed
							if canAccess
								timer.done()
								res.render 'project/editor',
									title:  project.name
									priority_title: true
									bodyClasses: ["editor"]
									project : project
									owner : project.owner_ref
									userObject : JSON.stringify({
										id    : user.id
										email : user.email
										first_name : user.first_name
										last_name  : user.last_name
										referal_id : user.referal_id
										subscription :
											freeTrial: {allowed: allowedFreeTrial}
									})<commit_msg>check user isn't null in project controller<commit_after>User = require('../models/User').User
Project = require('../models/Project').Project
sanitize = require('validator').sanitize
path = require "path"
logger = require('logger-sharelatex')
_ = require('underscore')
fs = require('fs')
ProjectHandler = require '../handlers/ProjectHandler'
SecurityManager = require '../managers/SecurityManager'
GuidManager = require '../managers/GuidManager'
Settings = require('settings-sharelatex')
projectCreationHandler = require '../Features/Project/ProjectCreationHandler'
projectDuplicator = require('../Features/Project/ProjectDuplicator')
ProjectZipStreamManager = require '../Features/Downloads/ProjectZipStreamManager'
metrics = require('../infrastructure/Metrics')
TagsHandler = require('../Features/Tags/TagsHandler')
SubscriptionLocator = require("../Features/Subscription/SubscriptionLocator")
SubscriptionFormatters = require("../Features/Subscription/SubscriptionFormatters")
FileStoreHandler = require("../Features/FileStore/FileStoreHandler")

module.exports = class ProjectController
	constructor: (@collaberationManager)->
		ProjectHandler = new ProjectHandler()

	list: (req, res, next)->
		timer = new metrics.Timer("project-list")
		user_id = req.session.user._id
		startTime = new Date()
		User.findById user_id, (error, user) ->
			logger.log user_id: user_id, duration: (new Date() - startTime), "project list timer - User.findById"
			startTime = new Date()
			# TODO: Remove this one month after the ability to start free trials was removed
			SubscriptionLocator.getUsersSubscription user._id, (err, subscription)->
				logger.log user_id: user_id, duration: (new Date() - startTime), "project list timer - Subscription.getUsersSubscription"
				startTime = new Date()
				return next(error) if error?
				# TODO: Remove this one month after the ability to start free trials was removed
				if subscription? and subscription.freeTrial? and subscription.freeTrial.expiresAt?
					freeTrial =
						expired: !!subscription.freeTrial.downgraded
						expiresAt: SubscriptionFormatters.formatDate(subscription.freeTrial.expiresAt)
				TagsHandler.getAllTags user_id, (err, tags, tagsGroupedByProject)->
					logger.log user_id: user_id, duration: (new Date() - startTime), "project list timer - TagsHandler.getAllTags"
					startTime = new Date()
					Project.findAllUsersProjects user_id, 'name lastUpdated publicAccesLevel', (projects, collabertions, readOnlyProjects)->
						logger.log user_id: user_id, duration: (new Date() - startTime), "project list timer - Project.findAllUsersProjects"
						startTime = new Date()
						for project in projects
							project.accessLevel = "owner"
						for project in collabertions
							project.accessLevel = "readWrite"
						for project in readOnlyProjects
							project.accessLevel = "readOnly"
						projects = projects.concat(collabertions).concat(readOnlyProjects)
						projects = projects.map (project)->
							project.tags = tagsGroupedByProject[project._id] || []
							return project
						tags = _.sortBy tags, (tag)->
							-tag.project_ids.length
						logger.log projects:projects, collabertions:collabertions, readOnlyProjects:readOnlyProjects, user_id:user_id, "rendering project list"
						sortedProjects = _.sortBy projects, (project)->
							return - project.lastUpdated
						res.render 'project/list',
							title:'Your Projects'
							priority_title: true
							projects: sortedProjects
							freeTrial: freeTrial
							tags:tags
							projectTabActive: true
						logger.log user_id: user_id, duration: (new Date() - startTime), "project list timer - Finished"
						timer.done()

	apiNewProject: (req, res)->
		user = req.session.user
		projectName = sanitize(req.body.projectName).xss()
		template = sanitize(req.body.template).xss()
		logger.log user: user, type: template, name: projectName, "creating project"
		if template == 'example'
			projectCreationHandler.createExampleProject user._id, projectName, (err, project)->
				if err?
					logger.error err: err, project: project, user: user, name: projectName, type: "example", "error creating project"
					res.send 500
				else
					logger.log project: project, user: user, name: projectName, type: "example", "created project"
					res.send {project_id:project._id}
		else
			projectCreationHandler.createBasicProject user._id, projectName, (err, project)->
				if err?
					logger.error err: err, project: project, user: user, name: projectName, type: "basic", "error creating project"
					res.send 500
				else
					logger.log project: project, user: user, name: projectName, type: "basic", "created project"
					res.send {project_id:project._id}
	
	loadEditor: (req, res)->
		timer = new metrics.Timer("load-editor")
		if !Settings.editorIsOpen
			res.render("general/closed", {title:"updating site"})
		else
			if req.session.user?
				user_id = req.session.user._id
			else
				user_id = 'openUser'
			project_id = req.params.Project_id
			Project.findPopulatedById project_id, (err, project)->
				User.findById user_id, (err, user)->
					if user_id == 'openUser'
						anonymous = true
						user =
							id : user_id
							ace:
								mode:'none'
								theme:'textmate'
								fontSize: '12'
								autoComplete: true
								spellCheckLanguage: ""
								pdfViewer: ""
							subscription:
								freeTrial:
									allowed: true
							featureSwitches:
								dropbox: false
								trackChanges: false
					else
						anonymous = false
					SubscriptionLocator.getUsersSubscription user?._id, (err, subscription)->
						SecurityManager.userCanAccessProject user, project, (canAccess, privlageLevel)->
							allowedFreeTrial = true
							if subscription? and subscription.freeTrial? and subscription.freeTrial.expiresAt?
								allowedFreeTrial = !!subscription.freeTrial.allowed
							if canAccess
								timer.done()
								res.render 'project/editor',
									title:  project.name
									priority_title: true
									bodyClasses: ["editor"]
									project : project
									owner : project.owner_ref
									userObject : JSON.stringify({
										id    : user.id
										email : user.email
										first_name : user.first_name
										last_name  : user.last_name
										referal_id : user.referal_id
										subscription :
											freeTrial: {allowed: allowedFreeTrial}
									})
{'fraction_non_alphanumeric': 0.10693845550764407, 'fraction_numerical': 0.0012544100352802822, 'mean_word_length': 3.458580915763719, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7057753', 'n_tokens_mistral': 4676, 'n_tokens_neox': 3528, 'n_words': 922}