git ssb

10+

Matt McKegg / patchwork



Tree: 593dfd760dee299082f615e02f2747f1c6f660ec

Files: 593dfd760dee299082f615e02f2747f1c6f660ec / modules / profile / obs / contact.js

2680 bytesRaw
1var computed = require('mutant/computed')
2var nest = require('depnest')
3
4exports.needs = nest({
5 'keys.sync.id': 'first',
6 'contact.obs': {
7 followers: 'first',
8 following: 'first',
9 blockers: 'first'
10 }
11})
12
13exports.gives = nest('profile.obs.contact')
14
15exports.create = function (api) {
16 return nest('profile.obs.contact', function (id) {
17 var yourId = api.keys.sync.id()
18 var yourFollowing = api.contact.obs.following(yourId)
19 var yourFollowers = api.contact.obs.followers(yourId)
20
21 var followers = api.contact.obs.followers(id)
22 var following = api.contact.obs.following(id)
23 var sync = computed([followers.sync, following.sync], (...x) => x.every(Boolean))
24
25 var blockers = api.contact.obs.blockers(id)
26 var youBlock = computed(blockers, function (blockers) {
27 return blockers.includes(yourId)
28 })
29
30 var youFollow = computed([yourFollowing], function (yourFollowing) {
31 return yourFollowing.includes(id)
32 })
33
34 var blockingFriends = computed([yourFollowers, yourFollowing, blockers], inAllSets)
35 var mutualFriends = computed([yourFollowers, yourFollowing, followers, following], inAllSets)
36 var outgoingVia = computed([yourFollowers, following], inAllSets)
37 var incomingVia = computed([yourFollowing, followers], inAllSets)
38
39 var hasOutgoing = computed([yourFollowers, following], (a, b) => {
40 return a.some((id) => b.includes(id))
41 })
42 var hasIncoming = computed([followers, yourFollowing], (a, b) => {
43 return a.some((id) => b.includes(id))
44 })
45
46 var isYou = computed([yourId, id], (a, b) => a === b)
47
48 var isNotFollowingAnybody = computed(following,
49 followingList => !followingList || followingList.length == 0
50 );
51
52 return {
53 followers,
54 following,
55 blockers,
56 blockingFriends,
57 blockingFriendsCount: count(blockingFriends),
58 mutualFriends,
59 mutualFriendsCount: count(mutualFriends),
60 outgoingVia,
61 outgoingViaCount: count(outgoingVia),
62 incomingVia,
63 incomingViaCount: count(incomingVia),
64 hasOutgoing,
65 isNotFollowingAnybody,
66 noOutgoing: not(hasOutgoing, isYou),
67 hasIncoming,
68 noIncoming: not(hasIncoming, isYou),
69 yourId,
70 yourFollowing,
71 yourFollowers,
72 youFollow,
73 youBlock,
74 isYou,
75 notFollowing: not(youFollow, isYou),
76 sync
77 }
78 })
79}
80
81function inAllSets (first, ...rest) {
82 return first.filter(value => rest.every((collection) => collection.includes(value)))
83}
84
85function not (obs, isFalse) {
86 return computed([obs, isFalse], (x, isFalse) => isFalse ? false : !x)
87}
88
89function count (obs) {
90 return computed(obs, (x) => x.length)
91}
92

Built with git-ssb-web