Files: 19bf91406114f251a3218b5c45c4a69e95d11f84 / modules / page / html / render / profile.js
11663 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 | ]), |
227 | h('div', {className: 'Loading'}) |
228 | ) |
229 | ]) |
230 | ]) |
231 | |
232 | // refresh feed (to hide all posts) when blocked |
233 | youBlock(feedView.reload) |
234 | |
235 | container.pendingUpdates = feedView.pendingUpdates |
236 | container.reload = feedView.reload |
237 | return container |
238 | }) |
239 | |
240 | function displayMutualFriends (profiles) { |
241 | api.sheet.profiles(profiles, i18n('Mutual Friends')) |
242 | } |
243 | |
244 | function displayBlockingFriends (profiles) { |
245 | api.sheet.profiles(profiles, i18n('Blocked by')) |
246 | } |
247 | |
248 | function renderContactBlock (title, profiles, yourFollows) { |
249 | profiles = api.profile.obs.rank(profiles) |
250 | return [ |
251 | when(computed(profiles, x => x.length), h('h2', title)), |
252 | h('div', { |
253 | classList: 'ProfileList' |
254 | }, [ |
255 | map(profiles, (id) => { |
256 | var following = computed(yourFollows, f => f.includes(id)) |
257 | return h('a.profile', { |
258 | href: id, |
259 | classList: [ |
260 | when(following, '-following') |
261 | ] |
262 | }, [ |
263 | h('div.avatar', [api.about.html.image(id)]), |
264 | h('div.main', [ |
265 | h('div.name', [ api.about.obs.name(id) ]) |
266 | ]) |
267 | ]) |
268 | }, { |
269 | maxTime: 5, |
270 | idle: true |
271 | }) |
272 | ]) |
273 | ] |
274 | } |
275 | |
276 | function assignImage (id, image) { |
277 | api.message.async.publish({ |
278 | type: 'about', |
279 | about: id, |
280 | image |
281 | }) |
282 | } |
283 | |
284 | function assignName (id, name) { |
285 | api.message.async.publish({ |
286 | type: 'about', |
287 | about: id, |
288 | name |
289 | }) |
290 | } |
291 | |
292 | function rename (id) { |
293 | api.sheet.display(close => { |
294 | var currentName = api.about.obs.name(id) |
295 | var input = h('input', { |
296 | style: {'font-size': '150%'}, |
297 | value: currentName() |
298 | }) |
299 | setTimeout(() => { |
300 | input.focus() |
301 | input.select() |
302 | }, 5) |
303 | return { |
304 | content: h('div', { |
305 | style: { |
306 | padding: '20px', |
307 | 'text-align': 'center' |
308 | } |
309 | }, [ |
310 | h('h2', { |
311 | style: { |
312 | 'font-weight': 'normal' |
313 | } |
314 | }, [i18n('What whould you like to call '), h('strong', [currentName]), '?']), |
315 | input |
316 | ]), |
317 | footer: [ |
318 | h('button -save', { |
319 | 'ev-click': () => { |
320 | if (input.value.trim() && input.value !== currentName()) { |
321 | // no confirm |
322 | api.sbot.async.publish({ |
323 | type: 'about', |
324 | about: id, |
325 | name: input.value.trim() |
326 | }) |
327 | } |
328 | close() |
329 | } |
330 | }, i18n('Confirm')), |
331 | h('button -cancel', { |
332 | 'ev-click': close |
333 | }, i18n('Cancel')) |
334 | ] |
335 | } |
336 | }) |
337 | } |
338 | |
339 | function nameList (prefix, ids) { |
340 | var items = map(ids, api.about.obs.name) |
341 | return computed([prefix, items], (prefix, names) => { |
342 | return (prefix ? (prefix + '\n') : '') + names.map((n) => `- ${n}`).join('\n') |
343 | }) |
344 | } |
345 | } |
346 | |
347 | function filterByValues (attributes, ...matchValues) { |
348 | return Object.keys(attributes).reduce((result, key) => { |
349 | var values = attributes[key].filter(value => { |
350 | return matchValues.some(matchValue => { |
351 | if (Array.isArray(matchValue)) { |
352 | return matchValue.includes(value) |
353 | } else { |
354 | return matchValue === value |
355 | } |
356 | }) |
357 | }) |
358 | if (values.length) { |
359 | result[key] = values |
360 | } |
361 | return result |
362 | }, {}) |
363 | } |
364 | |
365 | function inAllSets (first, ...rest) { |
366 | return first.filter(value => rest.every((collection) => collection.includes(value))) |
367 | } |
368 |
Built with git-ssb-web