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