Files: fec81386227bdfae59b98184c5a371cadbea9623 / modules / page / html / render / profile.js
10536 bytesRaw
1 | var nest = require('depnest') |
2 | var ref = require('ssb-ref') |
3 | var {h, when, computed, map, send, dictToCollection, resolve} = require('mutant') |
4 | var extend = require('xtend') |
5 | |
6 | exports.needs = nest({ |
7 | 'about.obs': { |
8 | name: 'first', |
9 | description: 'first', |
10 | names: 'first', |
11 | images: 'first', |
12 | color: 'first' |
13 | }, |
14 | 'blob.sync.url': 'first', |
15 | 'blob.html.input': 'first', |
16 | 'message.async.publish': 'first', |
17 | 'message.html.markdown': 'first', |
18 | 'about.html.image': 'first', |
19 | 'feed.html.rollup': 'first', |
20 | 'feed.pull.profile': 'first', |
21 | 'sbot.async.publish': 'first', |
22 | 'keys.sync.id': 'first', |
23 | 'sheet.display': 'first', |
24 | 'profile.obs.rank': 'first', |
25 | 'profile.sheet.edit': 'first', |
26 | 'app.navigate': 'first', |
27 | 'contact.obs': { |
28 | followers: 'first', |
29 | following: 'first', |
30 | blockers: 'first' |
31 | }, |
32 | 'contact.async.block': 'first', |
33 | 'contact.async.unblock': 'first', |
34 | 'intl.sync.i18n': 'first', |
35 | }) |
36 | exports.gives = nest('page.html.render') |
37 | |
38 | exports.create = function (api) { |
39 | const i18n = api.intl.sync.i18n |
40 | return nest('page.html.render', function profile (id) { |
41 | if (!ref.isFeed(id)) return |
42 | |
43 | var name = api.about.obs.name(id) |
44 | var description = api.about.obs.description(id) |
45 | var yourId = api.keys.sync.id() |
46 | var yourFollows = api.contact.obs.following(yourId) |
47 | var rawFollowers = api.contact.obs.followers(id) |
48 | var rawFollowing = api.contact.obs.following(id) |
49 | var friendsLoaded = computed([rawFollowers.sync, rawFollowing.sync], (...x) => x.every(Boolean)) |
50 | var { block, unblock } = api.contact.async |
51 | |
52 | var friends = computed([rawFollowing, rawFollowers], (following, followers) => { |
53 | return Array.from(following).filter(follow => followers.includes(follow)) |
54 | }) |
55 | |
56 | var following = computed([rawFollowing, friends], (following, friends) => { |
57 | return Array.from(following).filter(follow => !friends.includes(follow)) |
58 | }) |
59 | |
60 | var followers = computed([rawFollowers, friends], (followers, friends) => { |
61 | return Array.from(followers).filter(follower => !friends.includes(follower)) |
62 | }) |
63 | |
64 | var isFriends = computed([friends], function (friends) { |
65 | return friends.includes(yourId) |
66 | }) |
67 | |
68 | var followsYou = computed([following], function (followsYou) { |
69 | return followsYou.includes(yourId) |
70 | }) |
71 | |
72 | var youFollow = computed([yourFollows], function (youFollow) { |
73 | return youFollow.includes(id) |
74 | }) |
75 | |
76 | var blockers = api.contact.obs.blockers(id) |
77 | var youBlock = computed(blockers, function (blockers) { |
78 | return blockers.includes(yourId) |
79 | }) |
80 | |
81 | var names = computed([api.about.obs.names(id), yourFollows, rawFollowing], onlyEndorsed) |
82 | var images = computed([api.about.obs.images(id), yourFollows, rawFollowing], onlyEndorsed) |
83 | |
84 | var namePicker = h('div', {className: 'Picker'}, [ |
85 | map(dictToCollection(names), (item) => { |
86 | var isSelf = computed(item.value, (ids) => ids.includes(id)) |
87 | var isAssigned = computed(item.value, (ids) => ids.includes(yourId)) |
88 | return h('a.name', { |
89 | 'ev-click': () => { |
90 | if (!isAssigned()) { |
91 | assignName(id, resolve(item.key)) |
92 | } |
93 | }, |
94 | href: '#', |
95 | classList: [ |
96 | when(isSelf, '-self'), |
97 | when(isAssigned, '-assigned') |
98 | ], |
99 | title: nameList(when(isSelf, i18n('Self Assigned'), i18n('Assigned By')), item.value) |
100 | }, [ |
101 | item.key |
102 | ]) |
103 | }), |
104 | h('a -add', { |
105 | 'ev-click': () => { |
106 | rename(id) |
107 | }, |
108 | href: '#', |
109 | }, ['+']) |
110 | ]) |
111 | |
112 | var imagePicker = h('div', {className: 'Picker'}, [ |
113 | map(dictToCollection(images), (item) => { |
114 | var isSelf = computed(item.value, (ids) => ids.includes(id)) |
115 | var isAssigned = computed(item.value, (ids) => ids.includes(yourId)) |
116 | return h('a.name', { |
117 | 'ev-click': () => { |
118 | if (!isAssigned()) { |
119 | assignImage(id, resolve(item.key)) |
120 | } |
121 | }, |
122 | href: '#', |
123 | classList: [ |
124 | when(isSelf, '-self'), |
125 | when(isAssigned, '-assigned') |
126 | ], |
127 | title: nameList(when(isSelf, i18n('Self Assigned'), i18n('Assigned By')), item.value) |
128 | }, [ |
129 | h('img', { |
130 | className: 'Avatar', |
131 | style: { 'background-color': api.about.obs.color(id) }, |
132 | src: computed(item.key, api.blob.sync.url) |
133 | }) |
134 | ]) |
135 | }), |
136 | h('span.add', [ |
137 | api.blob.html.input(file => { |
138 | assignImage(id, file.link) |
139 | }, { |
140 | accept: 'image/*', |
141 | resize: { width: 500, height: 500 } |
142 | }) |
143 | ]) |
144 | ]) |
145 | |
146 | var prepend = h('header', {className: 'ProfileHeader'}, [ |
147 | h('div.image', api.about.html.image(id)), |
148 | h('div.main', [ |
149 | h('div.title', [ |
150 | h('h1', [name]), |
151 | h('div.meta', [ |
152 | when(id === yourId, [ |
153 | h('button', {'ev-click': api.profile.sheet.edit}, i18n('Edit Your Profile')) |
154 | ], [ |
155 | when(youBlock, [ |
156 | h('a.ToggleButton.-unblocking', { |
157 | 'href': '#', |
158 | 'title': i18n('Click to unblock'), |
159 | 'ev-click': () => unblock(id, console.log) |
160 | }, i18n('Blocked')) |
161 | ], [ |
162 | when(youFollow, |
163 | h('a.ToggleButton.-unsubscribe', { |
164 | 'href': '#', |
165 | 'title': i18n('Click to unfollow'), |
166 | 'ev-click': send(unfollow, id) |
167 | }, when(isFriends, i18n('Friends'), i18n('Following'))), |
168 | h('a.ToggleButton.-subscribe', { |
169 | 'href': '#', |
170 | 'ev-click': send(follow, id) |
171 | }, when(followsYou, i18n('Follow Back'), i18n('Follow'))) |
172 | ), |
173 | h('a.ToggleButton.-blocking', { |
174 | 'href': '#', |
175 | 'title': i18n('Block'), |
176 | 'ev-click': () => block(id, console.log) |
177 | }, i18n('Block')) |
178 | ]) |
179 | ]) |
180 | ]) |
181 | ]), |
182 | h('section -description', [ |
183 | computed(description, (text) => { |
184 | if (typeof text === 'string') { |
185 | return api.message.html.markdown(text) |
186 | } |
187 | }) |
188 | ]), |
189 | h('section', [ namePicker, imagePicker ]) |
190 | ]) |
191 | ]) |
192 | |
193 | var feedView = api.feed.html.rollup(api.feed.pull.profile(id), { |
194 | prepend, |
195 | displayFilter: (msg) => msg.value.author === id, |
196 | rootFilter: (msg) => !youBlock(), |
197 | bumpFilter: (msg) => msg.value.author === id |
198 | }) |
199 | |
200 | var container = h('div', {className: 'SplitView'}, [ |
201 | h('div.main', [ |
202 | feedView |
203 | ]), |
204 | h('div.side.-right', [ |
205 | h('button PrivateMessageButton', {'ev-click': () => api.app.navigate('/private', {compose: {to: id}})}, i18n('Send Private Message')), |
206 | when(friendsLoaded, |
207 | h('div', [ |
208 | renderContactBlock(i18n('Friends'), friends, yourFollows), |
209 | renderContactBlock(i18n('Followers'), followers, yourFollows), |
210 | renderContactBlock(i18n('Following'), following, yourFollows) |
211 | ]), |
212 | h('div', {className: 'Loading'}) |
213 | ) |
214 | ]) |
215 | ]) |
216 | |
217 | // refresh feed (to hide all posts) when blocked |
218 | youBlock(feedView.reload) |
219 | |
220 | container.pendingUpdates = feedView.pendingUpdates |
221 | container.reload = feedView.reload |
222 | return container |
223 | |
224 | // scoped |
225 | |
226 | function onlyEndorsed (values, yourFollows, theirFollows) { |
227 | return Object.keys(values).reduce((result, key) => { |
228 | var ids = values[key].filter(id => { |
229 | return yourFollows.includes(id) || theirFollows.includes(id) |
230 | }) |
231 | if (ids.length) { |
232 | result[key] = ids |
233 | } |
234 | return result |
235 | }, {}) |
236 | } |
237 | }) |
238 | |
239 | function renderContactBlock (title, profiles, yourFollows) { |
240 | profiles = api.profile.obs.rank(profiles) |
241 | return [ |
242 | when(computed(profiles, x => x.length), h('h2', title)), |
243 | h('div', { |
244 | classList: 'ProfileList' |
245 | }, [ |
246 | map(profiles, (id) => { |
247 | var following = computed(yourFollows, f => f.includes(id)) |
248 | return h('a.profile', { |
249 | href: id, |
250 | classList: [ |
251 | when(following, '-following') |
252 | ] |
253 | }, [ |
254 | h('div.avatar', [api.about.html.image(id)]), |
255 | h('div.main', [ |
256 | h('div.name', [ api.about.obs.name(id) ]) |
257 | ]) |
258 | ]) |
259 | }, { |
260 | maxTime: 5, |
261 | idle: true |
262 | }) |
263 | ]) |
264 | ] |
265 | } |
266 | |
267 | function follow (id) { |
268 | api.sbot.async.publish({ |
269 | type: 'contact', |
270 | contact: id, |
271 | following: true |
272 | }) |
273 | } |
274 | |
275 | function unfollow (id) { |
276 | api.sbot.async.publish({ |
277 | type: 'contact', |
278 | contact: id, |
279 | following: false |
280 | }) |
281 | } |
282 | |
283 | function assignImage (id, image) { |
284 | api.message.async.publish({ |
285 | type: 'about', |
286 | about: id, |
287 | image |
288 | }) |
289 | } |
290 | |
291 | function assignName (id, name) { |
292 | api.message.async.publish({ |
293 | type: 'about', |
294 | about: id, |
295 | name |
296 | }) |
297 | } |
298 | |
299 | function rename (id) { |
300 | api.sheet.display(close => { |
301 | var currentName = api.about.obs.name(id) |
302 | var input = h('input', { |
303 | style: {'font-size': '150%'}, |
304 | value: currentName() |
305 | }) |
306 | setTimeout(() => { |
307 | input.focus() |
308 | input.select() |
309 | }, 5) |
310 | return { |
311 | content: h('div', { |
312 | style: { |
313 | padding: '20px', |
314 | 'text-align': 'center' |
315 | } |
316 | }, [ |
317 | h('h2', { |
318 | style: { |
319 | 'font-weight': 'normal' |
320 | } |
321 | }, [i18n('What whould you like to call '), h('strong', [currentName]), '?']), |
322 | input |
323 | ]), |
324 | footer: [ |
325 | h('button -save', { |
326 | 'ev-click': () => { |
327 | if (input.value.trim() && input.value !== currentName()) { |
328 | // no confirm |
329 | api.sbot.async.publish({ |
330 | type: 'about', |
331 | about: id, |
332 | name: input.value.trim() |
333 | }) |
334 | } |
335 | close() |
336 | } |
337 | }, i18n('Confirm')), |
338 | h('button -cancel', { |
339 | 'ev-click': close |
340 | }, i18n('Cancel')) |
341 | ] |
342 | } |
343 | }) |
344 | } |
345 | |
346 | function nameList (prefix, ids) { |
347 | var items = map(ids, api.about.obs.name) |
348 | return computed([prefix, items], (prefix, names) => { |
349 | return (prefix ? (prefix + '\n') : '') + names.map((n) => `- ${n}`).join('\n') |
350 | }) |
351 | } |
352 | } |
353 |
Built with git-ssb-web