Files: a0d45464afed54dcb36fdf48278c914c2204a62e / lib / flumeview-channels.js
929 bytesRaw
1 | var FlumeReduce = require('flumeview-reduce') |
2 | |
3 | exports.name = 'channels' |
4 | exports.version = require('../package.json').version |
5 | exports.manifest = { |
6 | stream: 'source', |
7 | get: 'async' |
8 | } |
9 | |
10 | exports.init = function (ssb, config) { |
11 | return ssb._flumeUse('channels', FlumeReduce(1, reduce, map)) |
12 | } |
13 | |
14 | function reduce (result, item) { |
15 | if (!result) result = {} |
16 | if (item) { |
17 | var value = result[item.channel] |
18 | if (!value) { |
19 | value = result[item.channel] = {count: 0, timestamp: 0} |
20 | } |
21 | value.count += 1 |
22 | if (item.timestamp > value.timestamp) { |
23 | value.timestamp = item.timestamp |
24 | } |
25 | } |
26 | return result |
27 | } |
28 | |
29 | function map (msg) { |
30 | if (msg.value.content && typeof msg.value.content.channel === 'string') { |
31 | var channel = msg.value.content.channel |
32 | if (channel.length > 0 && channel.length < 30) { |
33 | return { |
34 | channel: channel.replace(/\s/g, ''), |
35 | timestamp: msg.timestamp |
36 | } |
37 | } |
38 | } |
39 | } |
40 |
Built with git-ssb-web