Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / services / sample-chats-service.js
blob9494408a51aef6be84686992bd157b15ca44e1e7
1 'use strict';
3 var chatService = require('gitter-web-chats');
4 var restSerializer = require('../serializers/rest-serializer');
5 var Promise = require('bluebird');
7 var cachedSamples = null;
8 function getSamples() {
9 if (cachedSamples) return Promise.resolve(cachedSamples);
11 return chatService
12 .getRecentPublicChats()
13 .then(function(chatMessage) {
14 // Remove any duplicate users
15 var users = {};
16 return chatMessage.filter(function(chatMessage) {
17 if (users[chatMessage.fromUserId]) {
18 return false;
20 users[chatMessage.fromUserId] = true;
21 return true;
22 });
24 .then(function(sampleChatMessages) {
25 var sampleChatStrategy = new restSerializer.SampleChatStrategy();
26 var results = restSerializer.serialize(sampleChatMessages, sampleChatStrategy);
28 cachedSamples = results;
30 // Keep them cached for 30 seconds
31 setTimeout(function() {
32 cachedSamples = null;
33 }, 30000);
35 return results;
36 });
39 module.exports = {
40 getSamples: getSamples