Files: 58ab0241031aa549a35cce1e678c27065ae66221 / lib / group-summaries.js
1291 bytesRaw
1 | const pull = require('pull-stream') |
2 | const metaSummaryTypes = ['about', 'channel', 'contact'] |
3 | const GroupUntil = require('./pull-group-until') |
4 | |
5 | module.exports = function GroupSummaries ({ windowSize, ungroupFilter, getPriority }) { |
6 | return pull( |
7 | GroupUntil((result, msg) => { |
8 | // stop grouping if more than 3 seconds between messages (waiting too long) |
9 | if (!result.lastTime) { |
10 | result.lastTime = Date.now() |
11 | } |
12 | |
13 | if (result.lastTime && Date.now() - result.lastTime > 1e3) { |
14 | return false |
15 | } |
16 | |
17 | return (result.length < windowSize || metaSummaryTypes.includes(msg.value.content.type)) && result.length < windowSize * 2 |
18 | }), |
19 | pull.map(function (msgs) { |
20 | const result = [] |
21 | const groups = {} |
22 | |
23 | msgs.forEach(msg => { |
24 | const type = (getPriority && getPriority(msg)) ? 'unreadMetaSummary' : 'metaSummary' |
25 | if (metaSummaryTypes.includes(msg.value.content.type) && !msg.totalReplies && (!ungroupFilter || !ungroupFilter(msg))) { |
26 | if (!groups[type]) { |
27 | groups[type] = { group: type, msgs: [] } |
28 | result.push(groups[type]) |
29 | } |
30 | groups[type].msgs.push(msg) |
31 | } else { |
32 | result.push(msg) |
33 | } |
34 | }) |
35 | |
36 | return result |
37 | }), |
38 | pull.flatten() |
39 | ) |
40 | } |
41 |
Built with git-ssb-web