3 var Promise = require('bluebird');
4 var StatusError = require('statuserror');
5 var groupService = require('gitter-web-groups/lib/group-service');
6 var policyFactory = require('gitter-web-permissions/lib/policy-factory');
7 var groupCreationService = require('../../../services/group-creation-service');
8 var inviteValidation = require('gitter-web-invites/lib/invite-validation');
9 var restful = require('../../../services/restful');
10 var restSerializer = require('../../../serializers/rest-serializer');
11 var internalClientAccessOnly = require('gitter-web-oauth/lib/internal-client-access-only');
13 var MAX_BATCHED_INVITES = 100;
15 function getInvites(invitesInput) {
16 if (!invitesInput || !invitesInput.length) return [];
18 if (invitesInput.length > MAX_BATCHED_INVITES) {
19 throw new StatusError(400, 'Too many batched invites.');
22 // This could throw, but it is the basic user-input validation that would
23 // have failed if the frontend didn't call the invite checker API like it
24 // should have anyway.
25 return invitesInput.map(function(input) {
26 return inviteValidation.parseAndValidateInput(input);
30 function validateStringArray(input, errorMessage) {
31 if (!input) return undefined;
33 if (!Array.isArray(input)) throw new StatusError(400, errorMessage);
35 var valuesAreStrings = input.every(function(s) {
36 return typeof s === 'string';
39 if (valuesAreStrings) {
42 throw new StatusError(400, errorMessage);
46 function getGroupOptions(body) {
47 var uri = body.uri ? String(body.uri) : undefined;
48 var name = body.name ? String(body.name) : undefined;
49 var defaultRoomName = body.defaultRoomName ? String(body.defaultRoomName) : undefined;
50 var providers = validateStringArray(body.providers, 'Providers must be strings.');
51 var invites = getInvites(body.invites);
57 defaultRoomName: defaultRoomName,
59 addBadge: !!body.addBadge
62 allowTweeting: body.allowTweeting
66 // for GitHub and future group types that are backed by other services
67 groupOptions.type = body.security.type ? String(body.security.type) : undefined;
68 groupOptions.linkPath = body.security.linkPath ? String(body.security.linkPath) : undefined;
77 index: function(req) {
79 throw new StatusError(401);
82 var lean = (req.query.lean && parseInt(req.query.lean, 10)) || false;
84 if (req.query.type === 'admin') {
85 return restful.serializeAdminGroupsForUser(req.user, { lean: lean });
88 return restful.serializeGroupsForUserId(req.user._id, { lean: lean });
91 create: function(req) {
94 // This is for internal clients only
95 if (!internalClientAccessOnly.isRequestFromInternalClient(req)) {
96 throw new StatusError(404);
100 throw new StatusError(401);
103 var groupCreationOptions = getGroupOptions(req.body);
105 return groupCreationService(user, groupCreationOptions).then(function(groupCreationResult) {
106 var group = groupCreationResult.group;
107 var defaultRoom = groupCreationResult.defaultRoom;
109 var groupStrategy = new restSerializer.GroupStrategy({
110 currentUserId: req.user.id
112 var troupeStrategy = new restSerializer.TroupeStrategy({
113 currentUserId: req.user.id,
115 includePermissions: true,
120 restSerializer.serializeObject(group, groupStrategy),
121 restSerializer.serializeObject(defaultRoom, troupeStrategy),
122 function(serializedGroup, serializedRoom) {
123 serializedGroup.defaultRoom = serializedRoom;
124 serializedGroup.hookCreationFailedDueToMissingScope =
125 groupCreationResult.hookCreationFailedDueToMissingScope;
126 return serializedGroup;
132 update: function(req) {
133 var group = req.group;
137 // Nothing to update on groups
139 if (!promises.length) {
140 throw new StatusError(400, 'Nothing to update.');
143 return Promise.all(promises).then(function() {
144 var strategy = new restSerializer.GroupStrategy({
145 currentUserId: user && user._id,
148 return restSerializer.serializeObject(group, strategy);
152 show: function(req) {
153 var group = req.group;
155 var userId = user && user._id;
157 var strategy = new restSerializer.GroupStrategy({
158 currentUserId: userId,
161 return restSerializer.serializeObject(group, strategy);
164 load: function(req, id) {
166 .createPolicyForGroupId(req.user, id)
167 .then(function(policy) {
169 req.userGroupPolicy = policy;
171 return req.method === 'GET' ? policy.canRead() : policy.canWrite();
173 .then(function(access) {
174 if (!access) return null;
176 return groupService.findById(id, { lean: true });
181 rooms: require('./rooms'),
182 suggestedRooms: require('./suggested-rooms'),
183 security: require('./security')