Files: a683541f27c2757c560090b0fa816b37d947d42f / intermediate / channelV2.js
971 bytesRaw
1 | const flumeView = require('flumeview-reduce') |
2 | |
3 | const NAME = 'channel' |
4 | const VERSION = 2 |
5 | |
6 | module.exports = { |
7 | name: NAME, |
8 | version: VERSION, |
9 | manifest: { |
10 | all: 'async' |
11 | }, |
12 | init: function (server, config) { |
13 | const view = server._flumeUse( |
14 | NAME, |
15 | flumeView(VERSION, reduce, map, null, initialState()) |
16 | ) |
17 | |
18 | return { |
19 | all: view.get |
20 | } |
21 | } |
22 | } |
23 | |
24 | function map (msg) { |
25 | var content = msg.value.content |
26 | var channelAttribute = content.channel |
27 | |
28 | var mentions = content.mentions || [] |
29 | var mentionedChannel = mentions |
30 | .filter(mention => Boolean(mention.link)) |
31 | .filter(mention => mention.link.match(/^#/)) |
32 | .map(mention => mention.link.replace('#','')) |
33 | .shift() |
34 | |
35 | return channelAttribute || mentionedChannel |
36 | } |
37 | |
38 | // if map returns null or undefined, reduce is skipped |
39 | function reduce (accumulator, channel) { |
40 | accumulator[channel] = (accumulator[channel] || 0) + 1 |
41 | return accumulator |
42 | } |
43 | |
44 | function initialState () { |
45 | return {} |
46 | } |
47 |
Built with git-ssb-web