git ssb

1+

Daan Patchwork / patchwork



Tree: 401c797bb92a373ffa4499d47ecea37f75e2a16e

Files: 401c797bb92a373ffa4499d47ecea37f75e2a16e / lib / group-summaries.js

1291 bytesRaw
1const pull = require('pull-stream')
2const metaSummaryTypes = ['about', 'channel', 'contact']
3const GroupUntil = require('./pull-group-until')
4
5module.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