Merge pull request #583 from sharelatex/ja-import-projects

Add in UserStub model and support in collaborators view
This commit is contained in:
James Allen 2017-08-29 16:06:00 +02:00 committed by GitHub
commit 9c4dc40abf
5 changed files with 37 additions and 9 deletions

View file

@ -30,13 +30,14 @@ module.exports = CollaboratorsHandler =
return callback(error) if error? return callback(error) if error?
return callback null, members.map (m) -> m.id return callback null, members.map (m) -> m.id
USER_PROJECTION: { _id: 1, email: 1 }
getMembersWithPrivilegeLevels: (project_id, callback = (error, members) ->) -> getMembersWithPrivilegeLevels: (project_id, callback = (error, members) ->) ->
CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members = []) -> CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members = []) ->
return callback(error) if error? return callback(error) if error?
result = [] result = []
async.mapLimit members, 3, async.mapLimit members, 3,
(member, cb) -> (member, cb) ->
UserGetter.getUser member.id, (error, user) -> UserGetter.getUserOrUserStubById member.id, CollaboratorsHandler.USER_PROJECTION, (error, user) ->
return cb(error) if error? return cb(error) if error?
if user? if user?
result.push { user: user, privilegeLevel: member.privilegeLevel } result.push { user: user, privilegeLevel: member.privilegeLevel }

View file

@ -27,9 +27,19 @@ module.exports = UserGetter =
db.users.find { _id: { $in: user_ids} }, projection, callback db.users.find { _id: { $in: user_ids} }, projection, callback
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
[ [
'getUser', 'getUser',
'getUsers' 'getUsers',
'getUserOrUserStubById'
].map (method) -> ].map (method) ->
metrics.timeAsyncMethod UserGetter, method, 'mongo.UserGetter', logger metrics.timeAsyncMethod UserGetter, method, 'mongo.UserGetter', logger

View file

@ -1,6 +1,6 @@
Settings = require "settings-sharelatex" Settings = require "settings-sharelatex"
mongojs = require "mongojs" mongojs = require "mongojs"
db = mongojs(Settings.mongo.url, ["projects", "users"]) db = mongojs(Settings.mongo.url, ["projects", "users", "userstubs"])
module.exports = module.exports =
db: db db: db
ObjectId: mongojs.ObjectId ObjectId: mongojs.ObjectId

View file

@ -0,0 +1,17 @@
Settings = require "settings-sharelatex"
mongoose = require('mongoose')
Schema = mongoose.Schema
ObjectId = Schema.ObjectId
UserStubSchema = new Schema
email : { type : String, default : '' }
first_name : { type : String, default : '' }
last_name : { type : String, default : '' }
overleaf : { id: { type: Number } }
conn = mongoose.createConnection(Settings.mongo.url, server: poolSize: 10)
UserStub = conn.model('UserStub', UserStubSchema)
model = mongoose.model 'UserStub', UserStubSchema
exports.UserStub = UserStub

View file

@ -80,12 +80,12 @@ describe "CollaboratorsHandler", ->
{ id: "read-write-ref-2", privilegeLevel: "readAndWrite" } { id: "read-write-ref-2", privilegeLevel: "readAndWrite" }
{ id: "doesnt-exist", privilegeLevel: "readAndWrite" } { id: "doesnt-exist", privilegeLevel: "readAndWrite" }
]) ])
@UserGetter.getUser = sinon.stub() @UserGetter.getUserOrUserStubById = sinon.stub()
@UserGetter.getUser.withArgs("read-only-ref-1").yields(null, { _id: "read-only-ref-1" }) @UserGetter.getUserOrUserStubById.withArgs("read-only-ref-1").yields(null, { _id: "read-only-ref-1" })
@UserGetter.getUser.withArgs("read-only-ref-2").yields(null, { _id: "read-only-ref-2" }) @UserGetter.getUserOrUserStubById.withArgs("read-only-ref-2").yields(null, { _id: "read-only-ref-2" })
@UserGetter.getUser.withArgs("read-write-ref-1").yields(null, { _id: "read-write-ref-1" }) @UserGetter.getUserOrUserStubById.withArgs("read-write-ref-1").yields(null, { _id: "read-write-ref-1" })
@UserGetter.getUser.withArgs("read-write-ref-2").yields(null, { _id: "read-write-ref-2" }) @UserGetter.getUserOrUserStubById.withArgs("read-write-ref-2").yields(null, { _id: "read-write-ref-2" })
@UserGetter.getUser.withArgs("doesnt-exist").yields(null, null) @UserGetter.getUserOrUserStubById.withArgs("doesnt-exist").yields(null, null)
@CollaboratorHandler.getMembersWithPrivilegeLevels @project_id, @callback @CollaboratorHandler.getMembersWithPrivilegeLevels @project_id, @callback
it "should return an array of members with their privilege levels", -> it "should return an array of members with their privilege levels", ->