Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / services / live-collection-handlers / live-collection-users.js
blob2bf41335ce4d293c6c6931ee7b18598fbf33f0d1
1 'use strict';
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');
22 });
23 });
26 module.exports = {
27 create: function(user) {
28 return Promise.join(
29 userTypeaheadElastic.upsertUser(user),
30 getRoomDistribution(user._id).then(function(troupeIds) {
31 return serializeUserToRooms(troupeIds, 'create', user);
32 }),
33 function() {}
37 update: function(user) {
38 return Promise.join(
39 userTypeaheadElastic.upsertUser(user),
40 getRoomDistribution(user._id).then(function(troupeIds) {
41 return serializeUserToRooms(troupeIds, 'update', user);
42 }),
43 function() {}
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');
56 });
57 });
60 remove: function(/*userId*/) {
61 // Not yet implemented
62 return Promise.resolve();
65 removeId: function(/*userId*/) {
66 // Not yet implemented
67 return Promise.resolve();