2014-02-12 05:23:40 -05:00
|
|
|
mongojs = require("../../infrastructure/mongojs")
|
2017-03-16 06:59:18 -04:00
|
|
|
metrics = require("../../infrastructure/Metrics")
|
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")
|
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]
|
2014-02-12 05:23:40 -05:00
|
|
|
excludes["rootFolder#{Array(i).join(".folder")}.docs.lines"] = 0
|
2014-05-06 08:34:37 -04:00
|
|
|
db.projects.find _id: ObjectId(project_id.toString()), excludes, (error, projects = []) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
callback error, projects[0]
|
|
|
|
|
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]
|
2015-10-21 11:56:06 -04:00
|
|
|
excludes["rootFolder#{Array(i).join(".folder")}.docs"] = 0
|
|
|
|
excludes["rootFolder#{Array(i).join(".folder")}.fileRefs"] = 0
|
|
|
|
db.projects.find _id: ObjectId(project_id.toString()), excludes, (error, projects = []) ->
|
|
|
|
callback error, projects[0]
|
|
|
|
|
2016-03-09 06:16:27 -05:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
getProject: (query, projection, callback = (error, project) ->) ->
|
2016-03-09 06:16:27 -05:00
|
|
|
if !query?
|
|
|
|
return callback("no query provided")
|
2016-02-29 14:01:46 -05:00
|
|
|
|
|
|
|
if typeof(projection) == "function"
|
|
|
|
callback = projection
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
if typeof query == "string"
|
|
|
|
query = _id: ObjectId(query)
|
|
|
|
else if query instanceof ObjectId
|
|
|
|
query = _id: query
|
2016-03-09 06:16:27 -05:00
|
|
|
else if query?.toString().length == 24 # sometimes mongoose ids are hard to identify, this will catch them
|
|
|
|
query = _id: ObjectId(query.toString())
|
|
|
|
else
|
|
|
|
err = new Error("malformed get request")
|
|
|
|
logger.log query:query, err:err, type:typeof(query), "malformed get request"
|
|
|
|
return callback(err)
|
2016-02-29 14:01:46 -05:00
|
|
|
|
2016-03-09 06:16:27 -05:00
|
|
|
db.projects.find query, projection, (err, project)->
|
|
|
|
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
|
|
|
|
2016-03-03 12:19:03 -05:00
|
|
|
|
|
|
|
findAllUsersProjects: (user_id, fields, callback = (error, ownedProjects, readAndWriteProjects, readOnlyProjects) ->) ->
|
2016-03-07 07:02:48 -05:00
|
|
|
CollaboratorsHandler = require "../Collaborators/CollaboratorsHandler"
|
2016-03-03 12:19:03 -05:00
|
|
|
Project.find {owner_ref: user_id}, fields, (error, projects) ->
|
|
|
|
return callback(error) if error?
|
2016-03-08 06:54:45 -05:00
|
|
|
CollaboratorsHandler.getProjectsUserIsCollaboratorOf user_id, fields, (error, readAndWriteProjects, readOnlyProjects) ->
|
2016-03-03 12:19:03 -05:00
|
|
|
return callback(error) if error?
|
|
|
|
callback null, projects, readAndWriteProjects, readOnlyProjects
|
2017-03-16 06:59:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
metrics.timeAsyncMethod ProjectGetter, 'getProject', 'ProjectGetter.getProject', logger
|