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
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
25 chat
.burstStart
= true;
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
) {
35 burstStart
= newSentTime
;
36 chat
.burstStart
= true;
40 // most messages won't be a burst
41 chat
.burstStart
= false;
44 // console.timeEnd('calculateBursts');
47 module
.exports
= calculateBursts
;