Files: a69e9da25b4bc586435174712185486605414551 / plugs / contact / html / follow-toggle.js
2756 bytesRaw
1 | var nest = require('depnest') |
2 | var { h, when, send, computed } = require('mutant') |
3 | |
4 | exports.gives = nest('contact.html.followToggle') |
5 | exports.needs = nest({ |
6 | 'intl.sync.i18n': 'first', |
7 | 'keys.sync.id': 'first', |
8 | 'contact.async.follow': 'first', |
9 | 'contact.async.unfollow': 'first', |
10 | 'contact.async.block': 'first', |
11 | 'contact.async.unblock': 'first', |
12 | 'contact.obs.following': 'first', |
13 | 'contact.obs.followers': 'first', |
14 | 'contact.obs.blockers': 'first' |
15 | }) |
16 | |
17 | exports.create = function (api) { |
18 | var i18n = api.intl.sync.i18n |
19 | return nest('contact.html.followToggle', function (id, opts) { |
20 | var yourId = api.keys.sync.id() |
21 | |
22 | var yourFollows = api.contact.obs.following(yourId) |
23 | var rawFollowers = api.contact.obs.followers(id) |
24 | var rawFollowing = api.contact.obs.following(id) |
25 | |
26 | var friends = computed([rawFollowing, rawFollowers], (following, followers) => { |
27 | return Array.from(following).filter(follow => followers.includes(follow)) |
28 | }) |
29 | |
30 | var following = computed([rawFollowing, friends], (following, friends) => { |
31 | return Array.from(following).filter(follow => !friends.includes(follow)) |
32 | }) |
33 | |
34 | var isFriends = computed([friends], function (friends) { |
35 | return friends.includes(yourId) |
36 | }) |
37 | |
38 | var followsYou = computed([following], function (followsYou) { |
39 | return followsYou.includes(yourId) |
40 | }) |
41 | |
42 | var youFollow = computed([yourFollows], function (youFollow) { |
43 | return youFollow.includes(id) |
44 | }) |
45 | |
46 | var blockers = api.contact.obs.blockers(id) |
47 | var youBlock = computed(blockers, function (blockers) { |
48 | return blockers.includes(yourId) |
49 | }) |
50 | |
51 | var showBlockButton = computed([opts && opts.block], (block) => block !== false) |
52 | |
53 | if (id !== yourId) { |
54 | return when(youBlock, [ |
55 | h('a.ToggleButton.-unblocking', { |
56 | 'href': '#', |
57 | 'title': i18n('Click to unblock'), |
58 | 'ev-click': send(api.contact.async.unblock, id) |
59 | }, i18n('Blocked')) |
60 | ], [ |
61 | when(youFollow, |
62 | h('a.ToggleButton.-unsubscribe', { |
63 | 'href': '#', |
64 | 'title': i18n('Click to unfollow'), |
65 | 'ev-click': send(api.contact.async.unfollow, id) |
66 | }, when(isFriends, i18n('Friends'), i18n('Following'))), |
67 | h('a.ToggleButton.-subscribe', { |
68 | 'href': '#', |
69 | 'ev-click': send(api.contact.async.follow, id) |
70 | }, when(followsYou, i18n('Follow Back'), i18n('Follow'))) |
71 | ), |
72 | when(showBlockButton, h('a.ToggleButton.-blocking', { |
73 | 'href': '#', |
74 | 'title': i18n('Click to block syncing with this person and hide their posts'), |
75 | 'ev-click': send(api.contact.async.block, id) |
76 | }, i18n('Block'))) |
77 | ]) |
78 | } else { |
79 | return [] |
80 | } |
81 | }) |
82 | } |
83 |
Built with git-ssb-web