3 var assert
= require('assert');
4 var StatusError
= require('statuserror');
5 var ensureAccessAndFetchDescriptor
= require('gitter-web-permissions/lib/ensure-access-and-fetch-descriptor');
6 var debug
= require('debug')('gitter:app:group-with-policy-service');
7 var roomService
= require('gitter-web-rooms');
8 var secureMethod
= require('gitter-web-utils/lib/secure-method');
9 var validateRoomName
= require('gitter-web-validators/lib/validate-room-name');
10 var validateProviders
= require('gitter-web-validators/lib/validate-providers');
11 var troupeService
= require('gitter-web-rooms/lib/troupe-service');
12 var groupService
= require('gitter-web-groups/lib/group-service');
17 function validateRoomSecurity(type
, security
) {
18 if (type
=== 'GROUP' && security
=== 'INHERITED') {
22 if (security
=== 'PUBLIC' || security
=== 'PRIVATE') {
29 function allowAdmin() {
30 return this.policy
.canAdmin();
33 function GroupWithPolicyService(group
, user
, policy
) {
34 assert(group
, 'Group required');
35 assert(policy
, 'Policy required');
42 * Allow admins to create a new room
43 * @return {Promise} Promise of room
45 GroupWithPolicyService
.prototype.createRoom
= secureMethod([allowAdmin
], function(options
) {
46 assert(options
, 'options required');
47 var type
= options
.type
|| null;
48 var providers
= options
.providers
|| [];
49 var security
= options
.security
;
50 var name
= options
.name
;
51 var associateWithGitHubRepo
= options
.associateWithGitHubRepo
;
54 var group
= this.group
;
56 if (!validateProviders(providers
)) {
57 throw new StatusError(400, 'Invalid providers ' + providers
);
60 assert(security
, 'security required');
62 if (!validateRoomSecurity(type
, security
)) {
63 throw new StatusError(400, 'Invalid room security for ' + type
+ ': ' + security
);
66 if (!validateRoomName(name
)) {
67 throw new StatusError(400, 'Invalid room name: ' + name
);
70 return this._ensureAccessAndFetchRoomInfo(options
)
71 .spread(function(roomInfo
, securityDescriptor
) {
72 debug('Upserting %j', roomInfo
);
74 return roomService
.createGroupRoom(user
, group
, roomInfo
, securityDescriptor
, {
75 tracking
: options
.tracking
,
76 associateWithGitHubRepo
: associateWithGitHubRepo
,
77 addBadge
: options
.addBadge
80 .then(function(results
) {
82 troupe
: results
.troupe
,
83 hookCreationFailedDueToMissingScope
: results
.hookCreationFailedDueToMissingScope
88 GroupWithPolicyService
.prototype.setAvatar
= secureMethod([allowAdmin
], function(url
) {
89 groupService
.setAvatarForGroup(this.group
._id
, url
);
95 GroupWithPolicyService
.prototype._ensureAccessAndFetchRoomInfo = function(options
) {
97 var group
= this.group
;
98 var type
= options
.type
|| null;
99 var providers
= options
.providers
|| [];
100 var security
= options
.security
;
101 var topic
= options
.topic
;
102 var name
= options
.name
;
103 var linkPath
= options
.linkPath
;
106 // This is probably not needed...
107 var obtainAccessFromGitHubRepo
= options
.obtainAccessFromGitHubRepo
;
109 var uri
= group
.uri
+ '/' + name
;
111 if (!validateProviders(providers
)) {
112 throw new StatusError(400, 'Invalid providers ' + providers
.toString());
115 assert(security
, 'security required');
117 if (!validateRoomSecurity(type
, security
)) {
118 throw new StatusError(400, 'Invalid room security for ' + type
+ ': ' + security
);
121 if (!validateRoomName(name
)) {
122 throw new StatusError(400, 'Invalid room name: ' + name
);
125 if (type
=== 'GROUP') {
126 internalId
= group
._id
;
129 return troupeService
.findByUri(uri
).then(function(room
) {
131 throw new StatusError(409, 'Room uri already taken: ' + uri
);
134 return ensureAccessAndFetchDescriptor(user
, {
137 obtainAccessFromGitHubRepo
: obtainAccessFromGitHubRepo
,
138 internalId
: internalId
,
140 }).then(function(securityDescriptor
) {
153 GroupWithPolicyService
.prototype.setAvatar
= secureMethod([allowAdmin
], function(url
) {
154 return groupService
.setAvatarForGroup(this.group
._id
, url
);
157 module
.exports
= GroupWithPolicyService
;