git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: fd85531cc70593cf1a26e65c7f4b8f3c2f435b95

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

9898 bytesRaw
1var nest = require('depnest')
2var ref = require('ssb-ref')
3var {h, when, computed, map, send, dictToCollection, resolve} = require('mutant')
4var extend = require('xtend')
5
6exports.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.async.block': 'first',
33 'contact.async.unblock': 'first',
34 'intl.sync.i18n': 'first',
35})
36exports.gives = nest('page.html.render')
37
38exports.create = function (api) {
39 const i18n = api.intl.sync.i18n
40 return nest('page.html.render', function profile (id) {
41 if (!ref.isFeed(id)) return
42
43 var name = api.about.obs.name(id)
44 var description = api.about.obs.description(id)
45 var yourId = api.keys.sync.id()
46 var yourFollows = api.contact.obs.following(yourId)
47 var rawFollowers = api.contact.obs.followers(id)
48 var rawFollowing = api.contact.obs.following(id)
49 var friendsLoaded = computed([rawFollowers.sync, rawFollowing.sync], (...x) => x.every(Boolean))
50 var { block, unblock } = api.contact.async
51
52 var friends = computed([rawFollowing, rawFollowers], (following, followers) => {
53 return Array.from(following).filter(follow => followers.includes(follow))
54 })
55
56 var following = computed([rawFollowing, friends], (following, friends) => {
57 return Array.from(following).filter(follow => !friends.includes(follow))
58 })
59
60 var followers = computed([rawFollowers, friends], (followers, friends) => {
61 return Array.from(followers).filter(follower => !friends.includes(follower))
62 })
63
64 var isFriends = computed([friends], function (friends) {
65 return friends.includes(yourId)
66 })
67
68 var followsYou = computed([following], function (followsYou) {
69 return followsYou.includes(yourId)
70 })
71
72 var youFollow = computed([yourFollows], function (youFollow) {
73 return youFollow.includes(id)
74 })
75
76 var blockers = api.contact.obs.blockers(id)
77 var ImBlockingThem = computed(blockers, function(blockers) {
78 return blockers.includes(yourId)
79 })
80
81 var names = api.about.obs.names(id)
82 var images = api.about.obs.images(id)
83
84 var namePicker = h('div', {className: 'Picker'}, [
85 map(dictToCollection(names), (item) => {
86 var isSelf = computed(item.value, (ids) => ids.includes(id))
87 var isAssigned = computed(item.value, (ids) => ids.includes(yourId))
88 return h('a.name', {
89 'ev-click': () => {
90 if (!isAssigned()) {
91 assignName(id, resolve(item.key))
92 }
93 },
94 href: '#',
95 classList: [
96 when(isSelf, '-self'),
97 when(isAssigned, '-assigned')
98 ],
99 title: nameList(when(isSelf, i18n('Self Assigned'), i18n('Assigned By')), item.value)
100 }, [
101 item.key
102 ])
103 }),
104 h('a -add', {
105 'ev-click': () => {
106 rename(id)
107 },
108 href: '#',
109 }, ['+'])
110 ])
111
112 var imagePicker = h('div', {className: 'Picker'}, [
113 map(dictToCollection(images), (item) => {
114 var isSelf = computed(item.value, (ids) => ids.includes(id))
115 var isAssigned = computed(item.value, (ids) => ids.includes(yourId))
116 return h('a.name', {
117 'ev-click': () => {
118 if (!isAssigned()) {
119 assignImage(id, resolve(item.key))
120 }
121 },
122 href: '#',
123 classList: [
124 when(isSelf, '-self'),
125 when(isAssigned, '-assigned')
126 ],
127 title: nameList(when(isSelf, i18n('Self Assigned'), i18n('Assigned By')), item.value)
128 }, [
129 h('img', {
130 className: 'Avatar',
131 style: { 'background-color': api.about.obs.color(id) },
132 src: computed(item.key, api.blob.sync.url)
133 })
134 ])
135 }),
136 h('span.add', [
137 api.blob.html.input(file => {
138 assignImage(id, file.link)
139 }, {
140 accept: 'image/*',
141 resize: { width: 500, height: 500 }
142 })
143 ])
144 ])
145
146 var prepend = h('header', {className: 'ProfileHeader'}, [
147 h('div.image', api.about.html.image(id)),
148 h('div.main', [
149 h('div.title', [
150 h('h1', [name]),
151 h('div.meta', [
152 when(id === yourId, [
153 h('button', {'ev-click': api.profile.sheet.edit}, i18n('Edit Your Profile'))
154 ], [
155 when(youFollow,
156 h('a.ToggleButton.-unsubscribe', {
157 'href': '#',
158 'title': i18n('Click to unfollow'),
159 'ev-click': send(unfollow, id)
160 }, when(isFriends, i18n('Friends'), i18n('Following'))),
161 h('a.ToggleButton.-subscribe', {
162 'href': '#',
163 'ev-click': send(follow, id)
164 }, when(followsYou, i18n('Follow Back'), i18n('Follow')))
165 ),
166 when(ImBlockingThem,
167 h('a.ToggleButton.-unblocking', {
168 'href': '#',
169 'title': i18n('Unblock'),
170 'ev-click': () => unblock(id, console.log)
171 }, i18n('Unblock')),
172 h('a.ToggleButton.-blocking', {
173 'href': '#',
174 'title': i18n('Block'),
175 'ev-click': () => block(id, console.log)
176 }, i18n('Block')),
177 )
178 ]),
179 ])
180 ]),
181 h('section -description', [
182 computed(description, (text) => {
183 if (typeof text === 'string') {
184 return api.message.html.markdown(text)
185 }
186 })
187 ]),
188 h('section', [ namePicker, imagePicker ])
189 ])
190 ])
191
192 var feedView = api.feed.html.rollup(api.feed.pull.profile(id), {
193 prepend,
194 displayFilter: (msg) => msg.value.author === id,
195 bumpFilter: (msg) => msg.value.author === id,
196 })
197
198 var container = h('div', {className: 'SplitView'}, [
199 h('div.main', [
200 feedView
201 ]),
202 h('div.side.-right', [
203 h('button PrivateMessageButton', {'ev-click': () => api.app.navigate('/private', {compose: {to: id}})}, i18n('Send Private Message')),
204 when(friendsLoaded,
205 h('div', [
206 renderContactBlock(i18n('Friends'), friends, yourFollows),
207 renderContactBlock(i18n('Followers'), followers, yourFollows),
208 renderContactBlock(i18n('Following'), following, yourFollows)
209 ]),
210 h('div', {className: 'Loading'})
211 )
212 ])
213 ])
214
215 container.pendingUpdates = feedView.pendingUpdates
216 container.reload = feedView.reload
217 return container
218 })
219
220 function renderContactBlock (title, profiles, yourFollows) {
221 profiles = api.profile.obs.rank(profiles)
222 return [
223 when(computed(profiles, x => x.length), h('h2', title)),
224 h('div', {
225 classList: 'ProfileList'
226 }, [
227 map(profiles, (id) => {
228 var following = computed(yourFollows, f => f.includes(id))
229 return h('a.profile', {
230 href: id,
231 classList: [
232 when(following, '-following')
233 ]
234 }, [
235 h('div.avatar', [api.about.html.image(id)]),
236 h('div.main', [
237 h('div.name', [ api.about.obs.name(id) ])
238 ])
239 ])
240 }, {
241 maxTime: 5,
242 idle: true
243 })
244 ])
245 ]
246 }
247
248 function follow (id) {
249 api.sbot.async.publish({
250 type: 'contact',
251 contact: id,
252 following: true
253 })
254 }
255
256 function unfollow (id) {
257 api.sbot.async.publish({
258 type: 'contact',
259 contact: id,
260 following: false
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

Built with git-ssb-web