lib/feed-summary.jsView |
---|
4 | 4 | var SortedArray = require('sorted-array-functions') |
5 | 5 | |
6 | 6 | module.exports = FeedSummary |
7 | 7 | |
8 | | -function FeedSummary (source, windowSize, cb) { |
| 8 | +function FeedSummary (source, opts, cb) { |
| 9 | + var bumpFilter = opts && opts.bumpFilter |
| 10 | + var windowSize = opts && opts.windowSize || 1000 |
9 | 11 | var last = null |
10 | 12 | var returned = false |
11 | 13 | var done = false |
12 | 14 | return pullNext(() => { |
13 | 15 | if (!done) { |
14 | | - var next = {reverse: true, limit: windowSize || 1000, live: false} |
| 16 | + var next = {reverse: true, limit: windowSize, live: false} |
15 | 17 | if (last) { |
16 | 18 | next.lt = last.timestamp || last.value.sequence |
17 | 19 | } |
18 | 20 | var pushable = pullPushable() |
22 | 24 | if (err) throw err |
23 | 25 | if (!values.length) { |
24 | 26 | done = true |
25 | 27 | } else { |
26 | | - groupMessages(values).forEach(v => pushable.push(v)) |
| 28 | + var fromTime = last && last.timestamp || Date.now() |
| 29 | + groupMessages(values, fromTime, bumpFilter).forEach(v => pushable.push(v)) |
27 | 30 | last = values[values.length - 1] |
28 | 31 | } |
29 | 32 | pushable.end() |
30 | 33 | if (!returned) cb && cb() |
35 | 38 | return pushable |
36 | 39 | }) |
37 | 40 | } |
38 | 41 | |
39 | | -function groupMessages (messages, filter) { |
| 42 | +function groupMessages (messages, fromTime, bumpFilter) { |
40 | 43 | var follows = {} |
41 | 44 | var messageUpdates = {} |
42 | 45 | reverseForEach(messages, function (msg) { |
43 | 46 | if (!msg.value) return |
52 | 55 | if (group) { |
53 | 56 | if (c.vote.value > 0) { |
54 | 57 | group.lastUpdateType = 'dig' |
55 | 58 | group.digs.add(msg.value.author) |
56 | | - group.updated = msg.timestamp |
| 59 | + |
| 60 | + if (!bumpFilter || bumpFilter(msg, group)) { |
| 61 | + group.updated = msg.timestamp |
| 62 | + } |
57 | 63 | } else { |
58 | 64 | group.digs.delete(msg.value.author) |
59 | 65 | if (group.lastUpdateType === 'dig' && !group.digs.size && !group.replies.length) { |
60 | 66 | group.lastUpdateType = 'reply' |
64 | 70 | } |
65 | 71 | } else { |
66 | 72 | if (c.root) { |
67 | 73 | const group = ensureMessage(c.root, messageUpdates) |
| 74 | + group.fromTime = fromTime |
68 | 75 | group.lastUpdateType = 'reply' |
69 | 76 | group.repliesFrom.add(msg.value.author) |
70 | 77 | |
71 | 78 | group.replies.push(msg) |
72 | 79 | group.channel = group.channel || msg.value.content.channel |
73 | | - group.updated = msg.timestamp |
| 80 | + |
| 81 | + |
| 82 | + if (!bumpFilter || bumpFilter(msg, group)) { |
| 83 | + group.updated = msg.timestamp |
| 84 | + } |
74 | 85 | } else { |
75 | 86 | const group = ensureMessage(msg.key, messageUpdates) |
| 87 | + group.fromTime = fromTime |
76 | 88 | group.lastUpdateType = 'post' |
77 | 89 | group.updated = msg.timestamp |
78 | 90 | group.author = msg.value.author |
79 | 91 | group.channel = msg.value.content.channel |
83 | 95 | }) |
84 | 96 | |
85 | 97 | var result = [] |
86 | 98 | Object.keys(follows).forEach((key) => { |
87 | | - SortedArray.add(result, follows[key], compareUpdated) |
| 99 | + if (follows[key].updated) { |
| 100 | + SortedArray.add(result, follows[key], compareUpdated) |
| 101 | + } |
88 | 102 | }) |
89 | 103 | Object.keys(messageUpdates).forEach((key) => { |
90 | | - SortedArray.add(result, messageUpdates[key], compareUpdated) |
| 104 | + if (messageUpdates[key].updated) { |
| 105 | + SortedArray.add(result, messageUpdates[key], compareUpdated) |
| 106 | + } |
91 | 107 | }) |
92 | 108 | return result |
93 | 109 | } |
94 | 110 | |
95 | | -function compareUserTimestamp (a, b) { |
96 | | - return a.value.timestamp - b.value.timestamp |
97 | | -} |
98 | | - |
99 | 111 | function compareUpdated (a, b) { |
100 | 112 | return b.updated - a.updated |
101 | 113 | } |
102 | 114 | |