Skip to content

Node.js based forum software built for the modern web

feat: ability to query remote users by webfinger handle

Julian Lam9761526710cd
diff --git a/src/activitypub/actors.js b/src/activitypub/actors.js
index 5477d79833..6f71f98e2d 100644
--- a/src/activitypub/actors.js
+++ b/src/activitypub/actors.js
@@ -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';
}
diff --git a/src/activitypub/helpers.js b/src/activitypub/helpers.js
index 34141de97a..c6f8e5c451 100644
--- a/src/activitypub/helpers.js
+++ b/src/activitypub/helpers.js
@@ -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
diff --git a/src/user/search.js b/src/user/search.js
index e613513e92..9f9ab2cb02 100644
--- a/src/user/search.js
+++ b/src/user/search.js
@@ -41,12 +41,15 @@ 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 (assertion === true) {
- uids = [query];
- } else if (Array.isArray(assertion) && assertion.length) {
- uids = assertion.map(u => u.id);
+ 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 = [handle ? await User.getUidByUserslug(handle) : query];
+ } else if (Array.isArray(assertion) && assertion.length) {
+ uids = assertion.map(u => u.id);
+ }
}
}