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