secure-scuttlebot classic
at main 4.3 kB view raw
1 2var h = require('hyperscript') 3var pull = require('pull-stream') 4 5//var plugs = require('../plugs') 6// 7//var avatar_edit = plugs.first(exports.avatar_edit = []) 8//var invite_parse = plugs.first(exports.invite_parse = []) 9//var invite_accept = plugs.first(exports.invite_accept = []) 10//var sbot_progress = plugs.first(exports.sbot_progress = []) 11//var sbot_query = plugs.first(exports.sbot_query = []) 12//var avatar = plugs.first(exports.avatar = []) 13 14exports.needs = { 15 avatar: 'first', 16 avatar_edit: 'first', 17 invite_parse: 'first', 18 invite_accept: 'first', 19 sbot_progress: 'first', 20 sbot_query: 'first' 21} 22 23//maybe this could show the pubs, or 24//if someone locally follows you, 25//it could show the second degree pubs? 26 27//maybe show the network as animated graph? 28 29function followers_query (id) { 30 return [{$filter: { 31 value: {content: { 32 type: "contact", 33 contact: id, 34 following: true, 35// autofollow: true 36 }} 37 }}] 38} 39 40exports.create = function (api) { 41 42 var exports = {} 43 44 //test whether we are connected to the ssb network. 45 exports.setup_is_fresh_install = function (cb) { 46 //test by checking whether you have any friends following you? 47 pull( 48 api.sbot_query({query: followers_query(id), limit: 1, live: false}), 49 pull.collect(function (err, ary) { 50 cb(err, !!ary.length) 51 }) 52 ) 53 } 54 55 function invite_form () { 56 var accept = h('button', 'enter code', {disabled: true, onclick: function () { 57 api.invite_accept(input.value, function (msg) { 58 status.textContent = msg 59 }, function (err) { 60 if(err) { 61 accept.textContent = 'error:'+(err.message || err.stack || error.type) 62 console.error(err) 63 } 64 else { 65 input.value = '' 66 accept.textContent = 'success!' 67 } 68 }) 69 }}) 70 71 function parseInput () { 72 if(!input.value) { 73 accept.disabled = true 74 accept.textContent = 'enter code' 75 } 76 else if(!invite_parse(input.value)) { 77 accept.disabled = true 78 accept.textContent = 'invalid code' 79 } 80 else { 81 accept.disabled = false 82 accept.textContent = 'accept' 83 } 84 } 85 86 var input = h('input.wide', {placeholder: 'invite code', oninput: parseInput, onchange: parseInput}) 87 88 return h('div.invite-form.row', input, accept) 89 } 90 91 exports.progress_bar = function () { 92 var liquid = h('div.hyperprogress__liquid', '.') 93 var bar = h('div.hyperprogress__bar', liquid) 94 liquid.style.width = '0%' 95 96 pull( 97 api.sbot_progress(), 98 pull.drain(function (e) { 99 liquid.style.width = Math.round((e.progress/e.total)*100)+'%' 100 }) 101 ) 102 103 return bar 104 } 105 106 //show the first 5 followers, and how they join you to the network. 107 //so this will show if a local peer follows you. 108 109 //when you join the network, I want this to show as people follow you. 110 //that could be when a pub accepts the invite, or when a local peer accepts. 111 112 exports.setup_joined_network = function (id) { 113 var followers = h('div.column') 114 var label = h('label', 'not connected to a network') 115 var joined = h('div.setup__joined', label, followers) 116 117 pull( 118 api.sbot_query({query: followers_query(id), limit: 5, live: true, sync: false}), 119 pull.drain(function (follower) { 120 if(follower.sync) return 121 label.textContent = 'connected to network via...' 122 followers.appendChild( 123 api.avatar(follower.value.author, 'thumbnail') 124 ) 125 }) 126 ) 127 128 return joined 129 } 130 131 exports.screen_view = function (path) { 132 133 if(path !== 'setup') return 134 135 var id = require('../keys').id 136 137 //set up an avatar 138 139 140 var status = h('span') 141 var invite = h('input', {placeholder: 'invite code'}) 142 return h('div.scroller', h('div.scroller__wrapper', 143 h('h1', 'welcome to patchbay!'), 144 h('div', 145 'please choose avatar image and name', 146 api.avatar_edit(id) 147 ), 148 h('h2', 'join network'), 149 invite_form(), 150 //show avatars of anyone on the same local network. 151 //show realtime changes in your followers, especially for local. 152 153 exports.progress_bar(), 154 exports.setup_joined_network(require('../keys').id) 155 )) 156 } 157 158 return exports 159 160}