git ssb

0+

Dominic / ssb-peer-invites



Commit a9decd2c1b7403fe1c4122243fec563cc1214351

refactor out sort method, and add min, max options

Dominic Tarr committed on 6/30/2019, 11:01:41 AM
Parent: 569614847b383eac9d3dde8b590a056de60750a2

Files changed

index.jschanged
index.jsView
@@ -305,9 +305,9 @@
305305 })
306306
307307 if(opts.offline) return cb(null, near)
308308
309- var count = 3, found = []
309 + var count = opts.min || 3, found = []
310310
311311 function pushFound (pub, err, will) {
312312 found.push({
313313 id: pub.id, address: pub.address,
@@ -317,11 +317,9 @@
317317 })
318318 if(will) count --
319319 //sort in order of wether they will replicate,
320320 //or availability
321- found.sort(function (a, b) {
322- (!!b.willReplicate) - (!!a.willReplicate) || b.availability - a.availability
323- })
321 + found = u.sort(found)
324322 }
325323
326324 pull(
327325 pull.values(near),
@@ -359,10 +357,15 @@
359357 return opts(new Error ('peer-invites: expected: options *must* be provided.'))
360358
361359 var host_id = opts.id || sbot.id
362360 invites.getNearbyPubs(opts, function (err, near) {
363- if(near.length == 0 && !opts.allowWithoutPubs)
364- return cb(new Error('failed to find any suitable pubs'))
361 + if(!opts.allowWithoutPubs) {
362 + near = near.filter(function (e) {
363 + return e.willReplicate
364 + }).slice(0, opts.max || 3)
365 + if(near.length == 0)
366 + return cb(new Error('failed to find any suitable pubs'))
367 + }
365368
366369 var seed = crypto.randomBytes(32).toString('base64')
367370 sbot.identities.publishAs({
368371 id: host_id,
@@ -491,6 +494,4 @@
491494 }
492495
493496 // I am not happy with how big this file is
494497 // but I can't see a really good line along which to break it up.
495-
496-

Built with git-ssb-web