git ssb

16+

Dominic / patchbay



Tree: 80807add560dfbbd3fe40aa17eac62b859624063

Files: 80807add560dfbbd3fe40aa17eac62b859624063 / modules_basic / invite.js

3534 bytesRaw
1'use strict'
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
9exports.needs = {
10 sbot_publish: 'first',
11 sbot_gossip_connect: 'first',
12 follower_of: 'first',
13 invite_parse: 'first',
14}
15
16exports.gives = {
17 invite_parse: true,
18 invite_accept: true,
19 screen_view: true
20}
21
22exports.create = function (api) {
23 var self
24 return self = {
25 invite_parse: function (invite) {
26 return ref.parseInvite(invite)
27 },
28
29 invite_accept: function (invite, onProgress, cb) {
30 var data = self.invite_parse(invite)
31 if(!data) return cb(new Error('not a valid invite code:' + invite))
32
33 onProgress('Connecting...')
34
35 api.sbot_gossip_connect(data.remote, function (err) {
36 if(err) console.log(err)
37 })
38
39 ssbClient(null, {
40 remote: data.invite,
41 manifest: { invite: {use: 'async'}, getAddress: 'async' }
42 }, function (err, sbot) {
43 if(err) return cb(err)
44 onProgress('Requesting follow...')
45 console.log(sbot)
46 sbot.invite.use({feed: id}, function (err, msg) {
47
48 //if they already follow us, just check we actually follow them.
49 if(err) api.follower_of(id, data.key, function (_err, follows) {
50 if(follows) { location.hash = '' }
51 else next()
52 })
53 else next()
54
55 function next () {
56 onProgress('Following...')
57
58 //remove the seed from the shs address.
59 //then it's correct address.
60 //this should make the browser connect to this as remote.
61 //we don't want to do this if when using this locally, though.
62 if(process.title === 'browser')
63 localStorage.remote = data.remote
64
65 api.sbot_publish({
66 type: 'contact',
67 contact: data.key,
68 following: true,
69 }, cb)
70 }
71 })
72 })
73 },
74
75 screen_view: function (invite) {
76
77 var data = ref.parseInvite(invite)
78 if(!data) return
79
80 var progress = Progress(4)
81
82 //connect to server
83 //request follow
84 //post pub announce
85 //post follow pub
86 var div = h('div.column.scroller__wrapper',
87 h('div.column.scroller__content', {style: 'margin-top: 25%;'},
88 h('h1', {innerHTML: 'The <a href="https://scuttlebot.io">Secure Scuttlebutt</a> Lite Client'}),
89 h('p', "You've been invited to join:"),
90 h('p', h('code', data.invite))
91 ),
92 h('p', h('button', 'Accept', {onclick: attempt})),
93 progress,
94 h('p', "Once you're in, give yourself a name and photo. And don't forget to say 'Hello!'")
95 )
96
97 function attempt () {
98 self.invite_accept(invite, function (message) {
99 progress.next(message)
100 }, function (err) {
101 if(err) return progress.fail(err)
102 progress.complete()
103 //check for redirect
104 var parts = location.hash.substring(1).split('#')
105
106 //TODO: handle in a consistent way with either hashrouting
107 //or with tabs...
108 if(parts[0] === data.invite)
109 location.hash = ''
110 else
111 console.log("NO REDIRECT")
112 })
113 }
114
115 // If we are in the browser,
116 // and do not already have a remote set, automatically trigger the invite.
117 if(process.title == 'browser' && !localStorage.remote) attempt()
118
119 return div
120 }
121 }
122}
123
124

Built with git-ssb-web