Files: dd429902f3108d6b49d95c16ad3d853cfc7dc12a / invite.js
2392 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({ |
15 | 'invite.async.accept': true, |
16 | 'invite.async.autofollow': true, |
17 | }) |
18 | |
19 | exports.create = function (api) { |
20 | function accept (invite, cb) { |
21 | var progress = Value('Connecting...') |
22 | var data = ref.parseInvite(invite) |
23 | var id = api.keys.sync.id() |
24 | if (!data) return cb(new Error('Not a valid invite code. Please make sure you copied the entire code and try again.')) |
25 | |
26 | api.sbot.async.gossipConnect(data.remote, function (err) { |
27 | if (err) console.log(err) |
28 | }) |
29 | |
30 | // connect to the remote pub using the invite code |
31 | ssbClient(null, { |
32 | remote: data.invite, |
33 | manifest: { invite: {use: 'async'}, getAddress: 'async' } |
34 | }, function (err, sbot) { |
35 | if (err) return cb(err) |
36 | progress.set('Requesting follow...') |
37 | |
38 | // ask them to follow us |
39 | sbot.invite.use({feed: id}, function (err, msg) { |
40 | if (err) { |
41 | // the probably already follow us |
42 | api.contact.async.followerOf(id, data.key, function (_, follows) { |
43 | if (follows) { |
44 | cb() |
45 | } else { |
46 | next() |
47 | } |
48 | }) |
49 | } else { |
50 | next() |
51 | } |
52 | |
53 | function next () { |
54 | progress.set('Following...') |
55 | |
56 | var address = ref.parseAddress(data.remote) |
57 | |
58 | if (address.host) { |
59 | api.sbot.async.publish({ |
60 | type: 'pub', |
61 | address |
62 | }) |
63 | } |
64 | |
65 | api.sbot.async.publish({ |
66 | type: 'contact', |
67 | contact: data.key, |
68 | following: true, |
69 | autofollow: true |
70 | }, cb) |
71 | } |
72 | }) |
73 | }) |
74 | |
75 | return progress |
76 | } |
77 | return nest({ |
78 | 'invite.async.accept': accept, |
79 | //like invite, but check whether we already follow them first |
80 | 'invite.async.autofollow': function (invite, cb) { |
81 | var id = api.keys.sync.id() |
82 | var data = ref.parseInvite(invite) |
83 | api.contact.async.followerOf(id, data.key, function (_, follows) { |
84 | if (follows) console.log('already following', cb()) |
85 | else accept(invite, cb) |
86 | }) |
87 | } |
88 | }) |
89 | } |
90 | |
91 |
Built with git-ssb-web