Files: 77570f8a228dbe5f1d398a92623d4c25675654ec / modules / page / html / render / profile.js
13217 bytesRaw
1 | var nest = require('depnest') |
2 | var ref = require('ssb-ref') |
3 | var {h, when, computed, map, send, dictToCollection, resolve} = require('mutant') |
4 | |
5 | exports.needs = nest({ |
6 | 'about.obs': { |
7 | name: 'first', |
8 | description: 'first', |
9 | names: 'first', |
10 | images: 'first', |
11 | color: 'first' |
12 | }, |
13 | 'blob.sync.url': 'first', |
14 | 'blob.html.input': 'first', |
15 | 'message.async.publish': 'first', |
16 | 'message.html.markdown': 'first', |
17 | 'message.sync.root': '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.sheet.edit': 'first', |
25 | 'app.navigate': 'first', |
26 | 'profile.obs.contact': 'first', |
27 | 'profile.obs.recentlyUpdated': 'first', |
28 | 'contact.html.followToggle': 'first', |
29 | 'intl.sync.i18n': 'first', |
30 | 'intl.sync.i18n_n': 'first', |
31 | 'sheet.profiles': 'first' |
32 | }) |
33 | exports.gives = nest('page.html.render') |
34 | |
35 | exports.create = function (api) { |
36 | const i18n = api.intl.sync.i18n |
37 | const plural = api.intl.sync.i18n_n |
38 | return nest('page.html.render', function profile (id) { |
39 | if (!ref.isFeed(id)) return |
40 | var yourId = api.keys.sync.id() |
41 | var name = api.about.obs.name(id) |
42 | var description = api.about.obs.description(id) |
43 | var contact = api.profile.obs.contact(id) |
44 | var recent = api.profile.obs.recentlyUpdated() |
45 | |
46 | var friends = computed([contact.following, contact.followers], (following, followers) => { |
47 | return Array.from(following).filter(follow => followers.includes(follow)) |
48 | }) |
49 | |
50 | var following = computed([contact.following, friends], (following, friends) => { |
51 | return following.filter(follow => !friends.includes(follow)) |
52 | }) |
53 | |
54 | var followers = computed([contact.followers, friends], (followers, friends) => { |
55 | return followers.filter(follower => !friends.includes(follower)) |
56 | }) |
57 | |
58 | var names = computed([api.about.obs.names(id), contact.yourFollowing, contact.following, yourId, id], filterByValues) |
59 | var images = computed([api.about.obs.images(id), contact.yourFollowing, contact.following, yourId, id], filterByValues) |
60 | |
61 | var namePicker = h('div', {className: 'Picker'}, [ |
62 | map(dictToCollection(names), (item) => { |
63 | var isSelf = computed(item.value, (ids) => ids.includes(id)) |
64 | var isAssigned = computed(item.value, (ids) => ids.includes(yourId)) |
65 | return h('a.name', { |
66 | 'ev-click': () => { |
67 | if (!isAssigned()) { |
68 | assignName(id, resolve(item.key)) |
69 | } |
70 | }, |
71 | href: '#', |
72 | classList: [ |
73 | when(isSelf, '-self'), |
74 | when(isAssigned, '-assigned') |
75 | ], |
76 | title: nameList(when(isSelf, i18n('Self Assigned'), i18n('Assigned By')), item.value) |
77 | }, [ |
78 | item.key |
79 | ]) |
80 | }), |
81 | h('a -add', { |
82 | 'ev-click': () => { |
83 | rename(id) |
84 | }, |
85 | href: '#' |
86 | }, ['+']) |
87 | ]) |
88 | |
89 | var imagePicker = h('div', {className: 'Picker'}, [ |
90 | map(dictToCollection(images), (item) => { |
91 | var isSelf = computed(item.value, (ids) => ids.includes(id)) |
92 | var isAssigned = computed(item.value, (ids) => ids.includes(yourId)) |
93 | return h('a.name', { |
94 | 'ev-click': () => { |
95 | if (!isAssigned()) { |
96 | assignImage(id, resolve(item.key)) |
97 | } |
98 | }, |
99 | href: '#', |
100 | classList: [ |
101 | when(isSelf, '-self'), |
102 | when(isAssigned, '-assigned') |
103 | ], |
104 | title: nameList(when(isSelf, i18n('Self Assigned'), i18n('Assigned By')), item.value) |
105 | }, [ |
106 | h('img', { |
107 | className: 'Avatar', |
108 | style: { 'background-color': api.about.obs.color(id) }, |
109 | src: computed(item.key, api.blob.sync.url) |
110 | }) |
111 | ]) |
112 | }), |
113 | h('span.add', [ |
114 | api.blob.html.input(file => { |
115 | assignImage(id, file.link) |
116 | }, { |
117 | accept: 'image/*', |
118 | resize: { width: 500, height: 500 } |
119 | }) |
120 | ]) |
121 | ]) |
122 | |
123 | var prepend = h('header', {className: 'ProfileHeader'}, [ |
124 | h('div.image', api.about.html.image(id)), |
125 | h('div.main', [ |
126 | h('div.title', [ |
127 | h('h1', [name]), |
128 | h('div.meta', [ |
129 | when(id === yourId, [ |
130 | h('button', {'ev-click': api.profile.sheet.edit}, i18n('Edit Your Profile')) |
131 | ], [ |
132 | api.contact.html.followToggle(id) |
133 | ]) |
134 | ]) |
135 | ]), |
136 | h('section -publicKey', [ |
137 | h('pre', {title: i18n('Public key for this profile')}, id) |
138 | ]), |
139 | |
140 | when(contact.notFollowing, [ |
141 | when(contact.blockingFriendsCount, h('section -blockWarning', [ |
142 | h('a', { |
143 | href: '#', |
144 | 'ev-click': send(displayBlockingFriends, contact.blockingFriends) |
145 | }, [ |
146 | '⚠️ ', computed(['This person is blocked by %s of your friends.', contact.blockingFriendsCount], plural) |
147 | ]) |
148 | ])), |
149 | |
150 | when(contact.noIncoming, |
151 | h('section -distanceWarning', [ |
152 | h('h1', i18n(`You don't follow anyone who follows this person`)), |
153 | h('p', i18n('You might not be seeing their latest messages. You could try joining a pub that they are a member of.')), |
154 | when(contact.hasOutgoing, |
155 | h('p', i18n('However, since they follow someone that follows you, they should be able to see your posts.')), |
156 | h('p', i18n(`They might not be able to see your posts either.`)) |
157 | ) |
158 | ]), |
159 | when(contact.noOutgoing, |
160 | h('section -distanceWarning', [ |
161 | h('h1', i18n('This person does not follow anyone that follows you')), |
162 | h('p', i18n('They might not receive your private messages or replies. You could try joining a pub that they are a member of.')), |
163 | h('p', i18n('However, since you follow someone that follows them, you should be able to see their latest posts.')) |
164 | ]), |
165 | when(contact.mutualFriendsCount, |
166 | h('section -mutualFriends', [ |
167 | h('a', { |
168 | href: '#', |
169 | title: nameList(i18n('Mutual Friends'), contact.mutualFriends), |
170 | 'ev-click': send(displayMutualFriends, contact.mutualFriends) |
171 | }, [ |
172 | '👥 ', computed(['You share %s mutual friends with this person.', contact.mutualFriendsCount], plural) |
173 | ]) |
174 | ]), |
175 | h('section -mutualFriends', [ |
176 | h('a', { |
177 | href: '#', |
178 | title: nameList(i18n('Followed by'), contact.incomingVia), |
179 | 'ev-click': send(displayFollowedBy, contact.incomingVia) |
180 | }, [ |
181 | '👥 ', computed(['You follow %s people that follow this person.', contact.incomingViaCount], plural) |
182 | ]) |
183 | ]) |
184 | ) |
185 | ) |
186 | ) |
187 | ]), |
188 | |
189 | h('section -description', [ |
190 | computed(description, (text) => { |
191 | if (typeof text === 'string') { |
192 | return api.message.html.markdown(text) |
193 | } |
194 | }) |
195 | ]), |
196 | h('section', [ namePicker, imagePicker ]) |
197 | ]) |
198 | ]) |
199 | |
200 | var feedView = api.feed.html.rollup(api.feed.pull.profile(id), { |
201 | prepend, |
202 | compactFilter: (msg) => msg.value.author !== id, // show root context messages smaller |
203 | displayFilter: (msg) => msg.value.author === id, |
204 | rootFilter: (msg) => !contact.youBlock() && !api.message.sync.root(msg), |
205 | bumpFilter: (msg) => msg.value.author === id |
206 | }) |
207 | |
208 | var container = h('div', {className: 'SplitView'}, [ |
209 | h('div.main', [ |
210 | feedView |
211 | ]), |
212 | h('div.side.-right', [ |
213 | h('button PrivateMessageButton', {'ev-click': () => api.app.navigate('/private', {compose: {to: id}})}, i18n('Send Private Message')), |
214 | when(contact.sync, |
215 | h('div', [ |
216 | renderContactBlock(i18n('Friends'), onlyRecent(friends, 10), contact.yourFollowing, friends), |
217 | renderContactBlock(i18n('Followers'), onlyFollowing(followers, 10), contact.yourFollowing, followers), |
218 | renderContactBlock(i18n('Following'), onlyRecent(following, 10), contact.yourFollowing, following), |
219 | renderContactBlock(i18n('Blocked by'), contact.blockingFriends, contact.yourFollowing) |
220 | ]), |
221 | h('div', {className: 'Loading'}) |
222 | ) |
223 | ]) |
224 | ]) |
225 | |
226 | // refresh feed (to hide all posts) when blocked |
227 | contact.youBlock(feedView.reload) |
228 | |
229 | container.pendingUpdates = feedView.pendingUpdates |
230 | container.reload = feedView.reload |
231 | return container |
232 | |
233 | // scoped |
234 | |
235 | function onlyFollowing (ids, max) { |
236 | return computed([recent, ids, contact.yourFollowing], (a, b, c) => { |
237 | var result = a.filter(x => b.includes(x) && c.includes(x)) |
238 | if (result.length === 0 && a.length) { |
239 | // fallback to just recent |
240 | result = a.filter(x => b.includes(x)) |
241 | } |
242 | if (max) { |
243 | return result.slice(0, max) |
244 | } else { |
245 | return result |
246 | } |
247 | }) |
248 | } |
249 | |
250 | function onlyRecent (ids, max) { |
251 | return computed([recent, ids], (a, b) => { |
252 | var result = a.filter(x => b.includes(x)) |
253 | if (max) { |
254 | return result.slice(0, max) |
255 | } else { |
256 | return result |
257 | } |
258 | }) |
259 | } |
260 | }) |
261 | |
262 | function displayFollowedBy (profiles) { |
263 | api.sheet.profiles(profiles, i18n('Followed by')) |
264 | } |
265 | |
266 | function displayMutualFriends (profiles) { |
267 | api.sheet.profiles(profiles, i18n('Mutual Friends')) |
268 | } |
269 | |
270 | function displayBlockingFriends (profiles) { |
271 | api.sheet.profiles(profiles, i18n('Blocked by')) |
272 | } |
273 | |
274 | function renderContactBlock (title, profiles, yourFollowing, fullList) { |
275 | var moreCount = computed([profiles, fullList], (a, b) => a && b && a.length < b.length && b.length - a.length) |
276 | return [ |
277 | when(computed([profiles, fullList], (a, b) => a.length || (b && b.length)), h('h2', title)), |
278 | h('div', { |
279 | classList: 'ProfileList' |
280 | }, [ |
281 | map(profiles, (id) => { |
282 | var following = computed(yourFollowing, f => f.includes(id)) |
283 | return h('a.profile', { |
284 | href: id, |
285 | classList: [ |
286 | when(following, '-following') |
287 | ] |
288 | }, [ |
289 | h('div.avatar', [api.about.html.image(id)]), |
290 | h('div.main', [ |
291 | h('div.name', [ api.about.obs.name(id) ]) |
292 | ]) |
293 | ]) |
294 | }, { |
295 | maxTime: 5, |
296 | idle: true |
297 | }), |
298 | when(moreCount, |
299 | h('a.profile -more', { |
300 | href: '#', |
301 | 'ev-click': function () { |
302 | api.sheet.profiles(fullList, title) |
303 | } |
304 | }, [ |
305 | h('div.main', [ |
306 | h('div.name', computed(moreCount, count => { |
307 | return count && plural('View %s more', count) |
308 | })) |
309 | ]) |
310 | ]) |
311 | ) |
312 | ]) |
313 | ] |
314 | } |
315 | |
316 | function assignImage (id, image) { |
317 | api.message.async.publish({ |
318 | type: 'about', |
319 | about: id, |
320 | image |
321 | }) |
322 | } |
323 | |
324 | function assignName (id, name) { |
325 | api.message.async.publish({ |
326 | type: 'about', |
327 | about: id, |
328 | name |
329 | }) |
330 | } |
331 | |
332 | function rename (id) { |
333 | api.sheet.display(close => { |
334 | var currentName = api.about.obs.name(id) |
335 | var input = h('input', { |
336 | style: {'font-size': '150%'}, |
337 | value: currentName() |
338 | }) |
339 | setTimeout(() => { |
340 | input.focus() |
341 | input.select() |
342 | }, 5) |
343 | return { |
344 | content: h('div', { |
345 | style: { |
346 | padding: '20px', |
347 | 'text-align': 'center' |
348 | } |
349 | }, [ |
350 | h('h2', { |
351 | style: { |
352 | 'font-weight': 'normal' |
353 | } |
354 | }, [i18n('What would you like to call '), h('strong', [currentName]), '?']), |
355 | input |
356 | ]), |
357 | footer: [ |
358 | h('button -save', { |
359 | 'ev-click': () => { |
360 | if (input.value.trim() && input.value !== currentName()) { |
361 | // no confirm |
362 | api.sbot.async.publish({ |
363 | type: 'about', |
364 | about: id, |
365 | name: input.value.trim() |
366 | }) |
367 | } |
368 | close() |
369 | } |
370 | }, i18n('Confirm')), |
371 | h('button -cancel', { |
372 | 'ev-click': close |
373 | }, i18n('Cancel')) |
374 | ] |
375 | } |
376 | }) |
377 | } |
378 | |
379 | function nameList (prefix, ids) { |
380 | var items = map(ids, api.about.obs.name) |
381 | return computed([prefix, items], (prefix, names) => { |
382 | return (prefix ? (prefix + '\n') : '') + names.map((n) => `- ${n}`).join('\n') |
383 | }) |
384 | } |
385 | } |
386 | |
387 | function filterByValues (attributes, ...matchValues) { |
388 | return Object.keys(attributes).reduce((result, key) => { |
389 | var values = attributes[key].filter(value => { |
390 | return matchValues.some(matchValue => { |
391 | if (Array.isArray(matchValue)) { |
392 | return matchValue.includes(value) |
393 | } else { |
394 | return matchValue === value |
395 | } |
396 | }) |
397 | }) |
398 | if (values.length) { |
399 | result[key] = values |
400 | } |
401 | return result |
402 | }, {}) |
403 | } |
404 |
Built with git-ssb-web