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-03 22:10:39 +00:00
|
|
|
|
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');
|
2023-01-25 04:40:12 +00:00
|
|
|
const { striptags } = require('striptags');
|
2023-01-05 03:29:18 +00:00
|
|
|
|
2023-01-09 00:11:49 +00:00
|
|
|
exports.ActivityStreamsContext = 'https://www.w3.org/ns/activitystreams';
|
2023-01-08 20:18:50 +00:00
|
|
|
exports.isValidLink = isValidLink;
|
2023-01-22 20:51:32 +00:00
|
|
|
exports.makeSharedInboxUrl = makeSharedInboxUrl;
|
2023-01-04 03:32:09 +00:00
|
|
|
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;
|
2023-01-12 05:37:09 +00:00
|
|
|
exports.messageBodyToHtml = messageBodyToHtml;
|
2023-01-25 04:40:12 +00:00
|
|
|
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%
|
|
|
|
`;
|
2023-01-03 22:10:39 +00:00
|
|
|
|
2023-01-08 20:18:50 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2023-01-04 03:32:09 +00:00
|
|
|
function makeUserUrl(webServer, user, relPrefix) {
|
2023-01-03 22:10:39 +00:00
|
|
|
return webServer.buildUrl(
|
|
|
|
WellKnownLocations.Internal + `${relPrefix}${user.username}`
|
|
|
|
);
|
|
|
|
}
|
2023-01-04 03:32:09 +00:00
|
|
|
|
|
|
|
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
|
2023-01-09 00:11:49 +00:00
|
|
|
// :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);
|
|
|
|
}
|
|
|
|
|
2023-01-04 03:32:09 +00:00
|
|
|
function userFromAccount(accountName, cb) {
|
2023-01-06 22:09:08 +00:00
|
|
|
if (accountName.startsWith('@')) {
|
|
|
|
accountName = accountName.slice(1);
|
|
|
|
}
|
|
|
|
|
2023-01-04 03:32:09 +00:00
|
|
|
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
|
2023-01-04 03:32:09 +00:00
|
|
|
) {
|
|
|
|
return cb(Errors.AccessDenied('Account disabled', ErrorReasons.Disabled));
|
|
|
|
}
|
|
|
|
|
2023-01-09 00:11:49 +00:00
|
|
|
const activityPubSettings = ActivityPubSettings.fromUser(user);
|
|
|
|
if (!activityPubSettings.enabled) {
|
|
|
|
return cb(Errors.AccessDenied('ActivityPub is not enabled for user'));
|
|
|
|
}
|
|
|
|
|
2023-01-04 03:32:09 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-01-12 05:37:09 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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>`;
|
|
|
|
}
|
2023-01-25 04:40:12 +00:00
|
|
|
|
|
|
|
function htmlToMessageBody(html) {
|
|
|
|
return striptags(html);
|
|
|
|
}
|