69 lines
2.2 KiB
JavaScript
69 lines
2.2 KiB
JavaScript
// scuttlebot.js
|
|
|
|
const ssbClient = require('ssb-client');
|
|
const ssbKeys = require('ssb-keys');
|
|
const pull = require('pull-stream');
|
|
|
|
const keys = ssbKeys.loadOrCreateSync('~/.ssb/secret');
|
|
|
|
ssbClient(keys, (err, sbot) => {
|
|
if (err) {
|
|
console.error(JSON.stringify({ "error": "Failed to connect to the Scuttlebot server. Is it running?" }));
|
|
console.error(err);
|
|
process.exit(1);
|
|
}
|
|
|
|
const authors = new Set();
|
|
const aliases = new Map();
|
|
|
|
pull(
|
|
sbot.messagesByType("contact"),
|
|
pull.drain((msg) => {
|
|
authors.add(msg.value.author);
|
|
}, (err) => {
|
|
if (err) {
|
|
console.error(JSON.stringify({ "error": "Failed to retrieve messages." }));
|
|
console.error(err);
|
|
process.exit(1);
|
|
}
|
|
|
|
// Fetch aliases for all authors
|
|
pull(
|
|
sbot.query.read({
|
|
query: [{
|
|
$filter: {
|
|
value: {
|
|
content: { type: 'about' }
|
|
}
|
|
}
|
|
}]
|
|
}),
|
|
pull.drain((msg) => {
|
|
if (msg.value.content.about && msg.value.content.name) {
|
|
aliases.set(msg.value.content.about, msg.value.content.name);
|
|
}
|
|
}, (err) => {
|
|
if (err) {
|
|
console.error(JSON.stringify({ "error": "Failed to retrieve aliases." }));
|
|
console.error(err);
|
|
process.exit(1);
|
|
}
|
|
|
|
const feeds = Array.from(authors).map(author => {
|
|
return {
|
|
id: author,
|
|
alias: aliases.get(author) || ''
|
|
}
|
|
});
|
|
console.log(JSON.stringify(feeds));
|
|
try {
|
|
sbot.close(() => {});
|
|
} catch(err) {
|
|
console.error("Error closing SSB server connection: ", err);
|
|
}
|
|
})
|
|
);
|
|
})
|
|
);
|
|
});
|