git ssb

16+

Dominic / patchbay



Tree: dab8336e2c9b7ae4d3e407ce7c8059bea3f4e566

Files: dab8336e2c9b7ae4d3e407ce7c8059bea3f4e566 / modules_basic / invite.js

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

Built with git-ssb-web