From 2e330d8b3add876f68baba8d9338f366f37e10ef Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 4 Jan 2024 16:23:09 -0500 Subject: [PATCH] refactor: validator check to helper method --- src/activitypub/helpers.js | 16 +++++++++------- src/activitypub/index.js | 8 +------- src/user/data.js | 1 + 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/activitypub/helpers.js b/src/activitypub/helpers.js index 92aafec88e..23ea97bfd3 100644 --- a/src/activitypub/helpers.js +++ b/src/activitypub/helpers.js @@ -14,6 +14,14 @@ const webfingerCache = ttl({ ttl: 1000 * 60 * 60 * 24 }); // 24 hours const Helpers = module.exports; +Helpers.isUri = value => validator.isURL(value, { + require_protocol: true, + require_host: true, + protocols: ['https'], + require_valid_protocol: true, + require_tld: false, // temporary — for localhost +}); + Helpers.query = async (id) => { const [username, hostname] = id.split('@'); if (!username || !hostname) { @@ -71,13 +79,7 @@ Helpers.resolveLocalUid = async (input) => { if (process.env.CI === 'true') { protocols.push('http'); } - if (validator.isURL(input, { - require_protocol: true, - require_host: true, - require_tld: false, - protocols, - require_valid_protocol: true, - })) { + if (Helpers.isUri(input)) { const { host, pathname } = new URL(input); if (host === nconf.get('url_parsed').host) { diff --git a/src/activitypub/index.js b/src/activitypub/index.js index cad7c5efaf..0caec6b9ff 100644 --- a/src/activitypub/index.js +++ b/src/activitypub/index.js @@ -18,13 +18,7 @@ ActivityPub.inbox = require('./inbox'); ActivityPub.getActor = async (input) => { // Can be a webfinger id, uri, or object, handle as appropriate let uri; - if (validator.isURL(input, { - require_protocol: true, - require_host: true, - protocols: ['https'], - require_valid_protocol: true, - require_tld: false, - })) { + if (ActivityPub.helpers.isUri(input)) { uri = input; } else if (input.indexOf('@') !== -1) { // Webfinger ({ actorUri: uri } = await ActivityPub.helpers.query(input)); diff --git a/src/user/data.js b/src/user/data.js index e21cf92921..f5fa8ba8a3 100644 --- a/src/user/data.js +++ b/src/user/data.js @@ -7,6 +7,7 @@ const _ = require('lodash'); const db = require('../database'); const meta = require('../meta'); const plugins = require('../plugins'); +const activitypub = require('../activitypub'); const utils = require('../utils'); const relative_path = nconf.get('relative_path');