git ssb

0+

mixmix / ssb-server-channel



Tree: 254f8580feb2fed2a0cd8cb6744eca1dc9a537ec

Files: 254f8580feb2fed2a0cd8cb6744eca1dc9a537ec / index.js

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

Built with git-ssb-web