2014-02-12 05:23:40 -05:00
|
|
|
mongojs = require("../../infrastructure/mongojs")
|
2017-04-03 11:18:30 -04:00
|
|
|
metrics = require("metrics-sharelatex")
|
2014-02-12 05:23:40 -05:00
|
|
|
db = mongojs.db
|
|
|
|
ObjectId = mongojs.ObjectId
|
|
|
|
async = require "async"
|
2016-03-03 12:19:03 -05:00
|
|
|
Project = require("../../models/Project").Project
|
2016-02-29 14:01:46 -05:00
|
|
|
logger = require("logger-sharelatex")
|
2018-02-01 10:31:42 -05:00
|
|
|
LockManager = require("../../infrastructure/LockManager")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = ProjectGetter =
|
|
|
|
EXCLUDE_DEPTH: 8
|
|
|
|
|
|
|
|
getProjectWithoutDocLines: (project_id, callback=(error, project) ->) ->
|
|
|
|
excludes = {}
|
2016-03-09 06:16:27 -05:00
|
|
|
for i in [1..ProjectGetter.EXCLUDE_DEPTH]
|
2018-01-23 06:46:09 -05:00
|
|
|
excludes["rootFolder#{Array(i).join(".folders")}.docs.lines"] = 0
|
2018-02-15 08:16:24 -05:00
|
|
|
ProjectGetter.getProject project_id, excludes, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2015-10-21 11:56:06 -04:00
|
|
|
getProjectWithOnlyFolders: (project_id, callback=(error, project) ->) ->
|
|
|
|
excludes = {}
|
2016-03-09 06:16:27 -05:00
|
|
|
for i in [1..ProjectGetter.EXCLUDE_DEPTH]
|
2018-01-23 06:46:09 -05:00
|
|
|
excludes["rootFolder#{Array(i).join(".folders")}.docs"] = 0
|
|
|
|
excludes["rootFolder#{Array(i).join(".folders")}.fileRefs"] = 0
|
2018-02-15 08:16:24 -05:00
|
|
|
ProjectGetter.getProject project_id, excludes, callback
|
2016-03-09 06:16:27 -05:00
|
|
|
|
2018-02-15 10:20:14 -05:00
|
|
|
getProject: (project_id, projection, callback) ->
|
2018-02-15 08:16:50 -05:00
|
|
|
if !project_id?
|
2018-02-15 10:20:14 -05:00
|
|
|
return callback(new Error("no project_id provided"))
|
2018-02-15 08:16:50 -05:00
|
|
|
|
2018-02-15 10:20:14 -05:00
|
|
|
if typeof(projection) == "function" && !callback?
|
|
|
|
callback = projection
|
|
|
|
projection = {}
|
|
|
|
|
|
|
|
if typeof(projection) != "object"
|
|
|
|
return callback(new Error("projection is not an object"))
|
|
|
|
|
|
|
|
if projection?.rootFolder || Object.keys(projection).length == 0
|
2018-02-15 08:30:25 -05:00
|
|
|
ProjectEntityMongoUpdateHandler = require './ProjectEntityMongoUpdateHandler'
|
2018-02-19 06:23:43 -05:00
|
|
|
LockManager.runWithLock ProjectEntityMongoUpdateHandler.LOCK_NAMESPACE, project_id,
|
2018-02-01 10:31:42 -05:00
|
|
|
(cb) -> ProjectGetter.getProjectWithoutLock project_id, projection, cb
|
|
|
|
callback
|
|
|
|
else
|
|
|
|
ProjectGetter.getProjectWithoutLock project_id, projection, callback
|
|
|
|
|
2018-02-15 10:20:14 -05:00
|
|
|
getProjectWithoutLock: (project_id, projection, callback) ->
|
|
|
|
if !project_id?
|
|
|
|
return callback(new Error("no project_id provided"))
|
|
|
|
|
|
|
|
if typeof(projection) == "function" && !callback?
|
2016-02-29 14:01:46 -05:00
|
|
|
callback = projection
|
2018-02-15 10:20:14 -05:00
|
|
|
projection = {}
|
|
|
|
|
|
|
|
if typeof(projection) != "object"
|
|
|
|
return callback(new Error("projection is not an object"))
|
2016-02-29 14:01:46 -05:00
|
|
|
|
2018-02-15 08:16:50 -05:00
|
|
|
if typeof project_id == "string"
|
|
|
|
query = _id: ObjectId(project_id)
|
|
|
|
else if project_id instanceof ObjectId
|
|
|
|
query = _id: project_id
|
|
|
|
else if project_id?.toString().length == 24 # sometimes mongoose ids are hard to identify, this will catch them
|
|
|
|
query = _id: ObjectId(project_id.toString())
|
2016-03-09 06:16:27 -05:00
|
|
|
else
|
|
|
|
err = new Error("malformed get request")
|
2018-02-15 08:16:50 -05:00
|
|
|
logger.log project_id:project_id, err:err, type:typeof(project_id), "malformed get request"
|
2016-03-09 06:16:27 -05:00
|
|
|
return callback(err)
|
2016-02-29 14:01:46 -05:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
db.projects.find query, projection, (err, project) ->
|
2016-03-09 06:16:27 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, query:query, projection:projection, "error getting project"
|
2016-02-29 14:01:46 -05:00
|
|
|
return callback(err)
|
2016-03-09 06:16:27 -05:00
|
|
|
callback(null, project?[0])
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-12-18 09:56:40 -05:00
|
|
|
getProjectIdByReadAndWriteToken: (token, callback=(err, project_id)->) ->
|
|
|
|
Project.findOne {'tokens.readAndWrite': token}, {_id: 1}, (err, project) ->
|
|
|
|
return callback err if err?
|
|
|
|
return callback() unless project?
|
|
|
|
callback null, project._id
|
|
|
|
|
2017-10-20 06:49:20 -04:00
|
|
|
findAllUsersProjects: (
|
|
|
|
user_id,
|
|
|
|
fields,
|
|
|
|
callback = (error, projects={owned: [], readAndWrite: [], readOnly: [], tokenReadAndWrite: [], tokenReadOnly: []}) ->
|
|
|
|
) ->
|
2016-03-07 07:02:48 -05:00
|
|
|
CollaboratorsHandler = require "../Collaborators/CollaboratorsHandler"
|
2017-09-20 10:26:03 -04:00
|
|
|
Project.find {owner_ref: user_id}, fields, (error, ownedProjects) ->
|
2016-03-03 12:19:03 -05:00
|
|
|
return callback(error) if error?
|
2017-09-20 10:26:03 -04:00
|
|
|
CollaboratorsHandler.getProjectsUserIsMemberOf user_id, fields, (error, projects) ->
|
2016-03-03 12:19:03 -05:00
|
|
|
return callback(error) if error?
|
2017-10-20 06:49:20 -04:00
|
|
|
result = {
|
|
|
|
owned: ownedProjects || [],
|
|
|
|
readAndWrite: projects.readAndWrite || [],
|
|
|
|
readOnly: projects.readOnly || [],
|
|
|
|
tokenReadAndWrite: projects.tokenReadAndWrite || [],
|
|
|
|
tokenReadOnly: projects.tokenReadOnly || []
|
|
|
|
}
|
|
|
|
callback null, result
|
2017-03-16 06:59:18 -04:00
|
|
|
|
2017-03-17 10:55:41 -04:00
|
|
|
[
|
|
|
|
'getProject',
|
|
|
|
'getProjectWithoutDocLines'
|
|
|
|
].map (method) ->
|
|
|
|
metrics.timeAsyncMethod(ProjectGetter, method, 'mongo.ProjectGetter', logger)
|