git ssb

0+

Dominic / ssb-peer-invites



Commit a802072d7617b6d4a9de94ea317a53968dd3b63a

add willReplicate

Dominic Tarr committed on 12/17/2018, 7:16:04 PM
Parent: 74fdd566bc891a78ae65fcc6e0220fdcbbde4e21

Files changed

index.jschanged
index.jsView
@@ -1,13 +1,15 @@
1-var pull = require('pull-stream')
2-var Reduce = require('flumeview-reduce')
3-var I = require('./valid')
1 +var pull = require('pull-stream')
2 +var Reduce = require('flumeview-reduce')
3 +var I = require('./valid')
44 var deepEquals = require('deep-equals')
5-var crypto = require('crypto')
6-var ssbKeys = require('ssb-keys')
7-var ssbClient = require('ssb-client')
8-var types = require('./types')
5 +var types = require('./types')
6 +var paramap = require('pull-paramap')
7 +var ssbClient = require('ssb-client')
8 +var crypto = require('crypto')
9 +var ssbKeys = require('ssb-keys')
910
11 +
1012 function code(err, c) {
1113 err.code = 'user-invites:'+c
1214 return err
1315 }
@@ -29,13 +31,14 @@
2931 exports.version = '1.0.0'
3032 exports.manifest = {
3133 getInvite: 'async',
3234 confirm: 'async',
33- create: 'async'
35 + create: 'async',
36 + willReplicate: 'async'
3437 }
3538
3639 exports.permissions = {
37-// master: {allow: ['create']}
40 + anonymous: {allow: ['willReplicate']},
3841 }
3942
4043 // KNOWN BUG: it's possible to accept an invite more than once,
4144 // but peers will ignore subsequent acceptances. it's possible
@@ -49,10 +52,10 @@
4952 var init = false
5053 var layer = sbot.friends.createLayer('user-invites')
5154
5255 var caps = config.caps || {}
56 + caps.userInvite = caps.userInvite || require('./cap')
5357 var initial = {invites: {}, accepts: {}, hosts: {}, guests: {}}
54- caps.userInvite = caps.userInvite || require('./cap')
5558
5659 function reduce (acc, data, _seq) {
5760 if(!acc) acc = initial
5861 var msg = data.value
@@ -200,19 +203,8 @@
200203 )
201204 }, cb)
202205 }
203206
204-
205- function getAccept (invite_id, cb) {
206- getResponse(invite_id, function (msg) {
207- return (
208- msg.content.type === 'user-invite/accept' &&
209- msg.content.receipt === invite_id
210- )
211- }, cb)
212- }
213-
214-
215207 //used to request that a server confirms your acceptance.
216208 invites.confirm = function (accept, cb) {
217209 var invite_id = accept.content.receipt
218210 //check if the invite in question hasn't already been accepted.
@@ -238,8 +230,33 @@
238230 })
239231 })
240232 }
241233
234 +
235 + //if the caller is someone we know, let them know wether
236 + //we are willing to confirm (and replicate) their guest.
237 + invites.willReplicate = function (opts, cb) {
238 + if(isFunction(opts)) cb = opts, opts = {}
239 + var id = this.id //id of caller
240 + var max = config.friends && config.friends.hops || config.replicate && config.replicate.hops || 3
241 + sbot.friends.hops({}, function (err, hops) {
242 + // compare hops of caller (host to be) with max - 1
243 + // because that means that the hops of the guest
244 + // will be in range.
245 + if(hops[id] <= (max - 1)) cb(null, true)
246 + else cb(null, false)
247 + })
248 + }
249 +
250 + function getAccept (invite_id, cb) {
251 + getResponse(invite_id, function (msg) {
252 + return (
253 + msg.content.type === 'user-invite/accept' &&
254 + msg.content.receipt === invite_id
255 + )
256 + }, cb)
257 + }
258 +
242259 //retrive pubs who might be willing to confirm your invite. (used when creating an invte)
243260 function getNearbyPubs (opts, cb) {
244261 var maxHops = opts.hops || 2
245262 sbot.deviceAddress.getState(function (err, state) {
@@ -265,9 +282,26 @@
265282 a.hops - b.hops ||
266283 b.availability - a.availability
267284 )
268285 })
269- cb(null, near)
286 +
287 + if(opts.offline) return cb(null, near)
288 +
289 + pull(
290 + pull.values(near),
291 + paramap(function (pub, cb) {
292 + sbot.connect(pub.address, function (err, rpc) {
293 + rpc.userInvites.willReplicate({}, function (err, v) {
294 + //pass through input if true, else (err or false)
295 + //then drop.
296 + cb(null, v && pub)
297 + })
298 + })
299 + },3),
300 + pull.filter(Boolean),
301 + pull.take(3),
302 + pull.collect(cb)
303 + )
270304 })
271305 })
272306 }
273307
@@ -296,8 +330,10 @@
296330 function connectFirst (keys, pubs, cb) {
297331 var n = 0, err
298332 pubs.forEach(function (addr) {
299333 n++
334 + //don't use sbot.connect here, because we are connecting
335 + //with a different cap.
300336 ssbClient(keys, {
301337 remote: addr,
302338 caps: caps,
303339 manifest: {
@@ -395,9 +431,11 @@
395431 })
396432 })
397433 }
398434 }
435 +
399436 return invites
400437 }
401438
402439
403440
441 +

Built with git-ssb-web