Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / utils / burst-calculator.js
blob74d823b960dc6eb2883521093653424e26eec06b
1 'use strict';
3 /**
4 * IMPORTANT: this version differs from client-side calculateBursts() due to backbone access methods!
5 * calculateBursts() calculates what chat messages are 'bursts'.
7 * `Array` chats - the collection of chat messages
8 * returns the modified chats array
9 */
10 var calculateBursts = function(chats) {
11 // console.time('calculateBursts'); // benchmarking
12 /* @const - time window, in which an user can keep adding chat items as part of a initial "burst" */
13 var BURST_WINDOW = 5 * 60 * 1000; // 5 minutes
15 var burstUser, burstStart;
17 chats.forEach(function(chat) {
18 if (chat.parentId) return; // ignore thread messages for now
19 var newUser = chat.fromUser && chat.fromUser.username;
20 var newSentTime = chat.sent;
22 // if message is a me status
23 if (chat.status) {
24 burstUser = null;
25 chat.burstStart = true;
26 return;
29 // get the duration since last burst
30 var durationSinceBurstStart = new Date(newSentTime) - new Date(burstStart);
32 // if the current user is different or the duration since last burst is larger than 5 minutes we have a new burst
33 if (newUser !== burstUser || durationSinceBurstStart > BURST_WINDOW) {
34 burstUser = newUser;
35 burstStart = newSentTime;
36 chat.burstStart = true;
37 return;
40 // most messages won't be a burst
41 chat.burstStart = false;
42 });
43 return chats;
44 // console.timeEnd('calculateBursts');
47 module.exports = calculateBursts;