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