2014-02-12 05:23:40 -05:00
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
async = require("async")
|
2017-04-03 11:18:30 -04:00
|
|
|
metrics = require('metrics-sharelatex')
|
2014-02-12 05:23:40 -05:00
|
|
|
Settings = require('settings-sharelatex')
|
2017-10-24 06:48:21 -04:00
|
|
|
ObjectId = require('mongoose').Types.ObjectId
|
2014-02-12 05:23:40 -05:00
|
|
|
Project = require('../../models/Project').Project
|
|
|
|
Folder = require('../../models/Folder').Folder
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler = require('./ProjectEntityUpdateHandler')
|
2017-05-19 11:21:02 -04:00
|
|
|
ProjectDetailsHandler = require('./ProjectDetailsHandler')
|
2017-12-18 11:39:27 -05:00
|
|
|
HistoryManager = require('../History/HistoryManager')
|
2014-02-12 05:23:40 -05:00
|
|
|
User = require('../../models/User').User
|
|
|
|
fs = require('fs')
|
|
|
|
Path = require "path"
|
|
|
|
_ = require "underscore"
|
2018-02-22 16:22:55 -05:00
|
|
|
AnalyticsManger = require("../Analytics/AnalyticsManager")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2017-03-17 10:55:41 -04:00
|
|
|
module.exports = ProjectCreationHandler =
|
|
|
|
|
2017-10-27 10:24:18 -04:00
|
|
|
createBlankProject : (owner_id, projectName, projectHistoryId, callback = (error, project) ->)->
|
2014-02-12 05:23:40 -05:00
|
|
|
metrics.inc("project-creation")
|
2017-10-27 09:56:16 -04:00
|
|
|
if arguments.length == 3
|
2017-10-27 10:24:18 -04:00
|
|
|
callback = projectHistoryId
|
|
|
|
projectHistoryId = null
|
2017-10-27 09:56:16 -04:00
|
|
|
|
2017-05-19 11:21:02 -04:00
|
|
|
ProjectDetailsHandler.validateProjectName projectName, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
logger.log owner_id:owner_id, projectName:projectName, "creating blank project"
|
2017-10-27 10:24:18 -04:00
|
|
|
if projectHistoryId?
|
2018-03-06 18:26:25 -05:00
|
|
|
ProjectCreationHandler._createBlankProject owner_id, projectName, projectHistoryId, (error, project) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
AnalyticsManger.recordEvent(
|
|
|
|
owner_id, 'project-imported', { projectId: project._id, projectHistoryId: projectHistoryId }
|
|
|
|
)
|
|
|
|
callback(error, project)
|
2017-10-27 09:56:16 -04:00
|
|
|
else
|
2017-12-18 11:39:27 -05:00
|
|
|
HistoryManager.initializeProject (error, history) ->
|
2017-10-27 09:56:16 -04:00
|
|
|
return callback(error) if error?
|
2018-03-06 18:26:25 -05:00
|
|
|
ProjectCreationHandler._createBlankProject owner_id, projectName, history?.overleaf_id, (error, project) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
AnalyticsManger.recordEvent(
|
|
|
|
owner_id, 'project-created', { projectId: project._id }
|
|
|
|
)
|
|
|
|
callback(error, project)
|
2017-10-27 09:56:16 -04:00
|
|
|
|
2017-10-27 10:24:18 -04:00
|
|
|
_createBlankProject : (owner_id, projectName, projectHistoryId, callback = (error, project) ->)->
|
2017-10-27 09:56:16 -04:00
|
|
|
rootFolder = new Folder {'name':'rootFolder'}
|
|
|
|
project = new Project
|
|
|
|
owner_ref : new ObjectId(owner_id)
|
|
|
|
name : projectName
|
2017-10-27 10:24:18 -04:00
|
|
|
project.overleaf.history.id = projectHistoryId
|
2018-01-03 11:29:28 -05:00
|
|
|
if Settings.apis?.project_history?.displayHistoryForNewProjects
|
|
|
|
project.overleaf.history.display = true
|
2017-10-27 09:56:16 -04:00
|
|
|
if Settings.currentImageName?
|
|
|
|
project.imageName = Settings.currentImageName
|
|
|
|
project.rootFolder[0] = rootFolder
|
|
|
|
User.findById owner_id, "ace.spellCheckLanguage", (err, user)->
|
|
|
|
project.spellCheckLanguage = user.ace.spellCheckLanguage
|
|
|
|
project.save (err)->
|
|
|
|
return callback(err) if err?
|
|
|
|
callback err, project
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
createBasicProject : (owner_id, projectName, callback = (error, project) ->)->
|
|
|
|
self = @
|
|
|
|
@createBlankProject owner_id, projectName, (error, project)->
|
|
|
|
return callback(error) if error?
|
|
|
|
self._buildTemplate "mainbasic.tex", owner_id, projectName, (error, docLines)->
|
|
|
|
return callback(error) if error?
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.addDoc project._id, project.rootFolder[0]._id, "main.tex", docLines, owner_id, (error, doc)->
|
2016-02-29 08:05:17 -05:00
|
|
|
if error?
|
2016-02-29 12:34:38 -05:00
|
|
|
logger.err err:error, "error adding doc when creating basic project"
|
2016-02-29 08:05:17 -05:00
|
|
|
return callback(error)
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.setRootDoc project._id, doc._id, (error) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
callback(error, project)
|
|
|
|
|
|
|
|
createExampleProject: (owner_id, projectName, callback = (error, project) ->)->
|
|
|
|
self = @
|
|
|
|
@createBlankProject owner_id, projectName, (error, project)->
|
|
|
|
return callback(error) if error?
|
|
|
|
async.series [
|
|
|
|
(callback) ->
|
|
|
|
self._buildTemplate "main.tex", owner_id, projectName, (error, docLines)->
|
|
|
|
return callback(error) if error?
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.addDoc project._id, project.rootFolder[0]._id, "main.tex", docLines, owner_id, (error, doc)->
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.setRootDoc project._id, doc._id, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
(callback) ->
|
|
|
|
self._buildTemplate "references.bib", owner_id, projectName, (error, docLines)->
|
|
|
|
return callback(error) if error?
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.addDoc project._id, project.rootFolder[0]._id, "references.bib", docLines, owner_id, (error, doc)->
|
2014-02-12 05:23:40 -05:00
|
|
|
callback(error)
|
|
|
|
(callback) ->
|
|
|
|
universePath = Path.resolve(__dirname + "/../../../templates/project_files/universe.jpg")
|
2018-02-14 10:12:46 -05:00
|
|
|
ProjectEntityUpdateHandler.addFile project._id, project.rootFolder[0]._id, "universe.jpg", universePath, null, owner_id, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
], (error) ->
|
|
|
|
callback(error, project)
|
|
|
|
|
|
|
|
_buildTemplate: (template_name, user_id, project_name, callback = (error, output) ->)->
|
|
|
|
User.findById user_id, "first_name last_name", (error, user)->
|
|
|
|
return callback(error) if error?
|
|
|
|
monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
|
|
|
|
|
|
|
|
templatePath = Path.resolve(__dirname + "/../../../templates/project_files/#{template_name}")
|
|
|
|
fs.readFile templatePath, (error, template) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
data =
|
|
|
|
project_name: project_name
|
|
|
|
user: user
|
|
|
|
year: new Date().getUTCFullYear()
|
|
|
|
month: monthNames[new Date().getUTCMonth()]
|
|
|
|
output = _.template(template.toString(), data)
|
|
|
|
callback null, output.split("\n")
|
|
|
|
|
2017-03-17 10:55:41 -04:00
|
|
|
metrics.timeAsyncMethod(
|
|
|
|
ProjectCreationHandler, 'createBlankProject',
|
|
|
|
'mongo.ProjectCreationHandler',
|
|
|
|
logger
|
|
|
|
)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
|