Files: d3414d5beb5d4fc5940f696773b65c3d8b4acb15 / sbot / channels.js
1653 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 | |
18 | if (item[channel].timestamp > value.timestamp) { |
19 | value.timestamp = item[channel].timestamp |
20 | } |
21 | } |
22 | } |
23 | return result |
24 | } |
25 | |
26 | function map (msg) { |
27 | if (msg.value.content) { |
28 | // filter out likes and subscriptions |
29 | var isLike = msg.value.content.type === 'vote' |
30 | var isSubscription = msg.value.content.type === 'channel' |
31 | |
32 | if (!isLike && !isSubscription) { |
33 | var channels = getChannels(msg) |
34 | if (channels.length) { |
35 | return channels.reduce((result, channel) => { |
36 | result[channel] = {timestamp: msg.timestamp} |
37 | return result |
38 | }, {}) |
39 | } |
40 | } |
41 | } |
42 | } |
43 | |
44 | function getChannels (msg) { |
45 | var result = [] |
46 | if (msg.value && msg.value.content) { |
47 | var channel = normalizeChannel(msg.value.content.channel) |
48 | if (channel) { |
49 | result.push(channel) |
50 | } |
51 | if (Array.isArray(msg.value.content.mentions)) { |
52 | msg.value.content.mentions.forEach(mention => { |
53 | if (typeof mention.link === 'string' && mention.link.startsWith('#')) { |
54 | var tag = normalizeChannel(mention.link.slice(1)) |
55 | if (tag) { |
56 | result.push(tag) |
57 | } |
58 | } |
59 | }) |
60 | } |
61 | } |
62 | return result |
63 | } |
64 |
Built with git-ssb-web