git ssb

0+

mixmix / ssb-server-channel



Tree: 92f5724222307187f23d545ec3c17c9aeedd6922

Files: 92f5724222307187f23d545ec3c17c9aeedd6922 / index.js

1779 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: 'channel',
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 'channel',
19 FlumeView(FLUME_VIEW_VERSION, reduce, map, null, initialState())
20 )
21
22 return {
23 get: view.get,
24 subscription: view.get,
25 stream: view.stream,
26 }
27 }
28}
29
30function initialState () {
31 return {}
32}
33
34
35function map (msg) {
36 if (get(msg, 'value.content.type') !== 'channel') return null
37
38 const author = msg.value.author
39 const channel = get(msg, 'value.content.channel')
40 const subscribed = get(msg, 'value.content.subscribed')
41
42 if (typeof channel === undefined || typeof subscribed === undefined) {
43 console.log('Malformed channel subscription', msg)
44 return null
45 }
46
47 return {
48 channel,
49 author,
50 subscribed
51 }
52}
53
54function reduce (soFar, newSub) {
55 process.stdout.write('c')
56 const { channel, author, subscribed } = newSub
57
58 const channelSubs = get(soFar, [channel], new Set())
59 if (subscribed) channelSubs.add(author)
60 else channelSubs.delete(author)
61
62 soFar[channel] = channelSubs
63
64 return soFar
65}
66
67// state:
68// {
69// [Channel]: [ Set
70// FeedId
71// ]
72// }
73
74
75// {
76// 'ssb-learning': [
77// '@ye14....',
78// '@weandre..',
79// ],
80// 'brazil': [
81// '@weandre..',
82// ]
83// }
84
85// channel message:
86// value: {
87// author: FeedId,
88// content: {
89// {
90// type: 'channel',
91// channel: String, // ssb-learning
92// subscribed: Boolean
93// }
94// }
95// }
96
97

Built with git-ssb-web