From 127e9794ee68dbd9a92970d8a828f6ffa2266532 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Tue, 3 Jan 2023 15:10:39 -0700 Subject: [PATCH] Central check for enable/disabled web handlers, added some utility functions, etc. --- core/activitypub_util.js | 9 +++++++++ core/servers/content/web.js | 11 ++++++++--- core/servers/content/web_handlers/webfinger.js | 11 +++-------- 3 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 core/activitypub_util.js diff --git a/core/activitypub_util.js b/core/activitypub_util.js new file mode 100644 index 00000000..68072a2f --- /dev/null +++ b/core/activitypub_util.js @@ -0,0 +1,9 @@ +const { WellKnownLocations } = require('./servers/content/web'); + +exports.buildSelfUrl = buildSelfUrl; + +function buildSelfUrl(webServer, user, relPrefix) { + return webServer.buildUrl( + WellKnownLocations.Internal + `${relPrefix}${user.username}` + ); +} diff --git a/core/servers/content/web.js b/core/servers/content/web.js index 7b471dad..cbed4dde 100644 --- a/core/servers/content/web.js +++ b/core/servers/content/web.js @@ -5,7 +5,9 @@ const Log = require('../../logger.js').log; const ServerModule = require('../../server_module.js').ServerModule; const Config = require('../../config.js').get; -const { Errors } = require('../../enig_error.js'); +const { Errors, ErrorReasons } = require('../../enig_error.js'); +const { loadModulesForCategory, moduleCategories } = require('../../module_util'); +const WebHandlerModule = require('../../web_handler_module'); // deps const http = require('http'); @@ -17,8 +19,6 @@ const mimeTypes = require('mime-types'); const forEachSeries = require('async/forEachSeries'); const findSeries = require('async/findSeries'); -const { loadModulesForCategory, moduleCategories } = require('../../module_util'); - const ModuleInfo = (exports.moduleInfo = { name: 'Web', desc: 'Web Server', @@ -166,6 +166,11 @@ exports.getModule = class WebServerModule extends ServerModule { (module, nextModule) => { const moduleInst = new module.getModule(); try { + const normalizedName = _.camelCase(module.moduleInfo.name); + if (!WebHandlerModule.isEnabled(normalizedName)) { + return nextModule(null); + } + moduleInst.init(err => { return nextModule(err); }); diff --git a/core/servers/content/web_handlers/webfinger.js b/core/servers/content/web_handlers/webfinger.js index 146b3aaa..e7e95353 100644 --- a/core/servers/content/web_handlers/webfinger.js +++ b/core/servers/content/web_handlers/webfinger.js @@ -2,6 +2,7 @@ const WebHandlerModule = require('../../../web_handler_module'); const Config = require('../../../config').get; const { Errors, ErrorReasons } = require('../../../enig_error'); const { WellKnownLocations } = require('../web'); +const { buildSelfUrl } = require('../../../activitypub_util'); const _ = require('lodash'); const User = require('../../../user'); @@ -17,7 +18,7 @@ exports.moduleInfo = { name: 'WebFinger', desc: 'A simple WebFinger Handler.', author: 'NuSkooler, CognitiveGears', - packageName: 'codes.l33t.enigma.web.handler.finger', + packageName: 'codes.l33t.enigma.web.handler.webfinger', }; // :TODO: more info in default @@ -41,10 +42,6 @@ exports.getModule = class WebFingerWebHandler extends WebHandlerModule { init(cb) { const config = Config(); - if (!WebHandlerModule.isEnabled('webFinger')) { - return cb(null); - } - // we rely on the web server this.webServer = WebHandlerModule.getWebServer(); if (!this.webServer || !this.webServer.isEnabled()) { @@ -251,9 +248,7 @@ exports.getModule = class WebFingerWebHandler extends WebHandlerModule { } _selfUrl(user) { - return this.webServer.buildUrl( - WellKnownLocations.Internal + `/ap/users/${user.username}` - ); + return buildSelfUrl(this.webServer, user, '/ap/users/'); } // :TODO: only if ActivityPub is enabled