Files: 1db0c00d5c1029322455ac21c2ef002f981fa59c / modules / invite.js
2526 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 | var plugs = require('../plugs') |
10 | var sbot_publish = plugs.first(exports.sbot_publish = []) |
11 | |
12 | |
13 | //check that invite is |
14 | // ws:...~shs:key:seed |
15 | function parseMultiServerInvite (invite) { |
16 | var parts = invite.split('~') |
17 | .map(function (e) { return e.split(':') }) |
18 | |
19 | if(parts.length !== 2) return null |
20 | if(!/^(net|wss?)$/.test(parts[0][0])) return null |
21 | if(parts[1][0] !== 'shs') return null |
22 | if(parts[1].length !== 3) return null |
23 | var p2 = invite.split(':') |
24 | p2.pop() |
25 | |
26 | return { |
27 | invite: invite, |
28 | remote: p2.join(':'), |
29 | } |
30 | } |
31 | |
32 | exports.screen_view = function (invite) { |
33 | |
34 | var data = parseMultiServerInvite(invite) |
35 | if(!data) return |
36 | |
37 | var progress = Progress(4) |
38 | |
39 | //connect to server |
40 | //request follow |
41 | //post pub announce |
42 | //post follow pub |
43 | var div = h('div.column', |
44 | h('div', |
45 | "you have been invited to join:", h('br'), |
46 | h('code', invite) |
47 | ), |
48 | h('button', 'accept', {onclick: attempt}), |
49 | progress |
50 | ) |
51 | |
52 | function attempt () { |
53 | progress.reset().next('connecting...') |
54 | |
55 | ssbClient(null, { |
56 | remote: invite, |
57 | manifest: { invite: {use: 'async'}, getAddress: 'async' } |
58 | }, function (err, sbot) { |
59 | if(err) return progress.fail(err) |
60 | progress.next('requesting follow...') |
61 | |
62 | sbot.invite.use({feed: id}, function (err, msg) { |
63 | if(err) return progress.fail(err) |
64 | progress.next('following...') |
65 | |
66 | //remove the seed from the shs address. |
67 | //then it's correct address. |
68 | //this should make the browser connect to this as remote. |
69 | //we don't want to do this if when using this locally, though. |
70 | if(process.title === 'browser') |
71 | localStorage.remote = data.remote |
72 | |
73 | sbot_publish({ |
74 | type: 'contact', |
75 | contact: sbot.id, |
76 | following: true, |
77 | }, function (err) { |
78 | if(err) return progress.fail(err) |
79 | progress.complete() |
80 | //check for redirect |
81 | var parts = location.hash.substring(1).split('#') |
82 | //TODO: handle in a consistent way with either hashrouting |
83 | //or with tabs... |
84 | if(parts[0] === invite) location.hash = '#' |
85 | }) |
86 | }) |
87 | }) |
88 | } |
89 | |
90 | // If we are in the browser, |
91 | // and do not already have a remote set, automatically trigger the invite. |
92 | |
93 | if(process.title == 'browser' && !localStorage.remote) attempt() |
94 | |
95 | return div |
96 | } |
97 | |
98 | |
99 | |
100 | |
101 | |
102 |
Built with git-ssb-web