2014-02-12 05:23:40 -05:00
|
|
|
mongojs = require("../../infrastructure/mongojs")
|
2017-04-03 11:18:30 -04:00
|
|
|
metrics = require('metrics-sharelatex')
|
2017-03-16 06:59:18 -04:00
|
|
|
logger = require('logger-sharelatex')
|
2014-02-12 05:23:40 -05:00
|
|
|
db = mongojs.db
|
|
|
|
ObjectId = mongojs.ObjectId
|
|
|
|
|
|
|
|
module.exports = UserGetter =
|
|
|
|
getUser: (query, projection, callback = (error, user) ->) ->
|
|
|
|
if arguments.length == 2
|
|
|
|
callback = projection
|
|
|
|
projection = {}
|
|
|
|
if typeof query == "string"
|
2014-11-09 18:08:23 -05:00
|
|
|
try
|
|
|
|
query = _id: ObjectId(query)
|
|
|
|
catch e
|
|
|
|
return callback(null, null)
|
2014-02-12 05:23:40 -05:00
|
|
|
else if query instanceof ObjectId
|
|
|
|
query = _id: query
|
|
|
|
|
|
|
|
db.users.findOne query, projection, callback
|
2015-10-07 12:32:35 -04:00
|
|
|
|
|
|
|
getUsers: (user_ids, projection, callback = (error, users) ->) ->
|
|
|
|
try
|
|
|
|
user_ids = user_ids.map (u) -> ObjectId(u.toString())
|
|
|
|
catch error
|
|
|
|
return callback error
|
|
|
|
|
2017-03-16 06:59:18 -04:00
|
|
|
db.users.find { _id: { $in: user_ids} }, projection, callback
|
|
|
|
|
2017-08-24 11:48:47 -04:00
|
|
|
getUserOrUserStubById: (user_id, projection, callback = (error, user) ->) ->
|
|
|
|
try
|
|
|
|
query = _id: ObjectId(user_id.toString())
|
|
|
|
catch e
|
|
|
|
return callback(new Error(e))
|
|
|
|
db.users.findOne query, projection, (error, user) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback(null, user) if user?
|
|
|
|
db.userstubs.findOne query, projection, callback
|
2017-03-17 10:55:41 -04:00
|
|
|
|
|
|
|
[
|
|
|
|
'getUser',
|
2017-08-24 11:48:47 -04:00
|
|
|
'getUsers',
|
|
|
|
'getUserOrUserStubById'
|
2017-03-17 10:55:41 -04:00
|
|
|
].map (method) ->
|
|
|
|
metrics.timeAsyncMethod UserGetter, method, 'mongo.UserGetter', logger
|