Files: 322cf9527ab94f96536f92a63ad982f647e1f6b9 / sbot / channels.js
1709 bytesRaw
1 | var FlumeReduce = require('flumeview-reduce') |
2 | var normalizeChannel = require('ssb-ref').normalizeChannel |
3 | |
4 | module.exports = function (ssb, config) { |
5 | return ssb._flumeUse('patchwork-channels', FlumeReduce(2, 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 | var timestamp = Math.min(msg.value.timestamp, msg.timestamp) |
37 | result[channel] = {timestamp} |
38 | return result |
39 | }, {}) |
40 | } |
41 | } |
42 | } |
43 | } |
44 | |
45 | function getChannels (msg) { |
46 | var result = [] |
47 | if (msg.value && msg.value.content) { |
48 | var channel = normalizeChannel(msg.value.content.channel) |
49 | if (channel) { |
50 | result.push(channel) |
51 | } |
52 | if (Array.isArray(msg.value.content.mentions)) { |
53 | msg.value.content.mentions.forEach(mention => { |
54 | if (typeof mention.link === 'string' && mention.link.startsWith('#')) { |
55 | var tag = normalizeChannel(mention.link.slice(1)) |
56 | if (tag) { |
57 | result.push(tag) |
58 | } |
59 | } |
60 | }) |
61 | } |
62 | } |
63 | return result |
64 | } |
65 |
Built with git-ssb-web