3 var Promise
= require('bluebird');
4 var avatars
= require('gitter-web-avatars');
5 var mongoUtils
= require('gitter-web-persistence-utils/lib/mongo-utils');
6 var SecurityDescriptorStrategy
= require('./security-descriptor-strategy');
7 var FavouriteGroupsForUserStrategy
= require('./favourite-groups-for-user-strategy');
9 function GroupStrategy(options
) {
10 this.options
= options
|| {};
12 var securityDescriptorStrategy
;
13 var favouriteStrategy
;
15 this.preload = function() {
16 var options
= this.options
;
17 var currentUserId
= mongoUtils
.asObjectID(options
.currentUserId
);
20 securityDescriptorStrategy
= SecurityDescriptorStrategy
.slim();
23 // Favourites for user
24 favouriteStrategy
= new FavouriteGroupsForUserStrategy({
25 currentUserId
: options
.currentUserId
27 strategies
.push(favouriteStrategy
.preload());
30 return Promise
.all(strategies
);
33 this.map = function(group
) {
34 var options
= this.options
;
35 var id
= group
.id
|| (group
._id
&& group
._id
.toHexString());
37 var hasAvatarSet
= undefined;
38 if (options
.includeHasAvatarSet
) {
40 group
.avatarVersion
> 0 ||
41 group
.sd
.type
=== 'GH_ORG' ||
42 group
.sd
.type
=== 'GH_REPO' ||
43 group
.sd
.type
=== 'GH_USER';
50 favourite
: favouriteStrategy
? favouriteStrategy
.map(id
) : undefined,
51 homeUri
: group
.homeUri
,
52 backedBy
: securityDescriptorStrategy
.map(group
.sd
),
53 avatarUrl
: avatars
.getForGroup(group
),
54 hasAvatarSet
: hasAvatarSet
59 GroupStrategy
.prototype = {
63 module
.exports
= GroupStrategy
;