3 var Promise
= require('bluebird');
4 var Troupe
= require('gitter-web-persistence').Troupe
;
5 var assert
= require('assert');
6 var groupRoomFinder
= require('./group-room-finder');
8 function sumUserCountForQuery(query
) {
9 return Troupe
.aggregate([
16 count
: { $sum
: '$userCount' }
27 .then(function(results
) {
28 return (results
&& results
[0] && results
[0].count
) || 0;
32 function findRoomsWithPagination(groupId
, userId
, options
) {
33 assert(groupId
, 'groupId is required');
35 return groupRoomFinder
.queryForAccessibleRooms(groupId
, userId
).then(function(query
) {
46 if (options
&& options
.skip
&& options
.skip
> 0) {
52 var limit
= (options
&& options
.limit
) || 30;
57 if (options
.includeUsers
) {
63 foreignField
: 'troupeId',
73 $slice
: ['$troupeuser', 10]
86 in: '$$troupeuser.userId'
95 Troupe
.aggregate(pipeline
).exec(),
97 sumUserCountForQuery(query
),
98 function(results
, total
, totalUsers
) {
102 totalUsers
: totalUsers
110 findRoomsWithPagination
: findRoomsWithPagination