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 };
20 linkPath = castString(input.security.linkPath, undefined);
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) {
31 assert(!linkPath, 'linkPath cannot be specified');
38 assert(linkPath, 'linkPath required');
42 // for GitHub and future room types that are backed by other services
43 createOptions.linkPath = linkPath;
45 createOptions.security = 'PUBLIC';
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;
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;
61 createOptions.addBadge = !!input.addBadge;
63 // keep tracking info around for sendStats
64 if (typeof input.source === 'string') {
65 createOptions.tracking = { source: input.source };
74 index: function(req) {
75 var groupId = req.group._id;
77 var userId = user && user._id;
79 return restful.serializeRoomsForGroupId(groupId, userId);
82 create: function(req) {
84 throw new StatusError(401);
87 var createOptions = getCreateOptions(req.body);
89 var groupWithPolicyService = new GroupWithPolicyService(
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
107 return restSerializer.serializeObject(room, strategy).then(function(serialized) {
109 hookCreationFailedDueToMissingScope: hookCreationFailedDueToMissingScope