Files: 8dc25143a3f37f1618f7a92b3c46dec512b43bfd / modules / invite.js
3549 bytesRaw
1 | |
2 | var ref = require('ssb-ref') |
3 | var ssbClient = require('ssb-client') |
4 | var id = require('../keys').id |
5 | var h = require('hyperscript') |
6 | |
7 | var Progress = require('hyperprogress') |
8 | |
9 | exports.needs = { |
10 | sbot_publish: 'first', |
11 | sbot_gossip_connect: 'first', |
12 | follower_of: 'first', |
13 | invite_parse: 'first', |
14 | } |
15 | |
16 | exports.gives = { |
17 | invite_parse: true, |
18 | invite_accept: true, |
19 | screen_view: true |
20 | } |
21 | |
22 | exports.create = function (api) { |
23 | var self |
24 | return self = { |
25 | invite_parse: function (invite) { |
26 | return ref.parseInvite(invite) |
27 | }, |
28 | |
29 | invite_accept: function (invite, onProgress, cb) { |
30 | var data = self.invite_parse(invite) |
31 | if(!data) return cb(new Error('not a valid invite code:' + invite)) |
32 | |
33 | onProgress('Connecting...') |
34 | |
35 | api.sbot_gossip_connect(data.remote, function (err) { |
36 | if(err) console.log(err) |
37 | }) |
38 | |
39 | ssbClient(null, { |
40 | remote: data.invite, |
41 | manifest: { invite: {use: 'async'}, getAddress: 'async' } |
42 | }, function (err, sbot) { |
43 | if(err) return cb(err) |
44 | onProgress('Asking pub to follow...') |
45 | console.log(sbot) |
46 | sbot.invite.use({feed: id}, function (err, msg) { |
47 | |
48 | //if they already follow us, just check we actually follow them. |
49 | if(err) api.follower_of(id, data.key, function (_err, follows) { |
50 | if(follows) { location.hash = '' } |
51 | else next() |
52 | }) |
53 | else next() |
54 | |
55 | function next () { |
56 | onProgress('Following the pub...') |
57 | |
58 | //remove the seed from the shs address. |
59 | //then it's correct address. |
60 | //this should make the browser connect to this as remote. |
61 | //we don't want to do this if when using this locally, though. |
62 | if(process.title === 'browser') |
63 | localStorage.remote = data.remote |
64 | |
65 | api.sbot_publish({ |
66 | type: 'contact', |
67 | contact: data.key, |
68 | following: true, |
69 | }, cb) |
70 | } |
71 | }) |
72 | }) |
73 | }, |
74 | |
75 | screen_view: function (invite) { |
76 | |
77 | var data = ref.parseInvite(invite) |
78 | if(!data) return |
79 | |
80 | var progress = Progress(4) |
81 | |
82 | //connect to server |
83 | //request follow |
84 | //post pub announce |
85 | //post follow pub |
86 | var div = h('div.column.scroller', {style: 'overflow: auto;'}, |
87 | h('div.column.scroller__wrapper', |
88 | h('div.column.scroller__content', {style: 'margin-top: 25%;'}, |
89 | h('h1', {innerHTML: 'The <a href="https://scuttlebot.io">Secure Scuttlebutt</a> Lite Client'}), |
90 | h('p', "You've been invited to join:"), |
91 | h('p', h('code', data.invite)) |
92 | ), |
93 | h('p', h('button', 'Accept', {onclick: attempt})), |
94 | progress, |
95 | h('p', "Give this step a moment to finish, once we're done you can pick a name."), |
96 | h('p', {innerHTML: 'If you have a saved key and remote, <a href="/#Key">import them here</a>.'}) |
97 | ) |
98 | ) |
99 | function attempt () { |
100 | self.invite_accept(invite, function (message) { |
101 | progress.next(message) |
102 | }, function (err) { |
103 | if(err) return progress.fail(err) |
104 | progress.complete() |
105 | //check for redirect |
106 | var parts = location.hash.substring(1).split('#') |
107 | |
108 | //TODO: handle in a consistent way with either hashrouting |
109 | //or with tabs... |
110 | if(parts[0] === data.invite) |
111 | location.hash = 'Identify' |
112 | else |
113 | console.log("NO REDIRECT") |
114 | }) |
115 | } |
116 | return div |
117 | } |
118 | } |
119 | } |
120 | |
121 |
Built with git-ssb-web