Edited readme, moved some files and worked a bit on display names.

This commit is contained in:
Daniel Mowitz 2019-09-27 15:49:31 +02:00
parent 6f2d8d257c
commit 028683f963
3 changed files with 116 additions and 55 deletions

View file

@ -1,3 +1,17 @@
# ssb_ap_bridge # ssb_ap_bridge
A Javscript module to implement ActivityPub compatibility to [ssb](https://github.com/ssbc/ssb-db) A Javscript module to implement ActivityPub compatibility to [ssb](https://github.com/ssbc/ssb-db)
Feel free to open a PR or issue!
## What currently works:
- Webfinger detection of known ssb users
- logging AP messages in ssb
## What still needs work:
- Converting messages between the services
- encryption of converted messages

View file

@ -1,66 +1,23 @@
const config = require('./config'); const config = require('./config');
const ssbClient = require('ssb-client'); const ssb_bridge = require('./ssb_bridge');
const pull = require('pull-stream');
const ssb_bridge = require('./save_to_ssb');
DOMAIN = config.DOMAIN; DOMAIN = config.DOMAIN;
let chars_to_encode = "_!*'();:@&=+$,/?#[]"; // basically anything covered by %-encoding plus underscore let chars_to_encode = "_!*'();:@&=+$,/?#[]"; // basically anything covered by %-encoding plus underscore
function encode_webfinger_name(name) {
for (let i in chars_to_encode) {
name = name.replace(chars_to_encode[i], "_" + chars_to_encode[i].charCodeAt(0).toString(16) + "_");
}
return name;
}
function decode_webfinger_name(name) { function decode_webfinger_name(name) {
name = name + "=.ed25519";
for (let i in chars_to_encode) { for (let i in chars_to_encode) {
name = name.replace("_" + chars_to_encode[i].charCodeAt(0).toString(16) + "_", chars_to_encode[i]); name = name.replace("_" + chars_to_encode[i].charCodeAt(0).toString(16) + "_", chars_to_encode[i]);
} }
return name return name
} }
async function check_if_in_friends(name) { function encode_webfinger_name(name) {
let result = false; for (let i in chars_to_encode) {
name = decode_webfinger_name(name); name = name.replace(chars_to_encode[i], "_" + chars_to_encode[i].charCodeAt(0).toString(16) + "_");
let out = new Promise((resolve, reject) => {
ssbClient((err, sbot) => {
if (err) reject(err);
pull(
sbot.friends.createFriendStream(),
pull.collect((err, array) => {
array.forEach(function (actor) {
let short_actor = (actor.substr(1));
if (short_actor === name) {
result = true;
} }
}); return name;
sbot.close();
resolve(result);
})
);
});
});
return await out;
}
async function get_friends() {
let out = new Promise((resolve, reject) => {
ssbClient((err, sbot) => {
if (err) reject(err);
pull(
sbot.friends.createFriendStream(),
pull.collect((err, array) => {
sbot.close();
resolve(array);
})
);
});
});
return await out;
} }
function get_webfinger(req, res) { function get_webfinger(req, res) {
@ -72,7 +29,7 @@ function get_webfinger(req, res) {
name = name.substr(0, name.indexOf('@')); name = name.substr(0, name.indexOf('@'));
let encoded_name = encodeURIComponent(name); let encoded_name = encodeURIComponent(name);
let p = check_if_in_friends(name + "=.ed25519"); let p = ssb_bridge.check_if_in_friends(decode_webfinger_name(name));
p.then((result) => { p.then((result) => {
if (result) { if (result) {
@ -103,11 +60,22 @@ function get_user(req, res) {
return res.status(400).send('Bad request.'); return res.status(400).send('Bad request.');
} else { } else {
let p = check_if_in_friends(name + "=.ed25519"); let p = ssb_bridge.check_if_in_friends(decode_webfinger_name(name));
p.then((result) => { p.then((result) => {
if (result) { if (result) {
let uname_p = ssb_bridge.get_username(decode_webfinger_name(name));
let uname = '';
uname_p.then((uname_res) => {
uname = uname_res;
});
if (uname === '') {
uname = name;
}
res.json( res.json(
{ {
'@context': [ '@context': [
@ -117,7 +85,8 @@ function get_user(req, res) {
id: `https://${DOMAIN}/u/${name}`, id: `https://${DOMAIN}/u/${name}`,
type: 'Person', type: 'Person',
preferredUsername: 'TESTPERSON', //todo: read from latest about message preferredUsername: uname, //todo: read from latest about message
name: uname,
// inbox: `https://${DOMAIN}/u/${name}/inbox`, // inbox: `https://${DOMAIN}/u/${name}/inbox`,
inbox: `https://${DOMAIN}/inbox`, inbox: `https://${DOMAIN}/inbox`,
@ -146,7 +115,7 @@ function get_user(req, res) {
} }
function get_users(req, res) { function get_users(req, res) {
let p = get_friends(); let p = ssb_bridge.get_friends();
p.then((result) => { p.then((result) => {
if (result) { if (result) {

View file

@ -266,6 +266,81 @@ async function restore_ssb_message(id) {
return await out; return await out;
} }
async function check_if_in_friends(name) {
let result = false;
let out = new Promise((resolve, reject) => {
ssbClient((err, sbot) => {
if (err) reject(err);
pull(
sbot.friends.createFriendStream(),
pull.collect((err, array) => {
array.forEach(function (actor) {
let short_actor = (actor.substr(1));
if (short_actor === name) {
result = true;
}
});
sbot.close();
resolve(result);
})
);
});
});
return await out;
}
async function get_friends() {
let out = new Promise((resolve, reject) => {
ssbClient((err, sbot) => {
if (err) reject(err);
pull(
sbot.friends.createFriendStream(),
pull.collect((err, array) => {
sbot.close();
resolve(array);
})
);
});
});
return await out;
}
async function get_username(name) {
let last = null;
let out = new Promise((resolve, reject) => {
ssbClient((err, sbot) => {
pull(
sbot.createLogStream(),
pull.collect(function (err, array) {
sbot.close();
if (err) reject(err);
console.log(array);
console.log(name);
for (let i in array) {
if (array[i].value.content.type === 'about' &&
array[i].value.author.substr(1) === name &&
array[i].value.content.hasOwnProperty('name')) {
last = array[i].value.content.name;
}
}
if (last) {
resolve(last);
} else {
resolve('');
}
}),
);
});
});
return await out;
}
module.exports = { module.exports = {
save: (message) => { save: (message) => {
if (message["@context"] === "https://www.w3.org/ns/activitystreams") { if (message["@context"] === "https://www.w3.org/ns/activitystreams") {
@ -284,5 +359,8 @@ module.exports = {
throw ("Invalid message context."); throw ("Invalid message context.");
} }
}, },
restore: restore_ssb_message restore: restore_ssb_message,
check_if_in_friends,
get_friends,
get_username
}; };