Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / api / v1 / user / aggregated-unread-items.js
blob3f806fbab6276b4dfb3215452fdcc9a64a5014b6
1 'use strict';
3 var unreadItemService = require('gitter-web-unread-items');
4 var collections = require('gitter-web-utils/lib/collections');
5 var restSerializer = require('../../../serializers/rest-serializer');
7 module.exports = {
8   id: 'aggregatedUnreadItem',
9   index: function(req) {
10     var userId = req.resourceUser.id;
12     return unreadItemService.getAllUnreadItemCounts(userId).then(function(counts) {
13       var troupeIds = counts.map(function(c) {
14         return c.troupeId;
15       });
17       var strategy = new restSerializer.TroupeIdStrategy({
18         currentUserId: userId,
19         skipUnreadCounts: true
20       });
22       return restSerializer.serialize(troupeIds, strategy).then(function(troupes) {
23         var troupesIndexed = collections.indexById(troupes);
25         var results = [];
26         counts.forEach(function(count) {
27           var troupe = troupesIndexed[count.troupeId];
28           if (troupe) {
29             results.push({
30               id: troupe.id,
31               uri: troupe.uri,
32               unreadItems: count.unreadItems,
33               mentions: count.mentions
34             });
35           }
36         });
38         return results;
39       });
40     });
41   }