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