Files: bdd674e048f8917357c9360017872a4d20836af0 / data.js
2360 bytesRaw
1 | const { keys } = Object |
2 | const { join } = require('path') |
3 | const Sbot = require('ssb-client') |
4 | const avatar = require('ssb-avatar') |
5 | const waterfall = require('run-waterfall') |
6 | const parallel = require('run-parallel') |
7 | // const pull = require('pull-stream') |
8 | // const writeFile = require('pull-write-file') |
9 | |
10 | module.exports = fetchData |
11 | |
12 | function fetchData (cb) { |
13 | waterfall([ |
14 | Sbot, |
15 | (sbot, cb) => { |
16 | // fetch friend graph |
17 | sbot.friends.all((err, friends) => { |
18 | cb(err, sbot, friends) |
19 | }) |
20 | }, |
21 | (sbot, friends, cb) => { |
22 | // fetch profiles (name and avatar) |
23 | return parallel( |
24 | keys(friends).map(id => { |
25 | const friendOfFriends = keys(friends[id]) |
26 | .filter(foafId => friends[id][foafId] === true) |
27 | return (cb) => { |
28 | avatar(sbot, id, id, (err, { name, image }) => { |
29 | if (err) cb(err) |
30 | else cb(null, { |
31 | id, |
32 | friends: friendOfFriends, |
33 | about: { |
34 | name, |
35 | image |
36 | } |
37 | }) |
38 | }) |
39 | } |
40 | }) |
41 | , (err, friends) => { |
42 | cb(err, sbot, friends) |
43 | }) |
44 | }, |
45 | /* |
46 | (sbot, friends, cb) => { |
47 | // fetch images |
48 | return parallel( |
49 | friends.map(friend => { |
50 | const imageBlobId = friend.about.image |
51 | if (imageBlobId) { |
52 | const imageId = pathFriendlyId(imageBlobId) |
53 | const imagePath = join(__dirname, 'output', 'images', imageId) |
54 | const imageUrl = `/images/${imageId}` |
55 | return (cb) => { |
56 | pull( |
57 | sbot.blobs.get(imageBlobId), |
58 | writeFile(imagePath, function (err) { |
59 | if (err) cb(err) |
60 | else cb(null, { |
61 | id: friend.id, |
62 | friends: friend.friends, |
63 | about: { |
64 | name: friend.about.name, |
65 | image: imageUrl |
66 | } |
67 | }) |
68 | }) |
69 | ) |
70 | } |
71 | } else return (cb) => cb(null, friend) |
72 | }) |
73 | ) |
74 | }, |
75 | */ |
76 | (sbot, friends, cb) => { |
77 | sbot.close(err => { |
78 | cb(err, friends) |
79 | }) |
80 | } |
81 | ], cb) |
82 | } |
83 | |
84 | /* |
85 | function pathFriendlyId (str) { |
86 | return str |
87 | .replace(/\+/g, '-') |
88 | .replace(/\//g, '_') |
89 | } |
90 | */ |
91 |
Built with git-ssb-web