3 var Promise
= require('bluebird');
4 var appEvents
= require('gitter-web-appevents');
5 var restSerializer
= require('../../serializers/rest-serializer');
6 var troupeService
= require('gitter-web-rooms/lib/troupe-service');
7 var userTypeaheadElastic
= require('../typeaheads/user-typeahead-elastic');
8 var debug
= require('debug')('gitter:app:live-collection-room-members');
10 function notifyGroupRoomOfAddedUsers(room
, userIds
) {
11 /* No point in notifing large rooms */
12 if (room
.userCount
> 100) return Promise
.resolve();
14 debug('Notifying room %s of new users having been added', room
._id
);
17 .serialize(userIds
, new restSerializer
.UserIdStrategy())
18 .then(function(serializedUsers
) {
19 var roomUrl
= '/rooms/' + room
.id
+ '/users';
20 serializedUsers
.forEach(function(serializedUser
) {
21 appEvents
.dataChange2(roomUrl
, 'create', serializedUser
, 'user');
26 function notifyUsersOfAddedGroupRooms(room
, userIds
) {
27 debug('Notifying %s of being added to %s', userIds
, room
._id
);
29 var singleUserId
= userIds
.length
=== 1 && userIds
[0];
31 // TODO: custom serializations per user
35 new restSerializer
.TroupeStrategy({ isRoomMember
: true, currentUserId
: singleUserId
})
37 .then(function(serializedRoom
) {
38 userIds
.forEach(function(userId
) {
39 var userUrl
= '/user/' + userId
+ '/rooms';
40 appEvents
.dataChange2(userUrl
, 'create', serializedRoom
, 'room');
45 function notifyUsersOfAddedOneToOneRooms(room
, userIds
) {
46 return Promise
.map(userIds
, function(userId
) {
47 var strategy
= new restSerializer
.TroupeStrategy({ currentUserId
: userId
, isRoomMember
: true });
49 return restSerializer
.serializeObject(room
, strategy
).then(function(serializedRoom
) {
50 appEvents
.dataChange2('/user/' + userId
+ '/rooms', 'create', serializedRoom
, 'room');
56 added: function(troupeId
, userIds
) {
57 debug('Room %s: %s members added', troupeId
, userIds
.length
);
58 return troupeService
.findById(troupeId
).then(function(room
) {
60 return notifyUsersOfAddedOneToOneRooms(room
, userIds
);
64 notifyGroupRoomOfAddedUsers(room
, userIds
),
65 notifyUsersOfAddedGroupRooms(room
, userIds
),
66 userTypeaheadElastic
.addUsersToGroupRoom(userIds
, troupeId
),
72 removed: function(troupeId
, userIds
) {
73 // FIXME: NOCOMMIT deal with one to ones
74 userIds
.forEach(function(userId
) {
75 /* Dont mark the user as having been removed from the room */
76 appEvents
.dataChange2('/rooms/' + troupeId
+ '/users', 'remove', { id
: userId
}, 'user');
77 appEvents
.dataChange2('/user/' + userId
+ '/rooms', 'remove', { id
: troupeId
}, 'room');
79 appEvents
.userRemovedFromTroupe({ troupeId
: troupeId
, userId
: userId
});
82 return userTypeaheadElastic
.removeUsersFromRoom(userIds
, troupeId
);
85 lurkChange: function(troupeId
, userIds
, lurk
) {
86 userIds
.forEach(function(userId
) {
87 appEvents
.userTroupeLurkModeChange({ userId
: userId
, troupeId
: troupeId
, lurk
: lurk
});
89 // TODO: in future get rid of this but this collection is used by the native clients
90 appEvents
.dataChange2(
91 '/user/' + userId
+ '/rooms',
93 { id
: troupeId
, lurk
: lurk
},
98 return Promise
.resolve();