Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit 44cde29

Browse files
committed
migrate from ssb-gossip to ssb-conn
1 parent 7dcf79f commit 44cde29

File tree

9 files changed

+429
-163
lines changed

9 files changed

+429
-163
lines changed

lib/depject/invite/invite.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const nest = require('depnest')
66

77
exports.needs = nest({
88
'sbot.async.publish': 'first',
9-
'sbot.async.gossipConnect': 'first',
9+
'sbot.async.connRememberConnect': 'first',
1010
'contact.async.followerOf': 'first',
1111
'keys.sync.id': 'first',
1212
'config.sync.load': 'first'
@@ -26,7 +26,7 @@ exports.create = function (api) {
2626

2727
if (!data) return cb(new Error('Not a valid invite code. Please make sure you copied the entire code and try again.'))
2828

29-
api.sbot.async.gossipConnect(data.remote, function (err) {
29+
api.sbot.async.connRememberConnect(data.remote, {type: 'pub'}, (err) => {
3030
if (err) console.log(err)
3131
})
3232

lib/depject/page/html/render/public.js

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ exports.needs = nest({
66
obs: {
77
connectedPeers: 'first',
88
localPeers: 'first',
9+
stagedPeers: 'first',
910
connection: 'first'
1011
}
1112
},
13+
'sbot.async.connConnect': 'first',
1214
'sbot.pull.stream': 'first',
1315
'sbot.pull.resumeStream': 'first',
1416
'about.html.image': 'first',
@@ -30,6 +32,7 @@ exports.needs = nest({
3032
'contact.obs.following': 'first',
3133
'contact.obs.followers': 'first',
3234
'contact.obs.blocking': 'first',
35+
'contact.async.follow': 'first',
3336
'channel.obs': {
3437
subscribed: 'first',
3538
recent: 'first'
@@ -58,8 +61,10 @@ exports.create = function (api) {
5861
const recentChannels = api.channel.obs.recent(8)
5962
const channelsLoading = computed([subscribedChannels.sync, recentChannels.sync], (...args) => !args.every(Boolean))
6063
const connectedPeers = api.sbot.obs.connectedPeers()
64+
const stagedPeers = api.sbot.obs.stagedPeers()
6165
const localPeers = api.sbot.obs.localPeers()
62-
const connectedPubs = computed([connectedPeers, localPeers], (c, l) => c.filter(x => !l.includes(x)))
66+
const localPeersKeys = map(api.sbot.obs.localPeers(), peer => peer.data.key)
67+
const connectedPubs = computed([connectedPeers, localPeersKeys], (c, l) => c.filter(x => !l.includes(x.data.key)))
6368
const contact = api.profile.obs.contact(id)
6469

6570
const prepend = [
@@ -102,7 +107,7 @@ exports.create = function (api) {
102107
return result
103108

104109
function getSidebar () {
105-
const whoToFollow = computed([api.profile.obs.recentlyUpdated(), following, blocking, localPeers], (recent, ...ignoreFeeds) => {
110+
const whoToFollow = computed([api.profile.obs.recentlyUpdated(), following, blocking, localPeersKeys ], (recent, ...ignoreFeeds) => {
106111
return recent.filter(x => x !== id && !ignoreFeeds.some(f => f.includes(x))).slice(0, 10)
107112
})
108113
return [
@@ -145,6 +150,7 @@ exports.create = function (api) {
145150
]),
146151

147152
PeerList(localPeers, i18n('Local')),
153+
SuggestedPeerList(stagedPeers, i18n('Possible connections')),
148154
PeerList(connectedPubs, i18n('Connected Pubs')),
149155

150156
when(computed(whoToFollow, x => x.length), h('h2', i18n('Who to follow'))),
@@ -167,14 +173,16 @@ exports.create = function (api) {
167173
]
168174
}
169175

170-
function PeerList (ids, title) {
176+
function PeerList (peers, title) {
171177
return [
172-
when(computed(ids, x => x.length), h('h2', title)),
178+
when(computed(peers, arr => arr.length), h('h2', title)),
173179
h('div', {
174180
classList: 'ProfileList'
175181
}, [
176-
map(ids, (id) => {
177-
const connected = computed([connectedPeers, id], (peers, id) => peers.includes(id))
182+
map(peers, peer => {
183+
const address = peer.address
184+
const connected = peer.data.state === 'connected'
185+
const id = peer.data.key
178186
return h('a.profile', {
179187
classList: [
180188
when(connected, '-connected')
@@ -189,7 +197,28 @@ exports.create = function (api) {
189197
api.progress.html.peer(id)
190198
]),
191199
h('div.controls', [
192-
h('a.disconnect', { href: '#', 'ev-click': send(disconnect, id), title: i18n('Force Disconnect') }, ['x'])
200+
h('a.disconnect', { href: '#', 'ev-click': send(disconnect, address), title: i18n('Force Disconnect') }, ['x'])
201+
])
202+
])
203+
})
204+
])
205+
]
206+
}
207+
208+
function SuggestedPeerList (peers, title) {
209+
return [
210+
when(computed(peers, arr => arr.length), h('h2', title)),
211+
h('div', {
212+
classList: 'ProfileList'
213+
}, [
214+
map(peers, peer => {
215+
const id = peer.data.key
216+
return h('a.profile', { href: id }, [
217+
h('div.main', [
218+
h('div.name', [api.about.obs.name(id)])
219+
]),
220+
h('div.controls', [
221+
h('a.connect', { href: '#', 'ev-click': send(connect, peer), title: i18n('Connect') }, [i18n('Connect')])
193222
])
194223
])
195224
})
@@ -224,9 +253,13 @@ exports.create = function (api) {
224253
return api.feed.html.followerWarning(shownWhen, explanation)
225254
}
226255

227-
function disconnect (id) {
228-
onceTrue(api.sbot.obs.connection, (sbot) => {
229-
sbot.patchwork.disconnect(id)
256+
function connect (peer) {
257+
api.sbot.async.connConnect(peer.address, peer.data, () => {})
258+
}
259+
260+
function disconnect (addr) {
261+
onceTrue(api.sbot.obs.connection, sbot => {
262+
sbot.patchwork.disconnect(addr)
230263
})
231264
}
232265
}

lib/depject/sbot.js

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ exports.gives = {
2727
get: true,
2828
publish: true,
2929
addBlob: true,
30-
gossipConnect: true,
30+
connConnect: true,
31+
connRememberConnect: true,
3132
friendsGet: true,
3233
acceptDHT: true,
3334
createDHT: true
@@ -46,7 +47,8 @@ exports.gives = {
4647
connectionStatus: true,
4748
connection: true,
4849
connectedPeers: true,
49-
localPeers: true
50+
localPeers: true,
51+
stagedPeers: true
5052
}
5153
}
5254
}
@@ -61,6 +63,7 @@ exports.create = function (api) {
6163
const connectionStatus = Value()
6264
const connectedPeers = MutantSet()
6365
const localPeers = MutantSet()
66+
const stagedPeers = MutantSet()
6467

6568
const rec = Reconnect(function (isConn) {
6669
function notify (value) {
@@ -96,40 +99,22 @@ exports.create = function (api) {
9699
})
97100
})
98101

99-
setInterval(function () {
100-
if (sbot) {
101-
sbot.gossip.peers((err, peers) => {
102-
if (err) return console.error(err)
103-
localPeers.set(peers.filter(x => x.source === 'local').map(x => x.key))
104-
connectedPeers.set(peers.filter(x => x.state === 'connected').map(x => x.key))
105-
})
106-
}
107-
}, 1000 * 60)
108-
109102
watch(connection, (sbot) => {
110103
if (sbot) {
111-
sbot.gossip.peers((err, peers) => {
112-
if (err) return console.error(err)
113-
connectedPeers.set(peers.filter(x => x.state === 'connected').map(x => x.key))
114-
localPeers.set(peers.filter(x => x.source === 'local').map(x => x.key))
115-
})
116104
pull(
117-
sbot.gossip.changes(),
118-
pull.drain(data => {
119-
if (data.peer) {
120-
if (data.type === 'remove' || data.type === 'disconnect') {
121-
connectedPeers.delete(data.peer.key)
122-
} else {
123-
if (data.peer.source === 'local') {
124-
localPeers.add(data.peer.key)
125-
}
126-
if (data.peer.state === 'connected') {
127-
connectedPeers.add(data.peer.key)
128-
} else {
129-
connectedPeers.delete(data.peer.key)
130-
}
131-
}
132-
}
105+
sbot.conn.peers(),
106+
pull.drain(entries => {
107+
var peers = entries.filter(([, x]) => !!x.key).map(([address, data]) => ({ address, data }))
108+
localPeers.set(peers.filter(peer => peer.data.type === 'lan'))
109+
connectedPeers.set(peers.filter(peer => peer.data.state === 'connected'))
110+
})
111+
)
112+
113+
pull(
114+
sbot.conn.stagedPeers(),
115+
pull.drain(entries => {
116+
var peers = entries.filter(([, x]) => !!x.key).map(([address, data]) => ({ address, data }))
117+
stagedPeers.set(peers)
133118
})
134119
)
135120
}
@@ -215,8 +200,14 @@ exports.create = function (api) {
215200
sbot.blobs.add(cb)
216201
)
217202
}),
218-
gossipConnect: rec.async(function (opts, cb) {
219-
sbot.gossip.connect(opts, cb)
203+
connConnect: rec.async(function (address, data, cb) {
204+
sbot.conn.connect(address, data, cb)
205+
}),
206+
connRememberConnect: rec.async(function (address, data, cb) {
207+
sbot.conn.remember(address, {autoconnect: true, ...data}, (err) => {
208+
if (err) cb(err)
209+
else sbot.conn.connect(address, data, cb)
210+
})
220211
}),
221212
friendsGet: rec.async(function (opts, cb) {
222213
sbot.friends.get(opts, cb)
@@ -276,7 +267,8 @@ exports.create = function (api) {
276267
connectionStatus: (listener) => connectionStatus(listener),
277268
connection,
278269
connectedPeers: () => connectedPeers,
279-
localPeers: () => localPeers
270+
localPeers: () => localPeers,
271+
stagedPeers: () => stagedPeers
280272
}
281273
}
282274
}

lib/plugins/index.js

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,9 @@ exports.init = function (ssb, config) {
7070
getSubscriptions: subscriptions.get,
7171
liveBacklinks: LiveBacklinks(ssb),
7272

73-
disconnect: function (opts, cb) {
74-
if (ref.isFeed(opts)) opts = { key: opts }
75-
if (opts && (opts.key || opts.host)) {
76-
ssb.gossip.peers().find(peer => {
77-
if (peer.state === 'connected' && (peer.key === opts.key || peer.host === opts.host)) {
78-
ssb.gossip.disconnect(peer, cb)
79-
return true
80-
}
81-
})
82-
}
73+
disconnect: function (addr, cb) {
74+
ssb.conn.disconnect(addr, cb)
75+
return true
8376
}
8477
}
8578

@@ -88,22 +81,6 @@ exports.init = function (ssb, config) {
8881
}
8982

9083
// CONNECTIONS
91-
// prioritize friends for pub connections and remove blocked pubs (on startup)
92-
patchwork.contacts.raw.get((err, graph) => {
93-
if (!err) {
94-
ssb.gossip.peers().slice().forEach((peer) => {
95-
if (graph && graph[ssb.id]) {
96-
const value = graph[ssb.id][peer.key]
97-
if (value === true) { // following pub
98-
ssb.gossip.add(peer, 'friends')
99-
} else if (value === false) { // blocked pub
100-
ssb.gossip.remove(peer.key)
101-
}
102-
}
103-
})
104-
}
105-
})
106-
10784
// refuse connections from blocked peers
10885
ssb.auth.hook(function (fn, args) {
10986
const self = this
@@ -129,9 +106,9 @@ exports.init = function (ssb, config) {
129106
pull.drain(function (value) {
130107
if (value.sync && config.gossip && config.gossip.prune) {
131108
// clean up pubs announced by peers more than 2 hops away if `--gossip.prune=true`
132-
ssb.gossip.peers().slice().forEach(peer => {
133-
if (!discovered.has(peer.key)) {
134-
ssb.gossip.remove(peer, 'pub')
109+
ssb.conn.query().peersConnectable('db').forEach(([addr, data]) => {
110+
if (!discovered.has(data.key)) {
111+
ssb.conn.forget(addr)
135112
}
136113
})
137114
}
@@ -142,7 +119,10 @@ exports.init = function (ssb, config) {
142119
const blocking = graph && graph[ssb.id] && graph[ssb.id][address.key] === false
143120
if (!blocking) {
144121
discovered.add(address.key)
145-
ssb.gossip.add(address, 'pub')
122+
try {
123+
const msAddr = ref.toMultiServerAddress(address);
124+
ssb.conn.remember(msAddr, {type: 'pub', key: address.key, autoconnect: true});
125+
} catch (err) {}
146126
}
147127
}
148128
})
@@ -162,18 +142,6 @@ exports.init = function (ssb, config) {
162142

163143
// request (or unrequest) the feed
164144
ssb.replicate.request(feedId, state[feedId] === true)
165-
166-
// if blocking and a pub, drop connection and remove from peer table
167-
if (state[feedId] === false) {
168-
const peer = ssb.gossip.get(feedId)
169-
if (peer) {
170-
if (peer.state === 'connected') {
171-
ssb.gossip.disconnect(peer, () => {
172-
ssb.gossip.remove(peer)
173-
})
174-
}
175-
}
176-
}
177145
}
178146
})
179147
)

lib/server-process.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ const fixPath = require('fix-path')
2121

2222
const createSbot = require('secret-stack')()
2323
.use(require('ssb-db'))
24-
.use(require('ssb-legacy-conn'))
25-
.use(require('ssb-local'))
24+
.use(require('ssb-conn'))
25+
.use(require('ssb-lan'))
2626
.use(require('ssb-logging'))
2727
.use(require('ssb-master'))
2828
.use(require('ssb-no-auth'))
2929
.use(require('ssb-replicate'))
3030
.use(require('ssb-unix-socket'))
31-
// .use(require('ssb-friends')) // woah! (being handled in sbot/index.js and sbot/contacts.js)
31+
.use(require('ssb-friends')) // not strictly required, but helps ssb-conn a lot
3232
.use(require('ssb-blobs'))
3333
.use(require('ssb-backlinks'))
3434
.use(require('ssb-about'))

locales/en.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,5 +311,7 @@
311311
"eo": "eo",
312312
"zh-TW": "zh-TW",
313313
"Blocking": "Blocking",
314-
"Automatically delete messages from blocked authors. This is irreversible and will cause problems with clients that share the database but do not support deleted messages. Enable at your own risk!": "Automatically delete messages from blocked authors. This is irreversible and will cause problems with clients that share the database but do not support deleted messages. Enable at your own risk!"
315-
}
314+
"Automatically delete messages from blocked authors. This is irreversible and will cause problems with clients that share the database but do not support deleted messages. Enable at your own risk!": "Automatically delete messages from blocked authors. This is irreversible and will cause problems with clients that share the database but do not support deleted messages. Enable at your own risk!",
315+
"Possible connections": "Possible connections",
316+
"Connect": "Connect"
317+
}

0 commit comments

Comments
 (0)