3 var ChatMessage
= require('gitter-web-persistence').ChatMessage
;
4 var _
= require('lodash');
5 var Promise
= require('bluebird');
6 var mongoReadPrefs
= require('gitter-web-persistence-utils/lib/mongo-read-prefs');
8 function getEstimatedMessageCountForRoomId(roomId
) {
9 return ChatMessage
.count({ toTroupeId
: roomId
})
10 .read(mongoReadPrefs
.secondaryPreferred
)
14 function getEstimatedMessageCountForRoomIds(roomIds
) {
15 if (!roomIds
|| !roomIds
.length
) return {};
16 if (roomIds
.length
=== 1) {
17 var singleId
= roomIds
[0];
18 return getEstimatedMessageCountForRoomId(singleId
).then(function(count
) {
20 hash
[singleId
] = count
;
25 return ChatMessage
.aggregate([
27 $match
: { toTroupeId
: { $in: roomIds
} }
36 .read(mongoReadPrefs
.secondaryPreferred
)
38 .then(function(results
) {
39 if (!results
|| !results
.length
) return {};
42 function(memo
, result
) {
43 memo
[result
._id
] = result
.count
;
52 getEstimatedMessageCountForRoomId
: Promise
.method(getEstimatedMessageCountForRoomId
),
53 getEstimatedMessageCountForRoomIds
: Promise
.method(getEstimatedMessageCountForRoomIds
)