Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / services / estimated-chats-service.js
blob6e13291241ac5272b9184d133399ad852b9f244a
1 'use strict';
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)
11 .exec();
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) {
19 var hash = {};
20 hash[singleId] = count;
21 return hash;
22 });
25 return ChatMessage.aggregate([
27 $match: { toTroupeId: { $in: roomIds } }
30 $group: {
31 _id: '$toTroupeId',
32 count: { $sum: 1 }
36 .read(mongoReadPrefs.secondaryPreferred)
37 .exec()
38 .then(function(results) {
39 if (!results || !results.length) return {};
40 return _.reduce(
41 results,
42 function(memo, result) {
43 memo[result._id] = result.count;
44 return memo;
48 });
51 module.exports = {
52 getEstimatedMessageCountForRoomId: Promise.method(getEstimatedMessageCountForRoomId),
53 getEstimatedMessageCountForRoomIds: Promise.method(getEstimatedMessageCountForRoomIds)