Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / api / v1 / groups / rooms.js
blob39b8d95e8d6a74af35a8fd83964ac986370ec974
1 'use strict';
3 var assert = require('assert');
4 var StatusError = require('statuserror');
5 var restful = require('../../../services/restful');
6 var GroupWithPolicyService = require('../../../services/group-with-policy-service');
7 var restSerializer = require('../../../serializers/rest-serializer');
9 function castString(v, defaultValue) {
10   return v ? String(v) : defaultValue;
13 function getCreateOptions(input) {
14   var name = castString(input.name);
15   var topic = castString(input.topic);
16   var createOptions = { name: name, topic: topic };
17   var linkPath;
19   if (input.security) {
20     linkPath = castString(input.security.linkPath, undefined);
22     // PUBLIC or PRIVATE
23     createOptions.security = castString(input.security.security, undefined);
24     assert(createOptions.security, 'security required');
26     // `type` defaults to null, not undefined
27     createOptions.type = castString(input.security.type, null);
28     switch (createOptions.type) {
29       case null:
30       case 'GROUP':
31         assert(!linkPath, 'linkPath cannot be specified');
32         break;
34       case 'GH_REPO':
35       case 'GH_ORG':
36       case 'GL_GROUP':
37       case 'GL_PROJECT':
38         assert(linkPath, 'linkPath required');
39         break;
40     }
42     // for GitHub and future room types that are backed by other services
43     createOptions.linkPath = linkPath;
44   } else {
45     createOptions.security = 'PUBLIC';
46   }
48   // input is json, so input.providers should already be an array if it
49   // exists. it gets validated further inside GroupWithPolicyService.
50   if (input.providers && Array.isArray(input.providers)) {
51     createOptions.providers = input.providers;
52   }
54   // If the backing type of the room is a repo,
55   // attempt to associate the room with that repo.
56   // In future, we could do this for any type of room
57   if (createOptions.type === 'GH_REPO') {
58     createOptions.associateWithGitHubRepo = linkPath;
59   }
61   createOptions.addBadge = !!input.addBadge;
63   // keep tracking info around for sendStats
64   if (typeof input.source === 'string') {
65     createOptions.tracking = { source: input.source };
66   }
68   return createOptions;
71 module.exports = {
72   id: 'groupRoom',
74   index: function(req) {
75     var groupId = req.group._id;
76     var user = req.user;
77     var userId = user && user._id;
79     return restful.serializeRoomsForGroupId(groupId, userId);
80   },
82   create: function(req) {
83     if (!req.user) {
84       throw new StatusError(401);
85     }
87     var createOptions = getCreateOptions(req.body);
89     var groupWithPolicyService = new GroupWithPolicyService(
90       req.group,
91       req.user,
92       req.userGroupPolicy
93     );
94     return groupWithPolicyService.createRoom(createOptions).then(function(createResult) {
95       var room = createResult.troupe;
96       var hookCreationFailedDueToMissingScope = createResult.hookCreationFailedDueToMissingScope;
97       var strategy = new restSerializer.TroupeStrategy({
98         currentUserId: req.user.id,
99         currentUser: req.user,
100         includeRolesForTroupe: room,
101         includeBackend: true,
102         // include all these because it will replace the troupe in the context
103         includeTags: true,
104         includeGroups: true
105       });
107       return restSerializer.serializeObject(room, strategy).then(function(serialized) {
108         serialized.extra = {
109           hookCreationFailedDueToMissingScope: hookCreationFailedDueToMissingScope
110         };
111         return serialized;
112       });
113     });
114   }