refactor: validator check to helper method

This commit is contained in:
Julian Lam
2024-01-04 16:23:09 -05:00
parent 8d4fd9c0f8
commit 2e330d8b3a
3 changed files with 11 additions and 14 deletions

View File

@@ -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) {

View File

@@ -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));

View File

@@ -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');