git ssb

16+

Dominic / patchbay



Tree: b1fc586afe1b02db034fa275db35c84c915ff42f

Files: b1fc586afe1b02db034fa275db35c84c915ff42f / modules_basic / invite.js

3455 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) cb(err)
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',
92 h('div',
93 "you have been invited to join:", h('br'),
94 h('code', data.invite)
95 ),
96 h('button', 'accept', {onclick: attempt}),
97 progress
98 )
99
100 function attempt () {
101 self.invite_accept(invite, function (message) {
102 progress.next(message)
103 }, function (err) {
104 if(err) return progress.fail(err)
105 progress.complete()
106 //check for redirect
107 var parts = location.hash.substring(1).split('#')
108
109 //TODO: handle in a consistent way with either hashrouting
110 //or with tabs...
111 if(parts[0] === data.invite)
112 location.hash = ''
113 else
114 console.log("NO REDIRECT")
115 })
116 }
117
118 // If we are in the browser,
119 // and do not already have a remote set, automatically trigger the invite.
120 if(process.title == 'browser' && !localStorage.remote) attempt()
121
122 return div
123 }
124 }
125}
126
127

Built with git-ssb-web