git ssb

10+

Matt McKegg / patchwork



Tree: ef4158cecbf339ae1458111a2ff1218a55bef5f6

Files: ef4158cecbf339ae1458111a2ff1218a55bef5f6 / modules / profile / obs / contact.js

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

Built with git-ssb-web