From 380920f6c85a75071953884e2c09951dabb57970 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sun, 1 Jan 2023 19:19:51 -0700 Subject: [PATCH] Profile template ability --- core/servers/content/web.js | 16 +++- .../servers/content/web_handlers/webfinger.js | 78 ++++++++++++++----- www/wf/profile.template.html | 19 +++++ 3 files changed, 91 insertions(+), 22 deletions(-) create mode 100644 www/wf/profile.template.html diff --git a/core/servers/content/web.js b/core/servers/content/web.js index 84df02b3..7b471dad 100644 --- a/core/servers/content/web.js +++ b/core/servers/content/web.js @@ -27,8 +27,8 @@ const ModuleInfo = (exports.moduleInfo = { }); exports.WellKnownLocations = { - Rfc5785: '/.well-known', // https://www.rfc-editor.org/rfc/rfc5785 - Internal: '/_enig', // location of most enigma provided routes + Rfc5785: '/.well-known', // https://www.rfc-editor.org/rfc/rfc5785 + Internal: '/_enig', // location of most enigma provided routes }; class Route { @@ -375,6 +375,18 @@ exports.getModule = class WebServerModule extends ServerModule { } } + resolveTemplatePath(path) { + if (paths.isAbsolute(path)) { + return path; + } + + const staticRoot = _.get(Config(), 'contentServers.web.staticRoot'); + const resolved = paths.resolve(staticRoot, path); + if (resolved.startsWith(staticRoot)) { + return resolved; + } + } + routeTemplateFilePage(templatePath, preprocessCallback, resp) { const self = this; diff --git a/core/servers/content/web_handlers/webfinger.js b/core/servers/content/web_handlers/webfinger.js index 0b5ebfc9..ee62f948 100644 --- a/core/servers/content/web_handlers/webfinger.js +++ b/core/servers/content/web_handlers/webfinger.js @@ -9,6 +9,10 @@ const _ = require('lodash'); const User = require('../../../user'); const UserProps = require('../../../user_property'); const Log = require('../../../logger').log; +const mimeTypes = require('mime-types'); + +const fs = require('graceful-fs'); +const paths = require('path'); exports.moduleInfo = { name: 'WebFinger', @@ -68,7 +72,7 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule { ws.addRoute({ method: 'GET', - path: /^\/_enig\/profile\//, + path: /^\/_enig\/wf\/@.+$/, handler: this._profileRequestHandler.bind(this), }); @@ -110,27 +114,62 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule { ); } - // TODO: More user information here - let body = ` -User information for: ${user.username} + this._getProfileTemplate((template, mimeType) => { + const varMap = { + USERNAME: user.username, + REAL_NAME: user.getSanitizedName('real'), + LOGIN_COUNT: user.getProperty(UserProps.LoginCount), + AFFILIATIONS: user.getProperty(UserProps.Affiliations) || 'N/A', + ACHIEVEMENT_POINTS: + user.getProperty(UserProps.AchievementTotalPoints) || '0', + }; -Real name: ${user.getProperty(UserProps.RealName)}, -Login Count: ${user.getProperty(UserProps.LoginCount)}, -Affiliations: ${user.getProperty(UserProps.Affiliations)}`; + let body = template; + _.each(varMap, (val, varName) => { + body = body.replace(new RegExp(`%${varName}%`, 'g'), val); + }); - if (user.getProperty(UserProps.AchievementTotalPoints) > 0) { - body = body + `, -Total Points: ${user.getProperty(UserProps.AchievementTotalPoints)} -`; + const headers = { + 'Content-Type': mimeType, + 'Content-Length': body.length, + }; + + resp.writeHead(200, headers); + return resp.end(body); + }); + }); + } + + _getProfileTemplate(cb) { + let templateFile = _.get( + Config(), + 'contentServers.web.handlers.webFinger.profileTemplate' + ); + if (templateFile) { + templateFile = this._webServer().resolveTemplatePath(templateFile); + } + fs.readFile(templateFile || '', 'utf8', (err, data) => { + if (err) { + if (templateFile) { + Log.warn( + { error: err.message }, + `Failed to load profile template "${templateFile}"` + ); + } + + // :TODO: more info in default + return cb( + ` +User information for: %USERNAME% + +Real Name: %REAL_NAME% +Login Count: %LOGIN_COUNT% +Affiliations: %AFFILIATIONS% +Achievement Points: %ACHIEVEMENT_POINTS%`, + 'text/plain' + ); } - - const headers = { - 'Content-Type': 'text/plain', - 'Content-Length': body.length, - }; - - resp.writeHead(200, headers); - return resp.end(body); + return cb(data, mimeTypes.contentType(paths.basename(templateFile))); }); } @@ -242,7 +281,6 @@ Total Points: ${user.getProperty(UserProps.AchievementTotalPoints)} ); } - _getUser(accountName, resp, cb) { User.getUserIdAndName(accountName, (err, userId) => { if (err) { diff --git a/www/wf/profile.template.html b/www/wf/profile.template.html new file mode 100644 index 00000000..5292c17e --- /dev/null +++ b/www/wf/profile.template.html @@ -0,0 +1,19 @@ + + + + + %USERNAME% + + + + +

Profile information for %USERNAME%:

+

+ Real Name %REAL_NAME%
+ Login Count %LOGIN_COUNT%
+ Affils: %AFFILIATIONS%
+ Achievement Points: %ACHIEVEMENT_POINTS%
+

+ + + \ No newline at end of file