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