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

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

View File

@ -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,28 +114,63 @@ 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)}`;
if (user.getProperty(UserProps.AchievementTotalPoints) > 0) {
body = body + `,
Total Points: ${user.getProperty(UserProps.AchievementTotalPoints)}
`;
}
let body = template;
_.each(varMap, (val, varName) => {
body = body.replace(new RegExp(`%${varName}%`, 'g'), val);
});
const headers = {
'Content-Type': 'text/plain',
'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'
);
}
return cb(data, mimeTypes.contentType(paths.basename(templateFile)));
});
}
_webFingerRequestHandler(req, resp) {
@ -242,7 +281,6 @@ Total Points: ${user.getProperty(UserProps.AchievementTotalPoints)}
);
}
_getUser(accountName, resp, cb) {
User.getUserIdAndName(accountName, (err, userId) => {
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>