Profile template ability

This commit is contained in:
Bryan Ashby 2023-01-01 19:19:51 -07:00
parent db652bff59
commit 380920f6c8
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
3 changed files with 91 additions and 22 deletions

View File

@ -27,8 +27,8 @@ const ModuleInfo = (exports.moduleInfo = {
}); });
exports.WellKnownLocations = { exports.WellKnownLocations = {
Rfc5785: '/.well-known', // https://www.rfc-editor.org/rfc/rfc5785 Rfc5785: '/.well-known', // https://www.rfc-editor.org/rfc/rfc5785
Internal: '/_enig', // location of most enigma provided routes Internal: '/_enig', // location of most enigma provided routes
}; };
class Route { 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) { routeTemplateFilePage(templatePath, preprocessCallback, resp) {
const self = this; const self = this;

View File

@ -9,6 +9,10 @@ const _ = require('lodash');
const User = require('../../../user'); const User = require('../../../user');
const UserProps = require('../../../user_property'); const UserProps = require('../../../user_property');
const Log = require('../../../logger').log; const Log = require('../../../logger').log;
const mimeTypes = require('mime-types');
const fs = require('graceful-fs');
const paths = require('path');
exports.moduleInfo = { exports.moduleInfo = {
name: 'WebFinger', name: 'WebFinger',
@ -68,7 +72,7 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule {
ws.addRoute({ ws.addRoute({
method: 'GET', method: 'GET',
path: /^\/_enig\/profile\//, path: /^\/_enig\/wf\/@.+$/,
handler: this._profileRequestHandler.bind(this), handler: this._profileRequestHandler.bind(this),
}); });
@ -110,27 +114,62 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule {
); );
} }
// TODO: More user information here this._getProfileTemplate((template, mimeType) => {
let body = ` const varMap = {
User information for: ${user.username} 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)}, let body = template;
Login Count: ${user.getProperty(UserProps.LoginCount)}, _.each(varMap, (val, varName) => {
Affiliations: ${user.getProperty(UserProps.Affiliations)}`; body = body.replace(new RegExp(`%${varName}%`, 'g'), val);
});
if (user.getProperty(UserProps.AchievementTotalPoints) > 0) { const headers = {
body = body + `, 'Content-Type': mimeType,
Total Points: ${user.getProperty(UserProps.AchievementTotalPoints)} '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'
);
} }
return cb(data, mimeTypes.contentType(paths.basename(templateFile)));
const headers = {
'Content-Type': 'text/plain',
'Content-Length': body.length,
};
resp.writeHead(200, headers);
return resp.end(body);
}); });
} }
@ -242,7 +281,6 @@ Total Points: ${user.getProperty(UserProps.AchievementTotalPoints)}
); );
} }
_getUser(accountName, resp, cb) { _getUser(accountName, resp, cb) {
User.getUserIdAndName(accountName, (err, userId) => { User.getUserIdAndName(accountName, (err, userId) => {
if (err) { if (err) {

View File

@ -0,0 +1,19 @@
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<title>%USERNAME%</title>
<meta name="description" content="Profile of %USERNAME% of %BOARDNAME%">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Profile information for %USERNAME%:</h1>
<p>
<b>Real Name</b> %REAL_NAME%<br>
<b>Login Count</b> %LOGIN_COUNT%<br>
<b>Affils</b>: %AFFILIATIONS%<br>
<b>Achievement Points</b>: %ACHIEVEMENT_POINTS%<br>
</p>
</body>
</html>