enigma-bbs/core/activitypub/util.js

184 lines
6.1 KiB
JavaScript
Raw Normal View History

2023-01-13 06:19:52 +00:00
const { WellKnownLocations } = require('../servers/content/web');
const User = require('../user');
const { Errors, ErrorReasons } = require('../enig_error');
const UserProps = require('../user_property');
const ActivityPubSettings = require('./settings');
2023-01-05 03:29:18 +00:00
// deps
const _ = require('lodash');
const mimeTypes = require('mime-types');
const waterfall = require('async/waterfall');
const fs = require('graceful-fs');
const paths = require('path');
const moment = require('moment');
const { striptags } = require('striptags');
2023-01-05 03:29:18 +00:00
exports.ActivityStreamsContext = 'https://www.w3.org/ns/activitystreams';
exports.isValidLink = isValidLink;
2023-01-22 20:51:32 +00:00
exports.makeSharedInboxUrl = makeSharedInboxUrl;
exports.makeUserUrl = makeUserUrl;
exports.webFingerProfileUrl = webFingerProfileUrl;
exports.selfUrl = selfUrl;
exports.userFromAccount = userFromAccount;
2023-01-08 08:22:02 +00:00
exports.accountFromSelfUrl = accountFromSelfUrl;
2023-01-05 03:29:18 +00:00
exports.getUserProfileTemplatedBody = getUserProfileTemplatedBody;
exports.messageBodyToHtml = messageBodyToHtml;
exports.htmlToMessageBody = htmlToMessageBody;
2023-01-05 03:29:18 +00:00
// :TODO: more info in default
// this profile template is the *default* for both WebFinger
// profiles and 'self' requests without the
// Accept: application/activity+json headers present
exports.DefaultProfileTemplate = `
User information for: %USERNAME%
Real Name: %REAL_NAME%
Login Count: %LOGIN_COUNT%
Affiliations: %AFFILIATIONS%
Achievement Points: %ACHIEVEMENT_POINTS%
`;
function isValidLink(l) {
return /^https?:\/\/.+$/.test(l);
}
2023-01-22 20:51:32 +00:00
function makeSharedInboxUrl(webServer) {
return webServer.buildUrl(WellKnownLocations.Internal + '/ap/shared-inbox');
}
function makeUserUrl(webServer, user, relPrefix) {
return webServer.buildUrl(
WellKnownLocations.Internal + `${relPrefix}${user.username}`
);
}
function webFingerProfileUrl(webServer, user) {
return webServer.buildUrl(WellKnownLocations.Internal + `/wf/@${user.username}`);
}
function selfUrl(webServer, user) {
return makeUserUrl(webServer, user, '/ap/users/');
}
2023-01-08 08:22:02 +00:00
function accountFromSelfUrl(url) {
// https://some.l33t.enigma.board/_enig/ap/users/Masto -> Masto
// :TODO: take webServer, and just take path-to-users.length +1
2023-01-08 08:22:02 +00:00
return url.substring(url.lastIndexOf('/') + 1);
}
function userFromAccount(accountName, cb) {
if (accountName.startsWith('@')) {
accountName = accountName.slice(1);
}
User.getUserIdAndName(accountName, (err, userId) => {
if (err) {
return cb(err);
}
User.getUser(userId, (err, user) => {
if (err) {
return cb(err);
}
const accountStatus = user.getPropertyAsNumber(UserProps.AccountStatus);
if (
User.AccountStatus.disabled == accountStatus ||
2023-01-07 01:05:11 +00:00
User.AccountStatus.inactive == accountStatus
) {
return cb(Errors.AccessDenied('Account disabled', ErrorReasons.Disabled));
}
const activityPubSettings = ActivityPubSettings.fromUser(user);
if (!activityPubSettings.enabled) {
return cb(Errors.AccessDenied('ActivityPub is not enabled for user'));
}
return cb(null, user);
});
});
}
2023-01-05 03:29:18 +00:00
function getUserProfileTemplatedBody(
templateFile,
user,
defaultTemplate,
defaultContentType,
cb
) {
2023-01-13 19:26:12 +00:00
const Log = require('../logger').log;
const Config = require('../config').get;
2023-01-05 03:29:18 +00:00
waterfall(
[
callback => {
return fs.readFile(templateFile || '', 'utf8', (err, template) => {
return callback(null, template);
});
},
(template, callback) => {
if (!template) {
if (templateFile) {
Log.warn(`Failed to load profile template "${templateFile}"`);
}
return callback(null, defaultTemplate, defaultContentType);
}
const contentType = mimeTypes.contentType(paths.basename(templateFile));
return callback(null, template, contentType);
},
(template, contentType, callback) => {
const up = (p, na = 'N/A') => {
return user.getProperty(p) || na;
};
let birthDate = up(UserProps.Birthdate);
if (moment.isDate(birthDate)) {
birthDate = moment(birthDate);
}
const varMap = {
USERNAME: user.username,
REAL_NAME: user.getSanitizedName('real'),
SEX: up(UserProps.Sex),
BIRTHDATE: birthDate,
AGE: user.getAge(),
LOCATION: up(UserProps.Location),
AFFILIATIONS: up(UserProps.Affiliations),
EMAIL: up(UserProps.EmailAddress),
WEB_ADDRESS: up(UserProps.WebAddress),
ACCOUNT_CREATED: moment(user.getProperty(UserProps.AccountCreated)),
LAST_LOGIN: moment(user.getProperty(UserProps.LastLoginTs)),
LOGIN_COUNT: up(UserProps.LoginCount),
ACHIEVEMENT_COUNT: up(UserProps.AchievementTotalCount, '0'),
ACHIEVEMENT_POINTS: up(UserProps.AchievementTotalPoints, '0'),
BOARDNAME: Config().general.boardName,
};
let body = template;
_.each(varMap, (val, varName) => {
body = body.replace(new RegExp(`%${varName}%`, 'g'), val);
});
return callback(null, body, contentType);
},
],
(err, data, contentType) => {
return cb(err, data, contentType);
}
);
}
//
// Apply very basic HTML to a message following
// Mastodon's supported tags of 'p', 'br', 'a', and 'span':
// https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
//
function messageBodyToHtml(body) {
return `<p>${body.replace(/\r?\n/g, '<br>')}</p>`;
}
function htmlToMessageBody(html) {
return striptags(html);
}