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