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
) {
16 if (recipientUserId
) {
17 return getOtherUserId(t
);
19 // Return all the userIds if one was not specified
20 return t
.oneToOneUsers
.map(function(oneToOneUser
) {
21 return oneToOneUser
.userId
;
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
);
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
;
74 this.map = function(item
) {
77 var otherUserId
= getOtherUserId(item
);
78 user
= otherUserId
&& userStategy
.map(otherUserId
);
82 id
: item
.id
|| item
._id
,
83 name
: item
.oneToOne
? user
&& user
.displayName
: item
.name
,
85 oneToOne
: item
.oneToOne
,
87 url
: item
.oneToOne
? user
&& user
&& getHomeUrl(user
) : '/' + item
.uri
,
88 urlUserMap
: item
.oneToOne
&& getUrlUserMap(item
),
89 nameUserMap
: item
.oneToOne
&& getNameUserMap(item
)
96 TroupeStrategy
.prototype = {
97 name
: 'TroupeStrategy'
100 module
.exports
= TroupeStrategy
;