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