const { keys } = Object const { join } = require('path') const Sbot = require('ssb-client') const avatar = require('ssb-avatar') const waterfall = require('run-waterfall') const parallel = require('run-parallel') // const pull = require('pull-stream') // const writeFile = require('pull-write-file') module.exports = fetchData function fetchData (cb) { waterfall([ Sbot, (sbot, cb) => { // fetch friend graph sbot.friends.all((err, friends) => { cb(err, sbot, friends) }) }, (sbot, friends, cb) => { // fetch profiles (name and avatar) return parallel( keys(friends).map(id => { const friendOfFriends = keys(friends[id]) .filter(foafId => friends[id][foafId] === true) return (cb) => { avatar(sbot, id, id, (err, { name, image }) => { if (err) cb(err) else cb(null, { id, friends: friendOfFriends, about: { name, image } }) }) } }) , (err, friends) => { cb(err, sbot, friends) }) }, /* (sbot, friends, cb) => { // fetch images return parallel( friends.map(friend => { const imageBlobId = friend.about.image if (imageBlobId) { const imageId = pathFriendlyId(imageBlobId) const imagePath = join(__dirname, 'output', 'images', imageId) const imageUrl = `/images/${imageId}` return (cb) => { pull( sbot.blobs.get(imageBlobId), writeFile(imagePath, function (err) { if (err) cb(err) else cb(null, { id: friend.id, friends: friend.friends, about: { name: friend.about.name, image: imageUrl } }) }) ) } } else return (cb) => cb(null, friend) }) ) }, */ (sbot, friends, cb) => { sbot.close(err => { cb(err, friends) }) } ], cb) } /* function pathFriendlyId (str) { return str .replace(/\+/g, '-') .replace(/\//g, '_') } */