git ssb

0+

mixmix / ssb-server-channel



Tree: ed7ce18d13ca61dec8480839bd240b0f4d50ee43

Files: ed7ce18d13ca61dec8480839bd240b0f4d50ee43 / index.js

1796 bytesRaw
1const FlumeView = require('flumeview-reduce')
2const get = require('lodash/get')
3const set = require('lodash/set')
4
5const FLUME_VIEW_VERSION = 1.5
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 'channels',
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
60 if (subscribed) {
61 channelSubs.add(author)
62 } else {
63 channelSubs.delete(author)
64 }
65
66 soFar[channel] = channelSubs
67
68 return soFar
69}
70
71// state:
72// {
73// [Channel]: [ Set
74// FeedId
75// ]
76// }
77
78
79// {
80// 'ssb-learning': [
81// '@ye14....',
82// '@weandre..',
83// ],
84// 'brazil': [
85// '@weandre..',
86// ]
87// }
88
89// channel message:
90// value: {
91// author: FeedId,
92// content: {
93// {
94// type: 'channel',
95// channel: String, // ssb-learning
96// subscribed: Boolean
97// }
98// }
99// }
100
101

Built with git-ssb-web