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