Files: afe7de9580840925af61711b5ab0d6c57d1131a4 / modules / invite.js
3658 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__wrapper', |
87 | h('div.column.scroller__content', {style: 'margin-top: 25%;'}, |
88 | h('h1', {innerHTML: 'The <a href="https://scuttlebot.io">Secure Scuttlebutt</a> Lite Client'}), |
89 | h('p', "You've been invited to join:"), |
90 | h('p', h('code', data.invite)) |
91 | ), |
92 | h('p', h('button', 'Accept', {onclick: attempt})), |
93 | progress, |
94 | h('p', "Give this step a moment to finish, once we're done you can pick a name."), |
95 | h('p', {innerHTML: 'If you have a saved key and remote, <a href="/#Your Key">import them here</a>.'}) |
96 | ) |
97 | |
98 | function attempt () { |
99 | self.invite_accept(invite, function (message) { |
100 | progress.next(message) |
101 | }, function (err) { |
102 | if(err) return progress.fail(err) |
103 | progress.complete() |
104 | //check for redirect |
105 | var parts = location.hash.substring(1).split('#') |
106 | |
107 | //TODO: handle in a consistent way with either hashrouting |
108 | //or with tabs... |
109 | if(parts[0] === data.invite) |
110 | location.hash = 'Identify' |
111 | else |
112 | console.log("NO REDIRECT") |
113 | }) |
114 | } |
115 | |
116 | // If we are in the browser, |
117 | // and do not already have a remote set, automatically trigger the invite. |
118 | // if(process.title == 'browser' && !localStorage.remote) attempt() |
119 | |
120 | return div |
121 | } |
122 | } |
123 | } |
124 | |
125 |
Built with git-ssb-web