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