git ssb

16+

Dominic / patchbay



Tree: e7baaa19e8b15d03ad3d980070f51aafa98f0f86

Files: e7baaa19e8b15d03ad3d980070f51aafa98f0f86 / modules_basic / invite.js

3333 bytesRaw
1
2var ref = require('ssb-ref')
3var ssbClient = require('ssb-client')
4var id = require('../keys').id
5var h = require('hyperscript')
6
7var Progress = require('hyperprogress')
8
9var plugs = require('../plugs')
10var sbot_publish = plugs.first(exports.sbot_publish = [])
11var follower_of = plugs.first(exports.follower_of = [])
12
13
14//check that invite is
15// ws:...~shs:key:seed
16function parseMultiServerInvite (invite) {
17 var redirect = invite.split('#')
18 if(!redirect.length) return null
19
20 var parts = redirect[0].split('~')
21 .map(function (e) { return e.split(':') })
22
23 if(parts.length !== 2) return null
24 if(!/^(net|wss?)$/.test(parts[0][0])) return null
25 if(parts[1][0] !== 'shs') return null
26 if(parts[1].length !== 3) return null
27 var p2 = invite.split(':')
28 p2.pop()
29
30 return {
31 invite: redirect[0],
32 remote: p2.join(':'),
33 key: '@'+parts[1][1]+'.ed25519',
34 redirect: '#' + redirect.slice(1).join('#')
35 }
36}
37
38exports.invite_parse = function (invite) {
39 return parseMultiServerInvite(invite)
40}
41
42exports.invite_accept = function (invite, onProgress, cb) {
43 var data = exports.invite_parse(invite)
44 if(!data) return cb(new Error('not a valid invite code:' + invite))
45
46 onProgress('connecting...')
47
48 ssbClient(null, {
49 remote: data.invite,
50 manifest: { invite: {use: 'async'}, getAddress: 'async' }
51 }, function (err, sbot) {
52 if(err) return cb(err)
53 onProgress('requesting follow...')
54 console.log(sbot)
55 sbot.invite.use({feed: id}, function (err, msg) {
56
57 //if they already follow us, just check we actually follow them.
58 if(err) follower_of(id, data.key, function (_err, follows) {
59 if(follows) cb(err)
60 else next()
61 })
62 else next()
63
64 function next () {
65 onProgress('following...')
66
67 //remove the seed from the shs address.
68 //then it's correct address.
69 //this should make the browser connect to this as remote.
70 //we don't want to do this if when using this locally, though.
71 if(process.title === 'browser')
72 localStorage.remote = data.remote
73
74 sbot_publish({
75 type: 'contact',
76 contact: data.key,
77 following: true,
78 }, cb)
79 }
80 })
81 })
82}
83
84exports.screen_view = function (invite) {
85
86 var data = parseMultiServerInvite(invite)
87 if(!data) return
88
89 var progress = Progress(4)
90
91 //connect to server
92 //request follow
93 //post pub announce
94 //post follow pub
95 var div = h('div.column',
96 h('div',
97 "you have been invited to join:", h('br'),
98 h('code', data.invite)
99 ),
100 h('button', 'accept', {onclick: attempt}),
101 progress
102 )
103
104 function attempt () {
105 exports.invite_accept(invite, function (message) {
106 progress.next(message)
107 }, function (err) {
108 if(err) return progress.fail(err)
109 progress.complete()
110 //check for redirect
111 var parts = location.hash.substring(1).split('#')
112
113 //TODO: handle in a consistent way with either hashrouting
114 //or with tabs...
115 if(parts[0] === data.invite)
116 location.hash = data.redirect
117 else
118 console.log("NO REDIRECT")
119 })
120 }
121
122 // If we are in the browser,
123 // and do not already have a remote set, automatically trigger the invite.
124 if(process.title == 'browser' && !localStorage.remote) attempt()
125
126 return div
127}
128
129

Built with git-ssb-web