Files: 1786f9c4c5f7c770e215e697546f92cbd098bc2c / lib / thread-summary.js
1298 bytesRaw
1 | const pull = require('pull-stream') |
2 | const getBump = require('./get-bump') |
3 | |
4 | module.exports = function (dest, { |
5 | recentLimit = 3, |
6 | bumpFilter = null, |
7 | recentFilter = null, |
8 | readThread = null, |
9 | messageFilter = null, |
10 | pullFilter = null |
11 | }, cb) { |
12 | const bumps = [] |
13 | let totalReplies = 0 |
14 | const latestReplies = [] |
15 | return pull( |
16 | readThread({ reverse: true, live: false, dest }), |
17 | pullFilter || pull.through(), |
18 | pull.filter((msg) => msg && (!messageFilter || messageFilter(msg))), |
19 | pull.drain(msg => { |
20 | try { |
21 | // bump filter can return values other than true that will be passed to view |
22 | if (msg && msg.value && msg.value.content) { |
23 | const type = msg.value.content.type |
24 | |
25 | if (type === 'post' || type === 'about') { |
26 | if (latestReplies.length < recentLimit && (!recentFilter || recentFilter(msg))) { |
27 | // collect the most recent bump messages |
28 | latestReplies.unshift(msg) |
29 | } |
30 | totalReplies += 1 |
31 | } |
32 | |
33 | const bump = getBump(msg, bumpFilter) |
34 | if (bump) bumps.push(bump) |
35 | } |
36 | } catch (ex) { |
37 | cb(ex) |
38 | } |
39 | }, (err) => { |
40 | if (err) return cb(err) |
41 | cb(null, { |
42 | bumps, |
43 | totalReplies, |
44 | latestReplies |
45 | }) |
46 | }) |
47 | ) |
48 | } |
49 |
Built with git-ssb-web