Files: 41ec31f643175bf360ff1416e6448bffcccb4212 / graph.js
1318 bytesRaw
1 | const pull = require('pull-stream') |
2 | const Sbot = require('ssb-client') |
3 | const avatar = require('ssb-avatar') |
4 | const waterfall = require('run-waterfall') |
5 | |
6 | const { keys } = Object |
7 | |
8 | module.exports = Graph |
9 | |
10 | function Graph (sbot, cb) { |
11 | waterfall([ |
12 | (cb) => sbot.friends.all(cb), |
13 | (friends, cb) => { |
14 | const filteredFriends = friends // activeFriends(friends) |
15 | |
16 | cb(null, { |
17 | nodes: buildNodes(filteredFriends), |
18 | links: buildLinks(filteredFriends) |
19 | }) |
20 | } |
21 | ], cb) |
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 | |
65 |
Built with git-ssb-web