Files: 5a9e9eddf4bd43670d794f0cdf0c6c53b0fb3f5d / sbot / channels.js
827 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 reduce (result, item) { |
9 | if (!result) result = {} |
10 | if (item) { |
11 | for (var channel in item) { |
12 | var value = result[channel] |
13 | if (!value) { |
14 | value = result[channel] = {count: 0, timestamp: 0} |
15 | } |
16 | value.count += 1 |
17 | if (item[channel].timestamp > value.timestamp) { |
18 | value.timestamp = item[channel].timestamp |
19 | } |
20 | } |
21 | } |
22 | return result |
23 | } |
24 | |
25 | function map (msg) { |
26 | if (msg.value.content) { |
27 | var channel = normalizeChannel(msg.value.content.channel) |
28 | if (channel) { |
29 | return { |
30 | [channel]: {timestamp: msg.timestamp} |
31 | } |
32 | } |
33 | } |
34 | } |
35 |
Built with git-ssb-web