3 var GitHubMeService
= require('gitter-web-github').GitHubMeService
;
4 var GitHubOrgService
= require('gitter-web-github').GitHubOrgService
;
5 var restSerializer
= require('../../serializers/rest-serializer');
6 var userService
= require('gitter-web-users');
7 var StatusError
= require('statuserror');
8 var Promise
= require('bluebird');
11 * TODO: refactor this https://github.com/troupe/gitter-webapp/issues/1708
13 * Proposed new method:
14 * - Find all non-public rooms for the group
15 * - Count the number of distinct users in the private rooms
18 function listOrgMembers(user
, uri
) {
19 var ghMe
= new GitHubMeService(user
);
20 return ghMe
.isOrgAdmin(uri
).then(function(isAdmin
) {
21 if (!isAdmin
) throw new StatusError(403);
23 var ghOrg
= new GitHubOrgService(user
);
24 return ghOrg
.members(uri
);
28 /* Only org owners get to call this service */
29 module
.exports = function(req
, res
, next
) {
30 var uri
= req
.params
.orgUri
;
33 return Promise
.try(function() {
34 if (!user
) throw new StatusError(401);
36 if (req
.query
.on_behalf_of
) {
37 return userService
.findById(req
.query
.on_behalf_of
).then(function(user
) {
38 if (!user
) throw new StatusError(404);
39 return listOrgMembers(user
, uri
);
43 return listOrgMembers(req
.user
, uri
);
45 .then(function(orgMembers
) {
46 var usernames
= orgMembers
.map(function(user
) {
49 return userService
.findByUsernames(usernames
);
51 .then(function(users
) {
52 return users
.filter(function(user
) {
56 .then(function(users
) {
57 if (req
.query
.count_only
) return res
.send({ count
: users
.length
});
59 /* Return the actual users */
61 .serialize(users
, new restSerializer
.UserStrategy())
62 .then(function(serializeUsers
) {
63 res
.send(serializeUsers
);