Files: ca58f0e7a14d980e607b0d0886ec4b0ef76b9dc1 / modules / invite.js
2120 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 | "invite to:", h('br'), |
46 | h('code', invite), |
47 | h('button', 'accept', {onclick: function (ev) { |
48 | attempt() |
49 | }}) |
50 | ), |
51 | progress |
52 | ) |
53 | |
54 | function attempt () { |
55 | progress.reset().next('connecting...') |
56 | |
57 | ssbClient(null, { |
58 | remote: invite, |
59 | manifest: { invite: {use: 'async'}, getAddress: 'async' } |
60 | }, function (err, sbot) { |
61 | if(err) return progress.fail(err) |
62 | progress.next('requesting follow...') |
63 | |
64 | sbot.invite.use({feed: id}, function (err, msg) { |
65 | if(err) return progress.fail(err) |
66 | progress.next('following...') |
67 | |
68 | //remove the seed from the shs address. |
69 | //then it's correct address. |
70 | //this should make the browser connect to this as remote. |
71 | //we don't want to do this if when using this locally, though. |
72 | if(process.title === 'browser') |
73 | localStorage.remote = data.remote |
74 | |
75 | sbot_publish({ |
76 | type: 'contact', |
77 | contact: sbot.id, |
78 | following: true, |
79 | }, function (err) { |
80 | if(err) return progress.fail(err) |
81 | progress.complete() |
82 | }) |
83 | |
84 | }) |
85 | }) |
86 | } |
87 | |
88 | return div |
89 | } |
90 | |
91 |
Built with git-ssb-web