Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / serializers / notifications / troupe-strategy.js
blob986be50d9bd0c2be5eac9fa19367f91350bbeb56
1 'use strict';
3 var UserIdStrategy = require('gitter-web-user-serialization/lib/notifications/user-id-strategy');
5 function TroupeStrategy(options) {
6 if (!options) options = {};
8 var userStategy = new UserIdStrategy(options);
10 var recipientUserId = options.recipientUserId;
12 this.preload = function(items) {
13 var userIds = items
14 .map(function(t) {
15 if (t.oneToOne) {
16 if (recipientUserId) {
17 return getOtherUserId(t);
18 } else {
19 // Return all the userIds if one was not specified
20 return t.oneToOneUsers.map(function(oneToOneUser) {
21 return oneToOneUser.userId;
22 });
26 .flatten()
27 .filter(function(f) {
28 return !!f;
29 });
31 if (userIds.isEmpty()) return;
33 return userStategy.preload(userIds);
36 function getOtherUserId(troupe) {
37 if (!recipientUserId || !troupe.oneToOne || !troupe.oneToOneUsers) return undefined;
39 for (var i = 0; i < troupe.oneToOneUsers.length; i++) {
40 var oneToOneUser = troupe.oneToOneUsers[i];
41 if ('' + oneToOneUser.userId !== '' + recipientUserId) {
42 return oneToOneUser.userId;
47 function getHomeUrl(user) {
48 if (!user) return undefined;
49 return '/' + user.username;
52 function getUrlUserMap(troupe) {
53 if (recipientUserId || !troupe.oneToOne) return undefined;
55 return troupe.oneToOneUsers.reduce(function(memo, oneToOneUser) {
56 var userId = oneToOneUser.userId;
57 var user = userStategy.map(userId);
58 memo[userId] = user && getHomeUrl(user);
59 return memo;
60 }, {});
63 function getNameUserMap(troupe) {
64 if (recipientUserId || !troupe.oneToOne) return undefined;
66 return troupe.oneToOneUsers.reduce(function(memo, oneToOneUser) {
67 var userId = oneToOneUser.userId;
68 var user = userStategy.map(userId);
69 memo[userId] = user && user.displayName;
70 return memo;
71 }, {});
74 this.map = function(item) {
75 var user;
76 if (item.oneToOne) {
77 var otherUserId = getOtherUserId(item);
78 user = otherUserId && userStategy.map(otherUserId);
81 var t = {
82 id: item.id || item._id,
83 name: item.oneToOne ? user && user.displayName : item.name,
84 uri: item.uri,
85 oneToOne: item.oneToOne,
86 user: user,
87 url: item.oneToOne ? user && user && getHomeUrl(user) : '/' + item.uri,
88 urlUserMap: item.oneToOne && getUrlUserMap(item),
89 nameUserMap: item.oneToOne && getNameUserMap(item)
92 return t;
96 TroupeStrategy.prototype = {
97 name: 'TroupeStrategy'
100 module.exports = TroupeStrategy;