Commit 41ec31f643175bf360ff1416e6448bffcccb4212
factor out building of nodes + links
mix irving committed on 12/7/2016, 8:43:20 AMParent: 0acc3cf490d4cdb39255ca110e2975c7c7d40768
Files changed
graph.js | changed |
index.js | changed |
package.json | changed |
graph.js | ||
---|---|---|
@@ -1,33 +1,64 @@ | ||
1 | 1 … | const pull = require('pull-stream') |
2 | 2 … | const Sbot = require('ssb-client') |
3 … | +const avatar = require('ssb-avatar') | |
3 | 4 … | const waterfall = require('run-waterfall') |
4 | 5 … | |
6 … | +const { keys } = Object | |
7 … | + | |
5 | 8 … | module.exports = Graph |
6 | 9 … | |
7 | 10 … | function Graph (sbot, cb) { |
8 | 11 … | waterfall([ |
9 | 12 … | (cb) => sbot.friends.all(cb), |
10 | 13 … | (friends, cb) => { |
14 … | + const filteredFriends = friends // activeFriends(friends) | |
15 … | + | |
11 | 16 … | cb(null, { |
12 | - nodes: Object.keys(friends).map(id => ({ id })), | |
13 | - links: Object.keys(friends).reduce((sofar, friend) => { | |
14 | - const friendOfFriends = Object.keys(friends[friend]) | |
15 | - .filter(id => friends[friend][id] === true) | |
16 | - const edges = friendOfFriends.map(friendOfFriend => { | |
17 | - return { | |
18 | - fromId: friend, | |
19 | - toId: friendOfFriend, | |
20 | - data: { | |
21 | - hidden: true | |
22 | - } | |
23 | - } | |
24 | - }) | |
25 | - return [ | |
26 | - ...sofar, | |
27 | - ...edges | |
28 | - ] | |
29 | - }, []) | |
17 … | + nodes: buildNodes(filteredFriends), | |
18 … | + links: buildLinks(filteredFriends) | |
30 | 19 … | }) |
31 | 20 … | } |
32 | 21 … | ], cb) |
33 | 22 … | } |
23 … | + | |
24 … | +function buildNodes(friends) { | |
25 … | + return keys(friends).map(id => ({ id }) ) | |
26 … | +} | |
27 … | + | |
28 … | +function buildLinks(friends) { | |
29 … | + return keys(friends).reduce((sofar, friend) => { | |
30 … | + const friendOfFriends = keys(friends[friend]) | |
31 … | + .filter(id => friends[friend][id] === true) | |
32 … | + // .filter(id => keys(friends).indexOf(id) > -1) | |
33 … | + | |
34 … | + const edges = friendOfFriends.map(friendOfFriend => { | |
35 … | + return { | |
36 … | + fromId: friend, | |
37 … | + toId: friendOfFriend, | |
38 … | + data: { | |
39 … | + hidden: true | |
40 … | + } | |
41 … | + } | |
42 … | + }) | |
43 … | + | |
44 … | + return [ | |
45 … | + ...sofar, | |
46 … | + ...edges | |
47 … | + ] | |
48 … | + }, []) | |
49 … | + | |
50 … | +} | |
51 … | + | |
52 … | +function activeFriends (friends) { | |
53 … | + return keys(friends) | |
54 … | + .reduce( | |
55 … | + (sofar, current) => { | |
56 … | + if (keys(friends[current]).length < 4) return sofar | |
57 … | + | |
58 … | + sofar[current] = friends[current] | |
59 … | + return sofar | |
60 … | + }, | |
61 … | + {} | |
62 … | + ) | |
63 … | +} | |
64 … | + |
Built with git-ssb-web