git ssb

16+

Dominic / patchbay



Tree: 9e65f9951f21778ee286834004367aa2b46bc882

Files: 9e65f9951f21778ee286834004367aa2b46bc882 / modules / invite.js

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

Built with git-ssb-web