git ssb

10+

Matt McKegg / patchwork



Tree: 10d7179c91530bc339270652277e99f17716fc99

Files: 10d7179c91530bc339270652277e99f17716fc99 / modules / profile / obs / contact.js

2960 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 yourFriends = computed([yourFollowers, yourFollowing], inAllSets)
35
36 var blockingFriends = computed([yourFollowers, yourFollowing, blockers], inAllSets)
37 var mutualFriends = computed([yourFollowers, yourFollowing, followers, following], inAllSets)
38 var outgoingVia = computed([yourFollowers, following], inAllSets)
39 var incomingVia = computed([yourFollowing, followers], inAllSets)
40
41 var hasOutgoing = computed([yourFollowers, following], (a, b) => {
42 return a.some((id) => b.includes(id))
43 })
44 var hasIncoming = computed([followers, yourFollowing], (a, b) => {
45 return a.some((id) => b.includes(id))
46 })
47
48 var isYou = computed([yourId, id], (a, b) => a === b)
49
50 var isNotFollowingAnybody = computed([following, following.sync], (following, sync) => {
51 return sync && (!following || !following.length)
52 })
53
54 var hasNoFollowers = computed(followers, followersList => {
55 return sync && (!followersList || !followersList.length)
56 })
57
58 return {
59 followers,
60 following,
61 blockers,
62 blockingFriends,
63 blockingFriendsCount: count(blockingFriends),
64 mutualFriends,
65 mutualFriendsCount: count(mutualFriends),
66 outgoingVia,
67 outgoingViaCount: count(outgoingVia),
68 incomingVia,
69 incomingViaCount: count(incomingVia),
70 hasOutgoing,
71 isNotFollowingAnybody,
72 hasNoFollowers,
73 noOutgoing: not(hasOutgoing, isYou),
74 hasIncoming,
75 noIncoming: not(hasIncoming, isYou),
76 yourId,
77 yourFollowing,
78 yourFollowers,
79 yourFriends,
80 youFollow,
81 youBlock,
82 isYou,
83 notFollowing: not(youFollow, isYou),
84 sync
85 }
86 })
87}
88
89function inAllSets (first, ...rest) {
90 return first.filter(value => rest.every((collection) => collection.includes(value)))
91}
92
93function not (obs, isFalse) {
94 return computed([obs, isFalse], (x, isFalse) => isFalse ? false : !x)
95}
96
97function count (obs) {
98 return computed(obs, (x) => x.length)
99}
100

Built with git-ssb-web