git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 8de1edb8454d207982be32e1b3b5a6e3497f19ce

Files: 8de1edb8454d207982be32e1b3b5a6e3497f19ce / modules / page / html / render / profile.js

11833 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.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})
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
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 compactFilter: (msg) => msg.value.author !== id, // show root context messages smaller
202 displayFilter: (msg) => msg.value.author === id,
203 rootFilter: (msg) => !contact.youBlock() && !api.message.sync.root(msg),
204 bumpFilter: (msg) => msg.value.author === id
205 })
206
207 var container = h('div', {className: 'SplitView'}, [
208 h('div.main', [
209 feedView
210 ]),
211 h('div.side.-right', [
212 h('button PrivateMessageButton', {'ev-click': () => api.app.navigate('/private', {compose: {to: id}})}, i18n('Send Private Message')),
213 when(contact.sync,
214 h('div', [
215 renderContactBlock(i18n('Friends'), friends, contact.yourFollowing),
216 renderContactBlock(i18n('Followers'), followers, contact.yourFollowing),
217 renderContactBlock(i18n('Following'), following, contact.yourFollowing),
218 renderContactBlock(i18n('Blocked by'), contact.blockingFriends, contact.yourFollowing)
219 ]),
220 h('div', {className: 'Loading'})
221 )
222 ])
223 ])
224
225 // refresh feed (to hide all posts) when blocked
226 contact.youBlock(feedView.reload)
227
228 container.pendingUpdates = feedView.pendingUpdates
229 container.reload = feedView.reload
230 return container
231 })
232
233 function displayFollowedBy (profiles) {
234 api.sheet.profiles(profiles, i18n('Followed by'))
235 }
236
237 function displayMutualFriends (profiles) {
238 api.sheet.profiles(profiles, i18n('Mutual Friends'))
239 }
240
241 function displayBlockingFriends (profiles) {
242 api.sheet.profiles(profiles, i18n('Blocked by'))
243 }
244
245 function renderContactBlock (title, profiles, yourFollowing) {
246 profiles = api.profile.obs.rank(profiles)
247 return [
248 when(computed(profiles, x => x.length), h('h2', title)),
249 h('div', {
250 classList: 'ProfileList'
251 }, [
252 map(profiles, (id) => {
253 var following = computed(yourFollowing, f => f.includes(id))
254 return h('a.profile', {
255 href: id,
256 classList: [
257 when(following, '-following')
258 ]
259 }, [
260 h('div.avatar', [api.about.html.image(id)]),
261 h('div.main', [
262 h('div.name', [ api.about.obs.name(id) ])
263 ])
264 ])
265 }, {
266 maxTime: 5,
267 idle: true
268 })
269 ])
270 ]
271 }
272
273 function assignImage (id, image) {
274 api.message.async.publish({
275 type: 'about',
276 about: id,
277 image
278 })
279 }
280
281 function assignName (id, name) {
282 api.message.async.publish({
283 type: 'about',
284 about: id,
285 name
286 })
287 }
288
289 function rename (id) {
290 api.sheet.display(close => {
291 var currentName = api.about.obs.name(id)
292 var input = h('input', {
293 style: {'font-size': '150%'},
294 value: currentName()
295 })
296 setTimeout(() => {
297 input.focus()
298 input.select()
299 }, 5)
300 return {
301 content: h('div', {
302 style: {
303 padding: '20px',
304 'text-align': 'center'
305 }
306 }, [
307 h('h2', {
308 style: {
309 'font-weight': 'normal'
310 }
311 }, [i18n('What whould you like to call '), h('strong', [currentName]), '?']),
312 input
313 ]),
314 footer: [
315 h('button -save', {
316 'ev-click': () => {
317 if (input.value.trim() && input.value !== currentName()) {
318 // no confirm
319 api.sbot.async.publish({
320 type: 'about',
321 about: id,
322 name: input.value.trim()
323 })
324 }
325 close()
326 }
327 }, i18n('Confirm')),
328 h('button -cancel', {
329 'ev-click': close
330 }, i18n('Cancel'))
331 ]
332 }
333 })
334 }
335
336 function nameList (prefix, ids) {
337 var items = map(ids, api.about.obs.name)
338 return computed([prefix, items], (prefix, names) => {
339 return (prefix ? (prefix + '\n') : '') + names.map((n) => `- ${n}`).join('\n')
340 })
341 }
342}
343
344function filterByValues (attributes, ...matchValues) {
345 return Object.keys(attributes).reduce((result, key) => {
346 var values = attributes[key].filter(value => {
347 return matchValues.some(matchValue => {
348 if (Array.isArray(matchValue)) {
349 return matchValue.includes(value)
350 } else {
351 return matchValue === value
352 }
353 })
354 })
355 if (values.length) {
356 result[key] = values
357 }
358 return result
359 }, {})
360}
361

Built with git-ssb-web