feat: ability to query remote users by webfinger handle

This commit is contained in:
Julian Lam
2024-05-07 10:11:36 -04:00
parent a7aeabc80d
commit 9761526710
3 changed files with 27 additions and 10 deletions

View File

@@ -31,10 +31,8 @@ Actors.assert = async (ids, options = {}) => {
// Translate webfinger handles to uris
ids = (await Promise.all(ids.map(async (id) => {
const originalId = id;
const isUri = activitypub.helpers.isUri(id);
// only look up webfinger if the id is not a supported URI
if (id.includes('@') && !isUri) {
const host = isUri ? new URL(id).host : id.split('@')[1];
if (activitypub.helpers.isWebfinger(id)) {
const host = id.split('@')[1];
if (host === nconf.get('url_parsed').host) { // do not assert loopback ids
return 'loopback';
}

View File

@@ -15,6 +15,7 @@ const ttl = require('../cache/ttl');
const user = require('../user');
const activitypub = require('.');
const webfingerRegex = /^(@|acct:)?\w+@.+$/;
const webfingerCache = ttl({ ttl: 1000 * 60 * 60 * 24 }); // 24 hours
const Helpers = module.exports;
@@ -33,6 +34,21 @@ Helpers.isUri = (value) => {
});
};
Helpers.isWebfinger = (value) => {
// N.B. returns normalized handle, so truthy check!
if (webfingerRegex.test(value) && !Helpers.isUri(value)) {
if (value.startsWith('@')) {
return value.slice(1);
} else if (value.startsWith('acct:')) {
return value.slice(5);
}
return value;
}
return false;
};
Helpers.query = async (id) => {
const isUri = Helpers.isUri(id);
// username@host ids use acct: URI schema

View File

@@ -41,14 +41,17 @@ module.exports = function (User) {
} else if (searchBy === 'uid') {
uids = [query];
} else {
if (!data.findUids && data.uid && activitypub.helpers.isUri(data.query)) {
const assertion = await activitypub.actors.assert([data.query]);
if (!data.findUids && data.uid) {
const handle = activitypub.helpers.isWebfinger(data.query);
if (handle || activitypub.helpers.isUri(data.query)) {
const assertion = await activitypub.actors.assert([handle || data.query]);
if (assertion === true) {
uids = [query];
uids = [handle ? await User.getUidByUserslug(handle) : query];
} else if (Array.isArray(assertion) && assertion.length) {
uids = assertion.map(u => u.id);
}
}
}
if (!uids.length) {
const searchMethod = data.findUids || findUids;