git ssb

10+

Matt McKegg / patchwork



Tree: b7ae6aab41bf1d2ae415e7621d196b09103f4371

Files: b7ae6aab41bf1d2ae415e7621d196b09103f4371 / modules / page / html / render / profile.js

13448 bytesRaw
1var nest = require('depnest')
2var ref = require('ssb-ref')
3var {h, when, computed, map, send, dictToCollection, resolve} = require('mutant')
4
5exports.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})
33exports.gives = nest('page.html.render')
34
35exports.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 ungroupFilter: (msg) => msg.value.author !== id
207 })
208
209 var container = h('div', {className: 'SplitView'}, [
210 h('div.main', [
211 feedView
212 ]),
213 h('div.side.-right', [
214 h('button PrivateMessageButton', {'ev-click': () => api.app.navigate('/private', {compose: {to: id}})}, i18n('Send Private Message')),
215 when(contact.sync,
216 h('div', [
217 renderContactBlock(i18n('Friends'), onlyRecent(friends, 10), contact.yourFollowing, friends),
218 renderContactBlock(i18n('Followers'), onlyFollowing(followers, 10), contact.yourFollowing, followers),
219 renderContactBlock(i18n('Following'), onlyRecent(following, 10), contact.yourFollowing, following),
220 renderContactBlock(i18n('Blocked by'), contact.blockingFriends, contact.yourFollowing)
221 ]),
222 h('div', {className: 'Loading'})
223 )
224 ])
225 ])
226
227 // refresh feed (to hide all posts) when blocked
228 contact.youBlock(feedView.reload)
229
230 container.pendingUpdates = feedView.pendingUpdates
231 container.reload = feedView.reload
232 return container
233
234 // scoped
235
236 function onlyFollowing (ids, max) {
237 return computed([recent, ids, contact.yourFollowing], (a, b, c) => {
238 var result = a.filter(x => b.includes(x) && c.includes(x))
239 if (result.length === 0 && a.length) {
240 // fallback to just recent
241 result = a.filter(x => b.includes(x))
242 }
243 if (max) {
244 return result.slice(0, max)
245 } else {
246 return result
247 }
248 })
249 }
250
251 function onlyRecent (ids, max) {
252 return computed([recent, ids], (a, b) => {
253 var result = a.filter(x => b.includes(x))
254 if (max) {
255 return result.slice(0, max)
256 } else {
257 return result
258 }
259 })
260 }
261 })
262
263 function displayFollowedBy (profiles) {
264 api.sheet.profiles(profiles, i18n('Followed by'))
265 }
266
267 function displayMutualFriends (profiles) {
268 api.sheet.profiles(profiles, i18n('Mutual Friends'))
269 }
270
271 function displayBlockingFriends (profiles) {
272 api.sheet.profiles(profiles, i18n('Blocked by'))
273 }
274
275 function renderContactBlock (title, profiles, yourFollowing, fullList) {
276 var moreCount = computed([profiles, fullList], (a, b) => a && b && a.length < b.length && b.length - a.length)
277 return [
278 when(computed([profiles, fullList], (a, b) => a.length || (b && b.length)), h('h2', title)),
279 h('div', {
280 classList: 'ProfileList'
281 }, [
282 map(profiles, (id) => {
283 var following = computed(yourFollowing, f => f.includes(id))
284 return h('a.profile', {
285 href: id,
286 classList: [
287 when(following, '-following')
288 ]
289 }, [
290 h('div.avatar', [api.about.html.image(id)]),
291 h('div.main', [
292 h('div.name', [ api.about.obs.name(id) ])
293 ])
294 ])
295 }, {
296 maxTime: 5,
297 idle: true
298 }),
299 when(moreCount,
300 h('a.profile -more', {
301 href: '#',
302 'ev-click': function () {
303 api.sheet.profiles(fullList, title)
304 }
305 }, [
306 h('div.main', [
307 h('div.name', computed(moreCount, count => {
308 return count && plural('View %s more', count)
309 }))
310 ])
311 ])
312 )
313 ])
314 ]
315 }
316
317 function assignImage (id, image) {
318 api.message.async.publish({
319 type: 'about',
320 about: id,
321 image
322 })
323 }
324
325 function assignName (id, name) {
326 api.message.async.publish({
327 type: 'about',
328 about: id,
329 name
330 })
331 }
332
333 function rename (id) {
334 api.sheet.display(close => {
335 var currentName = api.about.obs.name(id)
336 var input = h('input', {
337 style: {'font-size': '150%'},
338 value: currentName()
339 })
340 setTimeout(() => {
341 input.focus()
342 input.select()
343 }, 5)
344 return {
345 content: h('div', {
346 style: {
347 padding: '20px',
348 'text-align': 'center'
349 }
350 }, [
351 h('h2', {
352 style: {
353 'font-weight': 'normal'
354 }
355 }, [i18n('What would you like to call '), h('strong', [currentName]), '?']),
356 h('h3', {
357 style: {
358 'font-weight': 'normal'
359 }
360 }, [i18n('Names you assign here will be publicly visible to others.')]),
361 input
362 ]),
363 footer: [
364 h('button -save', {
365 'ev-click': () => {
366 if (input.value.trim() && input.value !== currentName()) {
367 // no confirm
368 api.sbot.async.publish({
369 type: 'about',
370 about: id,
371 name: input.value.trim()
372 })
373 }
374 close()
375 }
376 }, i18n('Confirm')),
377 h('button -cancel', {
378 'ev-click': close
379 }, i18n('Cancel'))
380 ]
381 }
382 })
383 }
384
385 function nameList (prefix, ids) {
386 var items = map(ids, api.about.obs.name)
387 return computed([prefix, items], (prefix, names) => {
388 return (prefix ? (prefix + '\n') : '') + names.map((n) => `- ${n}`).join('\n')
389 })
390 }
391}
392
393function filterByValues (attributes, ...matchValues) {
394 return Object.keys(attributes).reduce((result, key) => {
395 var values = attributes[key].filter(value => {
396 return matchValues.some(matchValue => {
397 if (Array.isArray(matchValue)) {
398 return matchValue.includes(value)
399 } else {
400 return matchValue === value
401 }
402 })
403 })
404 if (values.length) {
405 result[key] = values
406 }
407 return result
408 }, {})
409}
410

Built with git-ssb-web