git ssb

10+

Matt McKegg / patchwork



Tree: 5bf7aadafd4c7fe94a9bc4fead2e244b49cde8ca

Files: 5bf7aadafd4c7fe94a9bc4fead2e244b49cde8ca / modules / profile / obs / contact.js

2803 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 return {
55 followers,
56 following,
57 blockers,
58 blockingFriends,
59 blockingFriendsCount: count(blockingFriends),
60 mutualFriends,
61 mutualFriendsCount: count(mutualFriends),
62 outgoingVia,
63 outgoingViaCount: count(outgoingVia),
64 incomingVia,
65 incomingViaCount: count(incomingVia),
66 hasOutgoing,
67 isNotFollowingAnybody,
68 noOutgoing: not(hasOutgoing, isYou),
69 hasIncoming,
70 noIncoming: not(hasIncoming, isYou),
71 yourId,
72 yourFollowing,
73 yourFollowers,
74 yourFriends,
75 youFollow,
76 youBlock,
77 isYou,
78 notFollowing: not(youFollow, isYou),
79 sync
80 }
81 })
82}
83
84function inAllSets (first, ...rest) {
85 return first.filter(value => rest.every((collection) => collection.includes(value)))
86}
87
88function not (obs, isFalse) {
89 return computed([obs, isFalse], (x, isFalse) => isFalse ? false : !x)
90}
91
92function count (obs) {
93 return computed(obs, (x) => x.length)
94}
95

Built with git-ssb-web