3 var _
= require('lodash');
4 var Promise
= require('bluebird');
5 var appEvents
= require('gitter-web-appevents');
6 var restSerializer
= require('../../serializers/rest-serializer');
7 var roomMembershipService
= require('gitter-web-rooms/lib/room-membership-service');
8 var userTypeaheadElastic
= require('../typeaheads/user-typeahead-elastic');
10 function getRoomDistribution(userId
) {
11 return roomMembershipService
.findRoomIdsForUser(userId
);
14 function serializeUserToRooms(troupeIds
, operation
, user
) {
15 if (!troupeIds
.length
) return Promise
.resolve();
16 var strategy
= new restSerializer
.UserStrategy();
18 return restSerializer
.serializeObject(user
, strategy
).then(function(serializedUser
) {
19 troupeIds
.forEach(function(troupeId
) {
20 var url
= '/rooms/' + troupeId
+ '/users';
21 appEvents
.dataChange2(url
, 'update', serializedUser
, 'user');
27 create: function(user
) {
29 userTypeaheadElastic
.upsertUser(user
),
30 getRoomDistribution(user
._id
).then(function(troupeIds
) {
31 return serializeUserToRooms(troupeIds
, 'create', user
);
37 update: function(user
) {
39 userTypeaheadElastic
.upsertUser(user
),
40 getRoomDistribution(user
._id
).then(function(troupeIds
) {
41 return serializeUserToRooms(troupeIds
, 'update', user
);
47 patch: function(userId
, patch
) {
48 return getRoomDistribution(userId
).then(function(troupeIds
) {
49 if (!troupeIds
.length
) return;
51 var patchMessage
= _
.extend({}, patch
, { id
: userId
});
53 troupeIds
.forEach(function(troupeId
) {
54 var url
= '/rooms/' + troupeId
+ '/users';
55 appEvents
.dataChange2(url
, 'patch', patchMessage
, 'user');
60 remove: function(/*userId*/) {
61 // Not yet implemented
62 return Promise
.resolve();
65 removeId: function(/*userId*/) {
66 // Not yet implemented
67 return Promise
.resolve();