git ssb

0+

Dominic / ssb-peer-invites



Commit a26f91654f9e1a5d1ed4cb31b5ac1b58ed70c7a6

create supports options to create invites without pubs

Dominic Tarr committed on 6/30/2019, 11:34:19 AM
Parent: 12ca5ce78f962f0468698cc500fb68cd7e909b50

Files changed

index.jschanged
index.jsView
@@ -356,17 +356,24 @@
356356 if(isFunction(opts))
357357 return opts(new Error ('peer-invites: expected: options *must* be provided.'))
358358
359359 var host_id = opts.id || sbot.id
360- invites.getNearbyPubs(opts, function (err, near) {
361- if(!opts.allowWithoutPubs) {
360 + if(opts.allowWithoutPubs) {
361 + var pubs = opts.pubs
362 + pubs = Array.isArray(pubs) ? pubs : 'string' == typeof pubs ? pubs.split(',') : []
363 + create(pubs)
364 + }
365 + else
366 + invites.getNearbyPubs(opts, function (err, near) {
362367 near = near.filter(function (e) {
363368 return e.willReplicate
364369 }).slice(0, opts.max || 3)
365370 if(near.length == 0)
366371 return cb(new Error('failed to find any suitable pubs'))
367- }
372 + create(near.map(function (e) { return e.address }))
373 + })
368374
375 + function create(pubs) {
369376 var seed = crypto.randomBytes(32).toString('base64')
370377 sbot.identities.publishAs({
371378 id: host_id,
372379 content: I.createInvite(seed, host_id, opts.reveal, opts.private, caps)
@@ -375,20 +382,22 @@
375382 var invite = {
376383 seed: seed,
377384 invite: data.key,
378385 cap: opts.cap,
379- pubs: near.map(function (e) { return e.address }),
386 + pubs: pubs,
380387 }
381388 cb(null, u.stringify(invite))
382389 })
383- })
390 + }
384391 }
385392
386393 //try each of an array of addresses, and cb the first one that works.
387394 function connectFirst (invite, cb) {
388395 var n = 0, err
389396 var keys = ssbKeys.generate(null, toBuffer(invite.seed))
390- invite.pubs.forEach(function (addr) {
397 + var pubs = invite.pubs.filter(Boolean)
398 + if(!pubs.length) return cb(new Error('peer-invites: invite missing pub addresses'))
399 + pubs.forEach(function (addr) {
391400 n++
392401 //don't use sbot.connect here, because we are connecting
393402 //with a different cap.
394403 ssbClient(keys, {
@@ -406,9 +415,9 @@
406415 cb(null, rpc)
407416 } else {
408417 err = err || _err
409418 }
410- if(--n == 0) cb(explain(err, 'while trying to connect to:'+remote))
419 + if(--n == 0) cb(explain(err, 'while trying to connect to:'+addr))
411420 })
412421 })
413422 }
414423

Built with git-ssb-web