3 var Promise
= require('bluebird');
4 var appEvents
= require('gitter-web-appevents');
5 var restSerializer
= require('../../serializers/rest-serializer');
6 var roomMembershipService
= require('gitter-web-rooms/lib/room-membership-service');
7 var presenceService
= require('gitter-web-presence');
8 var _
= require('lodash');
10 function getUserDistribution(troupeId
) {
11 return roomMembershipService
.findMembersForRoom(troupeId
).then(function(userIds
) {
12 if (!userIds
.length
) return [];
14 return presenceService
.categorizeUsersByOnlineStatus(userIds
).then(function(online
) {
15 return userIds
.filter(function(userId
) {
16 return !!online
[userId
];
22 function serializeRoomToUsers(userIds
, operation
, troupe
) {
23 if (!userIds
.length
) return Promise
.resolve();
25 if (troupe
.oneToOne
) {
26 // Because the troupe needs customized per-user....
27 return serializeOneToOneRoomToUsers(userIds
, operation
, troupe
);
30 var strategy
= new restSerializer
.TroupeStrategy();
31 return restSerializer
.serializeObject(troupe
, strategy
).then(function(serializedTroupe
) {
32 appEvents
.dataChange2('/rooms/' + troupe
._id
, operation
, serializedTroupe
, 'room');
34 userIds
.forEach(function(userId
) {
35 appEvents
.dataChange2('/user/' + userId
+ '/rooms', operation
, serializedTroupe
, 'room');
40 /* Note: oneToOnes do not serialize to /rooms/:roomId since each user gets a different representation */
41 function serializeOneToOneRoomToUsers(userIds
, operation
, troupe
) {
42 /* Perform the serialization for each user */
44 return Promise
.map(userIds
, function(userId
) {
45 var strategy
= new restSerializer
.TroupeStrategy({ currentUserId
: userId
});
47 return restSerializer
.serializeObject(troupe
, strategy
).then(function(serializedTroupe
) {
48 appEvents
.dataChange2('/user/' + userId
+ '/rooms', operation
, serializedTroupe
, 'room');
54 create: function(troupe
, initialUserIds
) {
55 /* initialUserIds is used when the room has just been created */
56 /* and we know who the users in the room are */
58 return serializeRoomToUsers(initialUserIds
, 'create', troupe
);
61 return getUserDistribution(troupe
._id
).then(function(userIds
) {
62 return serializeRoomToUsers(userIds
, 'create', troupe
);
66 update: function(troupe
) {
67 return getUserDistribution(troupe
._id
).then(function(userIds
) {
68 return serializeRoomToUsers(userIds
, 'update', troupe
);
72 patch: function(troupeId
, patch
) {
73 var patchMessage
= _
.extend({}, patch
, { id
: troupeId
});
74 appEvents
.dataChange2('/rooms/' + troupeId
, 'patch', patchMessage
, 'room');
76 return getUserDistribution(troupeId
).then(function(userIds
) {
77 if (!userIds
.length
) return;
79 userIds
.forEach(function(userId
) {
80 var url
= '/user/' + userId
+ '/rooms';
81 appEvents
.dataChange2(url
, 'patch', patchMessage
, 'room');
86 remove: function(model
) {
87 return this.removeId(model
.id
);
90 removeId: function(troupeId
) {
91 appEvents
.dataChange2('/rooms/' + troupeId
, 'remove', { id
: troupeId
}, 'room');
93 return getUserDistribution(troupeId
).then(function(userIds
) {
94 if (!userIds
.length
) return;
96 userIds
.forEach(function(userId
) {
97 var url
= '/user/' + userId
+ '/rooms';
98 appEvents
.dataChange2(url
, 'remove', { id
: troupeId
}, 'room');