Files: d3516ed7e732fa6234093e1b15a55b982a89c652 / modules / profile / html / preview.js
3958 bytesRaw
1 | var nest = require('depnest') |
2 | var h = require('mutant/h') |
3 | var computed = require('mutant/computed') |
4 | var send = require('mutant/send') |
5 | |
6 | exports.needs = nest({ |
7 | 'about.obs.name': 'first', |
8 | 'about.html.image': 'first', |
9 | 'keys.sync.id': 'first', |
10 | 'sheet.display': 'first', |
11 | 'app.navigate': 'first', |
12 | 'contact.obs': { |
13 | followers: 'first', |
14 | following: 'first', |
15 | blockers: 'first' |
16 | }, |
17 | 'contact.async.block': 'first', |
18 | 'contact.async.unblock': 'first', |
19 | 'intl.sync.i18n': 'first', |
20 | 'intl.sync.i18n_n': 'first', |
21 | 'profile.obs.hops': 'first', |
22 | 'sheet.profiles': 'first', |
23 | 'contact.html.followToggle': 'first' |
24 | }) |
25 | |
26 | exports.gives = nest('profile.html.preview') |
27 | |
28 | exports.create = function (api) { |
29 | const i18n = api.intl.sync.i18n |
30 | const plural = api.intl.sync.i18n_n |
31 | |
32 | return nest('profile.html.preview', function (id) { |
33 | var name = api.about.obs.name(id) |
34 | var yourId = api.keys.sync.id() |
35 | var yourFollows = api.contact.obs.following(yourId) |
36 | var yourFollowers = api.contact.obs.followers(yourId) |
37 | |
38 | var rawFollowers = api.contact.obs.followers(id) |
39 | var rawFollowing = api.contact.obs.following(id) |
40 | var hops = api.profile.obs.hops(yourId, id) |
41 | |
42 | var blockers = api.contact.obs.blockers(id) |
43 | var youBlock = computed(blockers, function (blockers) { |
44 | return blockers.includes(yourId) |
45 | }) |
46 | var yourBlockingFriends = computed([yourFollowers, yourFollows, blockers], inAllSets) |
47 | var mutualFriends = computed([yourFollowers, yourFollows, rawFollowers, rawFollowing], inAllSets) |
48 | |
49 | return h('ProfilePreview', [ |
50 | h('header', [ |
51 | h('div.image', api.about.html.image(id)), |
52 | h('div.main', [ |
53 | h('div.title', [ |
54 | h('h1', [ |
55 | h('a', {href: id, 'ev-click': () => api.app.navigate(id)}, [name]) |
56 | ]), |
57 | h('div.meta', [ |
58 | api.contact.html.followToggle(id, {block: false}) |
59 | ]) |
60 | ]), |
61 | h('section -publicKey', [ |
62 | h('pre', {title: i18n('Public key for this profile')}, id) |
63 | ]) |
64 | ]) |
65 | ]), |
66 | computed([hops, yourBlockingFriends, youBlock], (value, yourBlockingFriends, youBlock) => { |
67 | if (value) { |
68 | if ((value[0] > 1 || youBlock) && yourBlockingFriends.length > 0) { |
69 | return h('section -blockWarning', [ |
70 | h('a', { |
71 | href: '#', |
72 | 'ev-click': send(displayBlockingFriends, yourBlockingFriends) |
73 | }, [ |
74 | '⚠️ ', plural('This person is blocked by %s of your friends.', yourBlockingFriends.length) |
75 | ]) |
76 | ]) |
77 | } else if (value[0] > 2 || value[1] === undefined) { |
78 | return h('section -distanceWarning', [ |
79 | '⚠️ ', i18n(`You don't follow anyone who follows this person`) |
80 | ]) |
81 | } else if (value[1] > 2 || value[1] === undefined) { |
82 | return h('section -distanceWarning', [ |
83 | '⚠️ ', i18n('This person does not follow anyone that follows you') |
84 | ]) |
85 | } else if (value[0] === 2) { |
86 | return h('section -mutualFriends', [ |
87 | h('a', { |
88 | href: '#', |
89 | 'ev-click': send(displayMutualFriends, mutualFriends) |
90 | }, [ |
91 | computed(mutualFriends, (items) => { |
92 | return plural('You share %s mutual friends with this person.', items.length) |
93 | }) |
94 | ]) |
95 | ]) |
96 | } else if (value[0] === 0) { |
97 | return h('section -you', [ |
98 | i18n('This is you.') |
99 | ]) |
100 | } |
101 | } |
102 | }) |
103 | ]) |
104 | }) |
105 | |
106 | function displayMutualFriends (profiles) { |
107 | api.sheet.profiles(profiles, i18n('Mutual Friends')) |
108 | } |
109 | |
110 | function displayBlockingFriends (profiles) { |
111 | api.sheet.profiles(profiles, i18n('Blocked by')) |
112 | } |
113 | } |
114 | |
115 | function inAllSets (first, ...rest) { |
116 | return first.filter(value => rest.every((collection) => collection.includes(value))) |
117 | } |
118 |
Built with git-ssb-web