git ssb

10+

Matt McKegg / patchwork



Tree: 3994612d1d69cb182c0e638c63591ead2781898b

Files: 3994612d1d69cb182c0e638c63591ead2781898b / plugs / contact / html / follow-toggle.js

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

Built with git-ssb-web