From 344d4716ceb8370493025e14a7061b6ebdd89381 Mon Sep 17 00:00:00 2001 From: Nathan Byrd Date: Fri, 6 Jan 2023 13:15:29 -0600 Subject: [PATCH] Added ability to use .json to get JSON from self URL. Also added some logging. --- core/servers/content/web.js | 63 ++++++++++--------- .../content/web_handlers/activitypub.js | 25 +++++++- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/core/servers/content/web.js b/core/servers/content/web.js index 6631ea5c..f26ecc53 100644 --- a/core/servers/content/web.js +++ b/core/servers/content/web.js @@ -5,7 +5,7 @@ const Log = require('../../logger.js').log; const ServerModule = require('../../server_module.js').ServerModule; const Config = require('../../config.js').get; -const { Errors, ErrorReasons } = require('../../enig_error.js'); +const { Errors } = require('../../enig_error.js'); const { loadModulesForCategory, moduleCategories } = require('../../module_util'); const WebHandlerModule = require('../../web_handler_module'); @@ -49,18 +49,18 @@ class Route { isValid() { return ( (this.pathRegExp instanceof RegExp && - -1 !== - [ - 'GET', - 'HEAD', - 'POST', - 'PUT', - 'DELETE', - 'CONNECT', - 'OPTIONS', - 'TRACE', - ].indexOf(this.method)) || - !_.isFunction(this.handler) + -1 !== + [ + 'GET', + 'HEAD', + 'POST', + 'PUT', + 'DELETE', + 'CONNECT', + 'OPTIONS', + 'TRACE', + ].indexOf(this.method)) || + !_.isFunction(this.handler) ); } @@ -96,13 +96,13 @@ exports.getModule = class WebServerModule extends ServerModule { } buildUrl(pathAndQuery) { - // - // Create a URL such as - // https://l33t.codes:44512/ + |pathAndQuery| - // - // Prefer HTTPS over HTTP. Be explicit about the port - // only if non-standard. Allow users to override full prefix in config. - // + // + // Create a URL such as + // https://l33t.codes:44512/ + |pathAndQuery| + // + // Prefer HTTPS over HTTP. Be explicit about the port + // only if non-standard. Allow users to override full prefix in config. + // const config = Config(); if (_.isString(config.contentServers.web.overrideUrlPrefix)) { return `${config.contentServers.web.overrideUrlPrefix}${pathAndQuery}`; @@ -113,15 +113,15 @@ exports.getModule = class WebServerModule extends ServerModule { if (config.contentServers.web.https.enabled) { schema = 'https://'; port = - 443 === config.contentServers.web.https.port - ? '' - : `:${config.contentServers.web.https.port}`; + 443 === config.contentServers.web.https.port + ? '' + : `:${config.contentServers.web.https.port}`; } else { schema = 'http://'; port = - 80 === config.contentServers.web.http.port - ? '' - : `:${config.contentServers.web.http.port}`; + 80 === config.contentServers.web.http.port + ? '' + : `:${config.contentServers.web.http.port}`; } return `${schema}${config.contentServers.web.domain}${port}${pathAndQuery}`; @@ -168,9 +168,12 @@ exports.getModule = class WebServerModule extends ServerModule { try { const normalizedName = _.camelCase(module.moduleInfo.name); if (!WebHandlerModule.isEnabled(normalizedName)) { + Log.info({ moduleName: normalizedName }, 'Skipping web handler module - not enabled.'); return nextModule(null); } + Log.info({ moduleName: normalizedName }, 'Initializing web handler module.'); + moduleInst.init(err => { return nextModule(err); }); @@ -326,8 +329,8 @@ exports.getModule = class WebServerModule extends ServerModule { const headers = { 'Content-Type': - mimeTypes.contentType(paths.basename(filePath)) || - mimeTypes.contentType('.bin'), + mimeTypes.contentType(paths.basename(filePath)) || + mimeTypes.contentType('.bin'), 'Content-Length': stats.size, }; @@ -359,8 +362,8 @@ exports.getModule = class WebServerModule extends ServerModule { const headers = { 'Content-Type': - mimeTypes.contentType(paths.basename(filePath)) || - mimeTypes.contentType('.bin'), + mimeTypes.contentType(paths.basename(filePath)) || + mimeTypes.contentType('.bin'), 'Content-Length': stats.size, }; diff --git a/core/servers/content/web_handlers/activitypub.js b/core/servers/content/web_handlers/activitypub.js index 470adecc..01770cba 100644 --- a/core/servers/content/web_handlers/activitypub.js +++ b/core/servers/content/web_handlers/activitypub.js @@ -10,6 +10,7 @@ const { const UserProps = require('../../../user_property'); const { Errors } = require('../../../enig_error'); const Config = require('../../../config').get; +const Log = require('../../../logger').log; // deps const _ = require('lodash'); @@ -31,6 +32,7 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule { if (!this.webServer) { return cb(Errors.UnexpectedState('Cannot access web server!')); } + Log.debug('Adding route for ActivityPub'); this.webServer.addRoute({ method: 'GET', @@ -42,20 +44,37 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule { } _selfUrlRequestHandler(req, resp) { + Log.debug({ url: req.url }, 'Received request for self url'); const url = new URL(req.url, `https://${req.headers.host}`); - const accountName = url.pathname.substring(url.pathname.lastIndexOf('/') + 1); + let accountName = url.pathname.substring(url.pathname.lastIndexOf('/') + 1); + let sendActor = false; + + // Like Mastodon, if .json is appended onto URL then return the JSON content + if (accountName.endsWith('.json')) { + sendActor = true; + accountName = accountName.slice(0, -5); + } + Log.debug({ accountName: accountName }, 'Retrieving self url for account.'); userFromAccount(accountName, (err, user) => { if (err) { + Log.info({ accountName: accountName }, 'Unable to find user from account retrieving self url.'); return this._notFound(resp); } const accept = req.headers['accept'] || '*/*'; if (accept === 'application/activity+json') { - return this._selfAsActorHandler(user, req, resp); + sendActor = true; } - return this._standardSelfHandler(user, req, resp); + Log.debug({ sendActor: sendActor }, 'Sending actor JSON'); + + if (sendActor) { + return this._selfAsActorHandler(user, req, resp); + } + else { + return this._standardSelfHandler(user, req, resp); + } }); }