Files: 579d1f5953b53c39e3c97eb628713affeabd63be / app / html / settings / friend-pub.js
3009 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 | title: 'Pub gossip', |
37 | body: h('FriendPub', [ |
38 | h('div.description', [ |
39 | 'Limit gossip with pubs based on who owns the pub' |
40 | ]), |
41 | h('div.slider', [ |
42 | h('datalist', { id: 'pub-gossip-datalist' }, [ |
43 | h('option', { value: 0, label: 'My pub' }), |
44 | h('option', { value: 1, label: 'My friend' }), |
45 | h('option', { value: 2, label: 'A friends friend' }), |
46 | h('option', { value: 3, label: 'Any pub' }) |
47 | ]), |
48 | h('input', { |
49 | type: 'range', |
50 | attributes: { list: 'pub-gossip-datalist' }, |
51 | min: 0, |
52 | max: 3, |
53 | value: hops, |
54 | 'ev-change': (ev) => hops.set(ev.target.value) |
55 | }) |
56 | ]), |
57 | computed(hops, (_hops) => { |
58 | if (_hops === hops.initial) return |
59 | return h('div.alert', [ |
60 | h('i.fa.fa-warning'), |
61 | ' please restart patchbay for this to take effect' |
62 | ]) |
63 | }), |
64 | Pubs(pubs) |
65 | ]) |
66 | } |
67 | |
68 | function updateConfig (hops) { |
69 | const configCustom = api.config.sync.getCustom() |
70 | |
71 | const next = merge({}, configCustom, { |
72 | friendPub: { hops }, |
73 | gossip: hops >= 3 |
74 | ? { friends: true, global: true } |
75 | : { friends: true, global: false } |
76 | }) |
77 | |
78 | api.config.sync.setCustom(next) |
79 | } |
80 | |
81 | function updatePubs (hops) { |
82 | onceTrue(api.sbot.obs.connection, sbot => { |
83 | if (hops === pubHopAll) pubs.set({}) |
84 | else { |
85 | sbot.friendPub.pubsWithinHops(hops, (_, pubsInHops) => { |
86 | pubs.set(pubsInHops) |
87 | }) |
88 | } |
89 | }) |
90 | } |
91 | } |
92 | |
93 | function Pubs (pubs) { |
94 | return h('Pubs', [ |
95 | h('div.description', 'Pubs you this means you will gossip with:'), |
96 | h('div.pubs', computed([pubs], function (pubs) { |
97 | return Object.values(pubs).map(pub => pubImageLink(pub.id, pub.owner)) |
98 | })) |
99 | ]) |
100 | } |
101 | function pubImageLink (id, ownerId) { |
102 | return h('a', { |
103 | href: id, |
104 | title: computed([api.about.obs.name(id), api.about.obs.name(ownerId)], (name, ownerName) => { |
105 | return '@' + name + ', owner ' + ownerName |
106 | }) |
107 | }, api.about.html.image(id)) |
108 | } |
109 | } |
110 |
Built with git-ssb-web