secure-scuttlebot classic
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 api.sbot_publish({
64 type: 'contact',
65 contact: data.key,
66 following: true,
67 }, cb)
68 }
69 })
70 })
71 },
72
73 screen_view: function (invite) {
74
75 var data = ref.parseInvite(invite)
76 if(!data) return
77
78 var progress = Progress(4)
79
80 //connect to server
81 //request follow
82 //post pub announce
83 //post follow pub
84 var div = h('div.column',
85 h('div',
86 "you have been invited to join:", h('br'),
87 h('code', data.invite)
88 ),
89 h('button', 'accept', {onclick: attempt}),
90 progress
91 )
92
93 function attempt () {
94 self.invite_accept(invite, function (message) {
95 progress.next(message)
96 }, function (err) {
97 if(err) return progress.fail(err)
98 progress.complete()
99 //check for redirect
100 var parts = location.hash.substring(1).split('#')
101
102 //TODO: handle in a consistent way with either hashrouting
103 //or with tabs...
104 if(parts[0] === data.invite)
105 location.hash = ''
106 else
107 console.log("NO REDIRECT")
108 })
109 }
110
111 // If we are in the browser, the user can click "accept" to trigger the invite.
112
113 return div
114 }
115 }
116}