Files: fd1ede87dfa5cdbb403b2eb9ea5406a1a6570755 / modules / page / html / render / profile.js
11749 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 | '⚠️ ', plural('This person is blocked by %s of your friends.', yourBlockingFriends.length) |
169 | ]) |
170 | ]) |
171 | } else if (value[0] > 2 || value[1] === undefined) { |
172 | return h('section -distanceWarning', [ |
173 | h('h1', i18n(`You don't follow anyone who follows this person`)), |
174 | h('p', i18n('You might not be seeing their latest messages. You could try joining a pub that they are a member of.')) |
175 | ]) |
176 | } else if (value[1] > 2 || value[1] === undefined) { |
177 | return h('section -distanceWarning', [ |
178 | h('h1', i18n('This person does not follow anyone that follows you')), |
179 | h('p', i18n('They might not receive your private messages or replies. You could try joining a pub that they are a member of.')) |
180 | ]) |
181 | } else if (value[0] === 2) { |
182 | return h('section -mutualFriends', [ |
183 | h('a', { |
184 | href: '#', |
185 | 'ev-click': send(displayMutualFriends, mutualFriends) |
186 | }, [ |
187 | '👥 ', |
188 | computed(mutualFriends, (items) => { |
189 | return plural('You share %s mutual friends with this person.', items.length) |
190 | }) |
191 | ]) |
192 | ]) |
193 | } |
194 | } |
195 | }), |
196 | |
197 | h('section -description', [ |
198 | computed(description, (text) => { |
199 | if (typeof text === 'string') { |
200 | return api.message.html.markdown(text) |
201 | } |
202 | }) |
203 | ]), |
204 | h('section', [ namePicker, imagePicker ]) |
205 | ]) |
206 | ]) |
207 | |
208 | var feedView = api.feed.html.rollup(api.feed.pull.profile(id), { |
209 | prepend, |
210 | displayFilter: (msg) => msg.value.author === id, |
211 | rootFilter: (msg) => !youBlock(), |
212 | bumpFilter: (msg) => msg.value.author === id |
213 | }) |
214 | |
215 | var container = h('div', {className: 'SplitView'}, [ |
216 | h('div.main', [ |
217 | feedView |
218 | ]), |
219 | h('div.side.-right', [ |
220 | h('button PrivateMessageButton', {'ev-click': () => api.app.navigate('/private', {compose: {to: id}})}, i18n('Send Private Message')), |
221 | when(friendsLoaded, |
222 | h('div', [ |
223 | renderContactBlock(i18n('Friends'), friends, yourFollows), |
224 | renderContactBlock(i18n('Followers'), followers, yourFollows), |
225 | renderContactBlock(i18n('Following'), following, yourFollows), |
226 | renderContactBlock(i18n('Blocked by'), yourBlockingFriends, yourFollows) |
227 | ]), |
228 | h('div', {className: 'Loading'}) |
229 | ) |
230 | ]) |
231 | ]) |
232 | |
233 | // refresh feed (to hide all posts) when blocked |
234 | youBlock(feedView.reload) |
235 | |
236 | container.pendingUpdates = feedView.pendingUpdates |
237 | container.reload = feedView.reload |
238 | return container |
239 | }) |
240 | |
241 | function displayMutualFriends (profiles) { |
242 | api.sheet.profiles(profiles, i18n('Mutual Friends')) |
243 | } |
244 | |
245 | function displayBlockingFriends (profiles) { |
246 | api.sheet.profiles(profiles, i18n('Blocked by')) |
247 | } |
248 | |
249 | function renderContactBlock (title, profiles, yourFollows) { |
250 | profiles = api.profile.obs.rank(profiles) |
251 | return [ |
252 | when(computed(profiles, x => x.length), h('h2', title)), |
253 | h('div', { |
254 | classList: 'ProfileList' |
255 | }, [ |
256 | map(profiles, (id) => { |
257 | var following = computed(yourFollows, f => f.includes(id)) |
258 | return h('a.profile', { |
259 | href: id, |
260 | classList: [ |
261 | when(following, '-following') |
262 | ] |
263 | }, [ |
264 | h('div.avatar', [api.about.html.image(id)]), |
265 | h('div.main', [ |
266 | h('div.name', [ api.about.obs.name(id) ]) |
267 | ]) |
268 | ]) |
269 | }, { |
270 | maxTime: 5, |
271 | idle: true |
272 | }) |
273 | ]) |
274 | ] |
275 | } |
276 | |
277 | function assignImage (id, image) { |
278 | api.message.async.publish({ |
279 | type: 'about', |
280 | about: id, |
281 | image |
282 | }) |
283 | } |
284 | |
285 | function assignName (id, name) { |
286 | api.message.async.publish({ |
287 | type: 'about', |
288 | about: id, |
289 | name |
290 | }) |
291 | } |
292 | |
293 | function rename (id) { |
294 | api.sheet.display(close => { |
295 | var currentName = api.about.obs.name(id) |
296 | var input = h('input', { |
297 | style: {'font-size': '150%'}, |
298 | value: currentName() |
299 | }) |
300 | setTimeout(() => { |
301 | input.focus() |
302 | input.select() |
303 | }, 5) |
304 | return { |
305 | content: h('div', { |
306 | style: { |
307 | padding: '20px', |
308 | 'text-align': 'center' |
309 | } |
310 | }, [ |
311 | h('h2', { |
312 | style: { |
313 | 'font-weight': 'normal' |
314 | } |
315 | }, [i18n('What whould you like to call '), h('strong', [currentName]), '?']), |
316 | input |
317 | ]), |
318 | footer: [ |
319 | h('button -save', { |
320 | 'ev-click': () => { |
321 | if (input.value.trim() && input.value !== currentName()) { |
322 | // no confirm |
323 | api.sbot.async.publish({ |
324 | type: 'about', |
325 | about: id, |
326 | name: input.value.trim() |
327 | }) |
328 | } |
329 | close() |
330 | } |
331 | }, i18n('Confirm')), |
332 | h('button -cancel', { |
333 | 'ev-click': close |
334 | }, i18n('Cancel')) |
335 | ] |
336 | } |
337 | }) |
338 | } |
339 | |
340 | function nameList (prefix, ids) { |
341 | var items = map(ids, api.about.obs.name) |
342 | return computed([prefix, items], (prefix, names) => { |
343 | return (prefix ? (prefix + '\n') : '') + names.map((n) => `- ${n}`).join('\n') |
344 | }) |
345 | } |
346 | } |
347 | |
348 | function filterByValues (attributes, ...matchValues) { |
349 | return Object.keys(attributes).reduce((result, key) => { |
350 | var values = attributes[key].filter(value => { |
351 | return matchValues.some(matchValue => { |
352 | if (Array.isArray(matchValue)) { |
353 | return matchValue.includes(value) |
354 | } else { |
355 | return matchValue === value |
356 | } |
357 | }) |
358 | }) |
359 | if (values.length) { |
360 | result[key] = values |
361 | } |
362 | return result |
363 | }, {}) |
364 | } |
365 | |
366 | function inAllSets (first, ...rest) { |
367 | return first.filter(value => rest.every((collection) => collection.includes(value))) |
368 | } |
369 |
Built with git-ssb-web