git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 01cee878b8ca9ff653f893db11ea3abc0f7eb5f8

Files: 01cee878b8ca9ff653f893db11ea3abc0f7eb5f8 / sbot / channels.js

1653 bytesRaw
1var FlumeReduce = require('flumeview-reduce')
2var normalizeChannel = require('ssb-ref').normalizeChannel
3
4module.exports = function (ssb, config) {
5 return ssb._flumeUse('patchwork-channels', FlumeReduce(2, reduce, map))
6}
7
8function 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
26function 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
44function 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