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