git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: fd15190d894f51e76a7c0fd5f53b0db037f171b8

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

2257 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 hasOutgoing = computed([yourFollowers, following], (a, b) => {
37 return a.some((id) => b.includes(id))
38 })
39 var hasIncoming = computed([followers, yourFollowing], (a, b) => {
40 return a.some((id) => b.includes(id))
41 })
42
43 var isYou = computed([yourId, id], (a, b) => a === b)
44
45 return {
46 followers,
47 following,
48 blockers,
49 blockingFriends,
50 blockingFriendsCount: count(blockingFriends),
51 mutualFriends,
52 mutualFriendsCount: count(mutualFriends),
53 hasOutgoing,
54 noOutgoing: not(hasOutgoing, isYou),
55 hasIncoming,
56 noIncoming: not(hasIncoming, isYou),
57 yourId,
58 yourFollowing,
59 yourFollowers,
60 youFollow,
61 youBlock,
62 isYou,
63 notFollowing: not(youFollow, isYou),
64 sync
65 }
66 })
67}
68
69function inAllSets (first, ...rest) {
70 return first.filter(value => rest.every((collection) => collection.includes(value)))
71}
72
73function not (obs, isFalse) {
74 return computed([obs, isFalse], (x, isFalse) => isFalse ? false : !x)
75}
76
77function count (obs) {
78 return computed(obs, (x) => x.length)
79}
80

Built with git-ssb-web