Files: 2b05be8ee023fdd9580d6a845ba074a425756685 / app / html / settings / friend-pub.js
3072 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, computed, onceTrue, Value, watch } = require('mutant') |
3 | const merge = require('lodash/merge') |
4 | |
5 | exports.gives = nest({ |
6 | 'app.html.settings': true |
7 | }) |
8 | |
9 | exports.needs = nest({ |
10 | 'about.html.image': 'first', |
11 | 'about.obs.name': 'first', |
12 | 'app.html.settings': 'map', |
13 | 'sbot.obs.connection': 'first', |
14 | 'config.sync.get': 'first', |
15 | 'config.sync.getCustom': 'first', |
16 | 'config.sync.setCustom': 'first' |
17 | }) |
18 | |
19 | const pubHopAll = 3 |
20 | exports.create = function (api) { |
21 | return nest({ |
22 | 'app.html.settings': pubHopConnections |
23 | }) |
24 | |
25 | function pubHopConnections () { |
26 | let hops = Value(api.config.sync.get('friendPub.hops', pubHopAll)) |
27 | hops.initial = hops() |
28 | let pubs = Value({}) |
29 | |
30 | watch(hops, hops => { |
31 | updateConfig(hops) |
32 | updatePubs(hops) |
33 | }) |
34 | |
35 | return { |
36 | group: 'replication', |
37 | title: 'Pub replication', |
38 | body: h('FriendPub', [ |
39 | h('div.description', [ |
40 | 'Limit gossip with pubs (always online peers who help replication) based on who owns the pub' |
41 | ]), |
42 | h('div.slider', [ |
43 | h('datalist', { id: 'pub-gossip-datalist' }, [ |
44 | h('option', { value: 0, label: 'My pub' }), |
45 | h('option', { value: 1, label: 'My friends' }), |
46 | h('option', { value: 2, label: 'A friends friend' }), |
47 | h('option', { value: 3, label: 'Any pub' }) |
48 | ]), |
49 | h('input', { |
50 | type: 'range', |
51 | attributes: { list: 'pub-gossip-datalist' }, |
52 | min: 0, |
53 | max: 3, |
54 | value: hops, |
55 | 'ev-change': (ev) => hops.set(parseInt(ev.target.value)) |
56 | }) |
57 | ]), |
58 | computed(hops, (_hops) => { |
59 | if (_hops === hops.initial) return |
60 | return h('div.alert', [ |
61 | h('i.fa.fa-warning'), |
62 | ' please restart patchbay for this to take effect' |
63 | ]) |
64 | }), |
65 | Pubs(pubs) |
66 | ]) |
67 | } |
68 | |
69 | function updatePubs (hops) { |
70 | onceTrue(api.sbot.obs.connection, sbot => { |
71 | if (hops === pubHopAll) pubs.set({}) |
72 | else { |
73 | sbot.friendPub.pubsWithinHops(hops, (_, pubsInHops) => { |
74 | pubs.set(pubsInHops) |
75 | }) |
76 | } |
77 | }) |
78 | } |
79 | } |
80 | |
81 | function Pubs (pubs) { |
82 | return h('Pubs', [ |
83 | h('div.description', 'Pubs this means you will gossip with:'), |
84 | h('div.pubs', computed([pubs], function (pubs) { |
85 | return Object.values(pubs).map(pub => pubImageLink(pub.id, pub.owner)) |
86 | })) |
87 | ]) |
88 | } |
89 | function pubImageLink (id, ownerId) { |
90 | return h('a', { |
91 | href: id, |
92 | title: computed([api.about.obs.name(id), api.about.obs.name(ownerId)], (name, ownerName) => { |
93 | return '@' + name + ', owner ' + ownerName |
94 | }) |
95 | }, api.about.html.image(id)) |
96 | } |
97 | |
98 | function updateConfig (hops) { |
99 | const configCustom = api.config.sync.getCustom() |
100 | |
101 | const next = merge({}, configCustom, { |
102 | friendPub: { hops }, |
103 | gossip: hops >= 3 |
104 | ? { friends: true, global: true } |
105 | : { friends: true, global: false } |
106 | }) |
107 | |
108 | api.config.sync.setCustom(next) |
109 | } |
110 | } |
111 |
Built with git-ssb-web