Files: abb85fa0947760cd74602bc9f246265ae015e3e1 / sbot / channels.js
1308 bytesRaw
1 | var FlumeReduce = require('flumeview-reduce') |
2 | var normalizeChannel = require('../lib/normalize-channel') |
3 | |
4 | module.exports = function (ssb, config) { |
5 | return ssb._flumeUse('patchwork-channels', FlumeReduce(1, reduce, map)) |
6 | } |
7 | |
8 | function isActivityMessage(msg) { |
9 | var isVote = msg.value.content.type === "vote"; |
10 | |
11 | return !isVote && msg.value.content.subscribed !== false |
12 | && msg.value.content.subscribed !== true; |
13 | } |
14 | |
15 | function reduce (result, item) { |
16 | if (!result) result = {} |
17 | if (item) { |
18 | for (var channel in item) { |
19 | var value = result[channel] |
20 | if (!value) { |
21 | value = result[channel] = {count: 0, timestamp: 0} |
22 | } |
23 | value.count += 1 |
24 | |
25 | // We don't update the timestamp if the messsage was just somebody subscribing |
26 | // or unsubscribing from the channel, or it is a vote as we don't want it to register as |
27 | // 'recent activity'. |
28 | if (item[channel].isActivityMessage && item[channel].timestamp > value.timestamp) { |
29 | value.timestamp = item[channel].timestamp |
30 | } |
31 | } |
32 | } |
33 | return result |
34 | } |
35 | |
36 | function map (msg) { |
37 | if (msg.value.content) { |
38 | var channel = normalizeChannel(msg.value.content.channel) |
39 | if (channel) { |
40 | return { |
41 | [channel]: {timestamp: msg.timestamp, isActivityMessage: isActivityMessage(msg) } |
42 | } |
43 | } |
44 | } |
45 | } |
46 |
Built with git-ssb-web