git ssb

16+

Dominic / patchbay



Tree: 04d2567aedcab2bcc20d320aacb021569be10cc3

Files: 04d2567aedcab2bcc20d320aacb021569be10cc3 / modules_basic / setup.js

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

Built with git-ssb-web