Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / api / private / org-members.js
blob9da89cdba955327bc4d498abd2ca86848539796f
1 'use strict';
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');
10 /**
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
16 * - Done
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);
25 });
28 /* Only org owners get to call this service */
29 module.exports = function(req, res, next) {
30 var uri = req.params.orgUri;
31 var user = req.user;
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);
40 });
43 return listOrgMembers(req.user, uri);
45 .then(function(orgMembers) {
46 var usernames = orgMembers.map(function(user) {
47 return user.login;
48 });
49 return userService.findByUsernames(usernames);
51 .then(function(users) {
52 return users.filter(function(user) {
53 return !user.state;
54 });
56 .then(function(users) {
57 if (req.query.count_only) return res.send({ count: users.length });
59 /* Return the actual users */
60 return restSerializer
61 .serialize(users, new restSerializer.UserStrategy())
62 .then(function(serializeUsers) {
63 res.send(serializeUsers);
64 });
66 .catch(next);