git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: 3a6b42e980da99c6778fd2db5f693fa7d89d7207

Files: 3a6b42e980da99c6778fd2db5f693fa7d89d7207 / invite.js

2534 bytesRaw
1'use strict'
2var ref = require('ssb-ref')
3var ssbClient = require('ssb-client')
4var Value = require('mutant/value')
5var nest = require('depnest')
6
7exports.needs = nest({
8 'sbot.async.publish': 'first',
9 'sbot.async.gossipConnect': 'first',
10 'contact.async.followerOf': 'first',
11 'keys.sync.id': 'first',
12 'config.sync.load': 'first'
13})
14
15exports.gives = nest({
16 'invite.async.accept': true,
17 'invite.async.autofollow': true,
18})
19
20exports.create = function (api) {
21 function accept (invite, cb) {
22 var progress = Value('Connecting...')
23 var data = ref.parseInvite(invite)
24 var id = api.keys.sync.id()
25
26 var config = api.config.sync.load()
27
28 if (!data) return cb(new Error('Not a valid invite code. Please make sure you copied the entire code and try again.'))
29
30 api.sbot.async.gossipConnect(data.remote, function (err) {
31 if (err) console.log(err)
32 })
33
34 // connect to the remote pub using the invite code
35 ssbClient(null, {
36 remote: data.invite,
37 manifest: { invite: {use: 'async'}, getAddress: 'async' },
38 appKey: config.caps.shs
39 }, function (err, sbot) {
40 if (err) return cb(err)
41 progress.set('Requesting follow...')
42
43 // ask them to follow us
44 sbot.invite.use({feed: id}, function (err, msg) {
45 if (err) {
46 // the probably already follow us
47 api.contact.async.followerOf(id, data.key, function (_, follows) {
48 if (follows) {
49 cb()
50 } else {
51 next()
52 }
53 })
54 } else {
55 next()
56 }
57
58 function next () {
59 progress.set('Following...')
60
61 var address = ref.parseAddress(data.remote)
62
63 if (address.host) {
64 api.sbot.async.publish({
65 type: 'pub',
66 address
67 })
68 }
69
70 api.sbot.async.publish({
71 type: 'contact',
72 contact: data.key,
73 following: true,
74 autofollow: true
75 }, cb)
76 }
77 })
78 })
79
80 return progress
81 }
82 return nest({
83 'invite.async.accept': accept,
84 //like invite, but check whether we already follow them first
85 'invite.async.autofollow': function (invite, cb) {
86 var id = api.keys.sync.id()
87 var data = ref.parseInvite(invite)
88 api.contact.async.followerOf(id, data.key, function (_, follows) {
89 if (follows) console.log('already following', cb())
90 else console.log('accept invite:'+invite, accept(invite, cb))
91 })
92 }
93 })
94}
95
96

Built with git-ssb-web