git ssb

0+

mixmix / ssb-server-channel



Tree: ceb725feeb3aac110f9c519878705290803c3ce7

Files: ceb725feeb3aac110f9c519878705290803c3ce7 / index.js

1751 bytesRaw
1const FlumeView = require('flumeview-reduce')
2const get = require('lodash/get')
3const set = require('lodash/set')
4
5const FLUME_VIEW_VERSION = 1.3
6
7module.exports = {
8 name: 'channels',
9 version: require('./package.json').version,
10 manifest: {
11 get: 'async',
12 stream: 'source',
13 },
14 init: (server, config) => {
15 console.log('///// CHANNELS plugin loaded /////')
16
17 const view = server._flumeUse(
18 'channels',
19 FlumeView(FLUME_VIEW_VERSION, reduce, map, null, initialState())
20 )
21
22 return {
23 get: view.get,
24 stream: view.stream,
25 }
26 }
27}
28
29function initialState () {
30 return {}
31}
32
33
34function map (msg) {
35 if (get(msg, 'value.content.type') !== 'channel') return null
36
37 const author = msg.value.author
38 const channel = get(msg, 'value.content.channel')
39 const subscribed = get(msg, 'value.content.subscribed')
40
41 if (typeof channel === undefined || typeof subscribed === undefined) {
42 console.log('Malformed channel subscription', msg)
43 return null
44 }
45
46 return {
47 channel,
48 author,
49 subscribed
50 }
51}
52
53function reduce (soFar, newSub) {
54 process.stdout.write('c')
55 const { channel, author, subscribed } = newSub
56
57 const channelSubs = get(soFar, [channel], new Set())
58 if (subscribed) channelSubs.add(author)
59 else channelSubs.delete(author)
60
61 soFar[channel] = channelSubs
62
63 return soFar
64}
65
66// state:
67// {
68// [Channel]: [ Set
69// FeedId
70// ]
71// }
72
73
74// {
75// 'ssb-learning': [
76// '@ye14....',
77// '@weandre..',
78// ],
79// 'brazil': [
80// '@weandre..',
81// ]
82// }
83
84// channel message:
85// value: {
86// author: FeedId,
87// content: {
88// {
89// type: 'channel',
90// channel: String, // ssb-learning
91// subscribed: Boolean
92// }
93// }
94// }
95
96

Built with git-ssb-web