Commit a26f91654f9e1a5d1ed4cb31b5ac1b58ed70c7a6
create supports options to create invites without pubs
Dominic Tarr committed on 6/30/2019, 11:34:19 AMParent: 12ca5ce78f962f0468698cc500fb68cd7e909b50
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -356,17 +356,24 @@ | ||
356 | 356 … | if(isFunction(opts)) |
357 | 357 … | return opts(new Error ('peer-invites: expected: options *must* be provided.')) |
358 | 358 … | |
359 | 359 … | 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) { | |
362 | 367 … | near = near.filter(function (e) { |
363 | 368 … | return e.willReplicate |
364 | 369 … | }).slice(0, opts.max || 3) |
365 | 370 … | if(near.length == 0) |
366 | 371 … | return cb(new Error('failed to find any suitable pubs')) |
367 | - } | |
372 … | + create(near.map(function (e) { return e.address })) | |
373 … | + }) | |
368 | 374 … | |
375 … | + function create(pubs) { | |
369 | 376 … | var seed = crypto.randomBytes(32).toString('base64') |
370 | 377 … | sbot.identities.publishAs({ |
371 | 378 … | id: host_id, |
372 | 379 … | content: I.createInvite(seed, host_id, opts.reveal, opts.private, caps) |
@@ -375,20 +382,22 @@ | ||
375 | 382 … | var invite = { |
376 | 383 … | seed: seed, |
377 | 384 … | invite: data.key, |
378 | 385 … | cap: opts.cap, |
379 | - pubs: near.map(function (e) { return e.address }), | |
386 … | + pubs: pubs, | |
380 | 387 … | } |
381 | 388 … | cb(null, u.stringify(invite)) |
382 | 389 … | }) |
383 | - }) | |
390 … | + } | |
384 | 391 … | } |
385 | 392 … | |
386 | 393 … | //try each of an array of addresses, and cb the first one that works. |
387 | 394 … | function connectFirst (invite, cb) { |
388 | 395 … | var n = 0, err |
389 | 396 … | 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) { | |
391 | 400 … | n++ |
392 | 401 … | //don't use sbot.connect here, because we are connecting |
393 | 402 … | //with a different cap. |
394 | 403 … | ssbClient(keys, { |
@@ -406,9 +415,9 @@ | ||
406 | 415 … | cb(null, rpc) |
407 | 416 … | } else { |
408 | 417 … | err = err || _err |
409 | 418 … | } |
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)) | |
411 | 420 … | }) |
412 | 421 … | }) |
413 | 422 … | } |
414 | 423 … |
Built with git-ssb-web