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