Files: cc06e263225583cc9a1c6958063f600fb97d8968 / modules_basic / invite.js
2967 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 | h('p', {innerHTML: 'If the page doesn\'t redirect, navigate to <a href="/">Home</a>'}) |
83 | ) |
84 | |
85 | function attempt () { |
86 | exports.invite_accept(invite, function (message) { |
87 | progress.next(message) |
88 | }, function (err) { |
89 | if(err) return progress.fail(err) |
90 | progress.complete() |
91 | //check for redirect |
92 | var parts = location.hash.substring(1).split('#') |
93 | |
94 | //TODO: handle in a consistent way with either hashrouting |
95 | //or with tabs... |
96 | if(parts[0] === data.invite) |
97 | location.hash = data.redirect |
98 | else |
99 | console.log("NO REDIRECT") |
100 | }) |
101 | } |
102 | |
103 | // If we are in the browser, |
104 | // and do not already have a remote set, automatically trigger the invite. |
105 | if(process.title == 'browser' && !localStorage.remote) attempt() |
106 | |
107 | return div |
108 | } |
109 | |
110 | |
111 | |
112 | |
113 | |
114 |
Built with git-ssb-web