2023-01-13 06:19:52 +00:00
|
|
|
const User = require('../user');
|
|
|
|
const { Errors, ErrorReasons } = require('../enig_error');
|
|
|
|
const UserProps = require('../user_property');
|
|
|
|
const ActivityPubSettings = require('./settings');
|
2023-01-26 05:22:45 +00:00
|
|
|
const { stripAnsiControlCodes } = require('../string_util');
|
2023-02-09 04:32:54 +00:00
|
|
|
const { WellKnownRecipientFields, WellKnownActivity } = require('./const');
|
|
|
|
const ActivityPubObject = require('./object');
|
|
|
|
const Log = require('../logger').log;
|
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-26 05:22:45 +00:00
|
|
|
const { encode, decode } = require('html-entities');
|
2023-01-29 23:52:01 +00:00
|
|
|
const { isString } = require('lodash');
|
2023-02-05 05:55:11 +00:00
|
|
|
|
|
|
|
exports.parseTimestampOrNow = parseTimestampOrNow;
|
2023-01-08 20:18:50 +00:00
|
|
|
exports.isValidLink = isValidLink;
|
2023-01-28 18:55:31 +00:00
|
|
|
exports.userFromActorId = userFromActorId;
|
2023-01-05 03:29:18 +00:00
|
|
|
exports.getUserProfileTemplatedBody = getUserProfileTemplatedBody;
|
2023-01-12 05:37:09 +00:00
|
|
|
exports.messageBodyToHtml = messageBodyToHtml;
|
2023-02-04 22:17:59 +00:00
|
|
|
exports.messageToHtml = messageToHtml;
|
2023-01-25 04:40:12 +00:00
|
|
|
exports.htmlToMessageBody = htmlToMessageBody;
|
2023-01-26 01:41:47 +00:00
|
|
|
exports.userNameFromSubject = userNameFromSubject;
|
2023-02-12 03:27:50 +00:00
|
|
|
exports.userNameToSubject = userNameToSubject;
|
2023-02-06 21:34:18 +00:00
|
|
|
exports.extractMessageMetadata = extractMessageMetadata;
|
2023-02-08 19:53:56 +00:00
|
|
|
exports.recipientIdsFromObject = recipientIdsFromObject;
|
2023-02-09 04:32:54 +00:00
|
|
|
exports.sendFollowRequest = sendFollowRequest;
|
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 = `
|
2023-01-29 23:52:01 +00:00
|
|
|
User information for: %PREFERRED_USERNAME%
|
2023-01-05 03:29:18 +00:00
|
|
|
|
2023-01-29 23:52:01 +00:00
|
|
|
Name: %NAME%
|
2023-01-05 03:29:18 +00:00
|
|
|
Login Count: %LOGIN_COUNT%
|
|
|
|
Affiliations: %AFFILIATIONS%
|
|
|
|
Achievement Points: %ACHIEVEMENT_POINTS%
|
|
|
|
`;
|
2023-01-03 22:10:39 +00:00
|
|
|
|
2023-02-05 05:55:11 +00:00
|
|
|
function parseTimestampOrNow(s) {
|
|
|
|
try {
|
|
|
|
return moment(s);
|
|
|
|
} catch (e) {
|
|
|
|
Log.warn({ error: e.message }, `Failed parsing timestamp "${s}"`);
|
|
|
|
return moment();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 20:18:50 +00:00
|
|
|
function isValidLink(l) {
|
|
|
|
return /^https?:\/\/.+$/.test(l);
|
|
|
|
}
|
|
|
|
|
2023-01-28 18:55:31 +00:00
|
|
|
function userFromActorId(actorId, cb) {
|
|
|
|
User.getUserIdsWithProperty(UserProps.ActivityPubActorId, actorId, (err, userId) => {
|
2023-01-04 03:32:09 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2023-01-28 18:55:31 +00:00
|
|
|
// must only be 0 or 1
|
|
|
|
if (!Array.isArray(userId) || userId.length !== 1) {
|
|
|
|
return cb(
|
|
|
|
Errors.DoesNotExist(
|
|
|
|
`No user with property '${UserProps.ActivityPubActorId}' of ${actorId}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
userId = userId[0];
|
2023-01-04 03:32:09 +00:00
|
|
|
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(
|
2023-01-29 23:52:01 +00:00
|
|
|
webServer,
|
2023-01-05 03:29:18 +00:00
|
|
|
templateFile,
|
|
|
|
user,
|
2023-01-29 23:52:01 +00:00
|
|
|
userAsActor,
|
2023-01-05 03:29:18 +00:00
|
|
|
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) => {
|
2023-01-29 23:52:01 +00:00
|
|
|
const val = v => {
|
|
|
|
if (isString(v)) {
|
|
|
|
return v ? encode(v) : '';
|
|
|
|
} else {
|
2023-01-30 19:30:36 +00:00
|
|
|
if (isNaN(v)) {
|
|
|
|
return '';
|
|
|
|
}
|
2023-01-29 23:52:01 +00:00
|
|
|
return v ? v : 0;
|
|
|
|
}
|
2023-01-05 03:29:18 +00:00
|
|
|
};
|
|
|
|
|
2023-01-29 23:52:01 +00:00
|
|
|
let birthDate = val(user.getProperty(UserProps.Birthdate));
|
2023-01-05 03:29:18 +00:00
|
|
|
if (moment.isDate(birthDate)) {
|
|
|
|
birthDate = moment(birthDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
const varMap = {
|
2023-01-29 23:52:01 +00:00
|
|
|
ACTOR_OBJ: JSON.stringify(userAsActor),
|
2023-02-12 03:27:50 +00:00
|
|
|
SUBJECT: userNameToSubject(user.username, webServer),
|
2023-01-29 23:52:01 +00:00
|
|
|
INBOX: userAsActor.inbox,
|
|
|
|
SHARED_INBOX: userAsActor.endpoints.sharedInbox,
|
|
|
|
OUTBOX: userAsActor.outbox,
|
|
|
|
FOLLOWERS: userAsActor.followers,
|
|
|
|
FOLLOWING: userAsActor.following,
|
|
|
|
USER_ICON: userAsActor.icon.url,
|
|
|
|
USER_IMAGE: userAsActor.image.url,
|
|
|
|
PREFERRED_USERNAME: userAsActor.preferredUsername,
|
|
|
|
NAME: userAsActor.name,
|
|
|
|
SEX: user.getProperty(UserProps.Sex),
|
2023-01-05 03:29:18 +00:00
|
|
|
BIRTHDATE: birthDate,
|
|
|
|
AGE: user.getAge(),
|
2023-01-29 23:52:01 +00:00
|
|
|
LOCATION: user.getProperty(UserProps.Location),
|
|
|
|
AFFILIATIONS: user.getProperty(UserProps.Affiliations),
|
|
|
|
EMAIL: user.getProperty(UserProps.EmailAddress),
|
|
|
|
WEB_ADDRESS: user.getProperty(UserProps.WebAddress),
|
2023-01-05 03:29:18 +00:00
|
|
|
ACCOUNT_CREATED: moment(user.getProperty(UserProps.AccountCreated)),
|
|
|
|
LAST_LOGIN: moment(user.getProperty(UserProps.LastLoginTs)),
|
2023-01-29 23:52:01 +00:00
|
|
|
LOGIN_COUNT: user.getPropertyAsNumber(UserProps.LoginCount),
|
|
|
|
ACHIEVEMENT_COUNT: user.getPropertyAsNumber(
|
|
|
|
UserProps.AchievementTotalCount
|
|
|
|
),
|
2023-01-30 19:30:36 +00:00
|
|
|
ACHIEVEMENT_POINTS: user.getPropertyAsNumber(
|
2023-01-29 23:52:01 +00:00
|
|
|
UserProps.AchievementTotalPoints
|
|
|
|
),
|
2023-01-05 03:29:18 +00:00
|
|
|
BOARDNAME: Config().general.boardName,
|
|
|
|
};
|
|
|
|
|
|
|
|
let body = template;
|
2023-01-29 23:52:01 +00:00
|
|
|
_.each(varMap, (v, varName) => {
|
|
|
|
body = body.replace(new RegExp(`%${varName}%`, 'g'), val(v));
|
2023-01-05 03:29:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return callback(null, body, contentType);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
(err, data, contentType) => {
|
|
|
|
return cb(err, data, contentType);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-01-12 05:37:09 +00:00
|
|
|
|
|
|
|
function messageBodyToHtml(body) {
|
2023-01-26 05:22:45 +00:00
|
|
|
body = encode(stripAnsiControlCodes(body), { mode: 'nonAsciiPrintable' }).replace(
|
|
|
|
/\r?\n/g,
|
|
|
|
'<br>'
|
|
|
|
);
|
|
|
|
|
|
|
|
return `<p>${body}</p>`;
|
2023-01-12 05:37:09 +00:00
|
|
|
}
|
2023-01-25 04:40:12 +00:00
|
|
|
|
2023-02-04 22:17:59 +00:00
|
|
|
//
|
|
|
|
// Apply very basic HTML to a message following
|
|
|
|
// Mastodon's supported tags of 'p', 'br', 'a', and 'span':
|
|
|
|
// - https://docs.joinmastodon.org/spec/activitypub/#sanitization
|
|
|
|
// - https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
|
|
|
|
//
|
|
|
|
// Microformats:
|
|
|
|
// - https://microformats.org/wiki/
|
|
|
|
// - https://indieweb.org/note
|
|
|
|
// - https://docs.joinmastodon.org/spec/microformats/
|
|
|
|
//
|
2023-02-05 05:55:11 +00:00
|
|
|
function messageToHtml(message) {
|
2023-02-04 22:17:59 +00:00
|
|
|
const msg = encode(stripAnsiControlCodes(message.message.trim()), {
|
|
|
|
mode: 'nonAsciiPrintable',
|
|
|
|
}).replace(/\r?\n/g, '<br>');
|
|
|
|
|
2023-02-05 05:55:11 +00:00
|
|
|
// :TODO: figure out any microformats we should use here...
|
2023-02-04 22:17:59 +00:00
|
|
|
|
|
|
|
return `<p>${msg}</p>`;
|
|
|
|
}
|
|
|
|
|
2023-01-25 04:40:12 +00:00
|
|
|
function htmlToMessageBody(html) {
|
2023-02-03 22:14:27 +00:00
|
|
|
// <br>, </br>, and <br />, <br/> -> \r\n
|
|
|
|
// </p> -> \r\n
|
|
|
|
html = html.replace(/(?:<\/?br ?\/?>)|(?:<\/p>)/g, '\r\n');
|
2023-01-26 05:22:45 +00:00
|
|
|
return striptags(decode(html));
|
2023-01-25 04:40:12 +00:00
|
|
|
}
|
2023-01-26 01:41:47 +00:00
|
|
|
|
|
|
|
function userNameFromSubject(subject) {
|
|
|
|
return subject.replace(/^acct:(.+)$/, '$1');
|
|
|
|
}
|
2023-02-06 21:34:18 +00:00
|
|
|
|
2023-02-12 03:27:50 +00:00
|
|
|
function userNameToSubject(userName, webServer) {
|
|
|
|
return `@${userName}@${webServer.getDomain()}`;
|
|
|
|
}
|
|
|
|
|
2023-02-06 21:34:18 +00:00
|
|
|
function extractMessageMetadata(body) {
|
|
|
|
const metadata = { mentions: new Set(), hashTags: new Set() };
|
|
|
|
|
|
|
|
const re = /(@\w+)|(#[A-Za-z0-9_]+)/g;
|
|
|
|
const matches = body.matchAll(re);
|
|
|
|
for (const m of matches) {
|
|
|
|
if (m[1]) {
|
|
|
|
metadata.mentions.add(m[1]);
|
|
|
|
} else if (m[2]) {
|
|
|
|
metadata.hashTags.add(m[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata;
|
|
|
|
}
|
2023-02-08 19:53:56 +00:00
|
|
|
|
|
|
|
function recipientIdsFromObject(obj) {
|
|
|
|
const ids = [];
|
|
|
|
|
|
|
|
WellKnownRecipientFields.forEach(field => {
|
|
|
|
let v = obj[field];
|
|
|
|
if (v) {
|
|
|
|
if (!Array.isArray(v)) {
|
|
|
|
v = [v];
|
|
|
|
}
|
|
|
|
ids.push(...v);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return Array.from(new Set(ids));
|
|
|
|
}
|
2023-02-09 04:32:54 +00:00
|
|
|
|
|
|
|
function sendFollowRequest(fromUser, toActor, webServer, cb) {
|
|
|
|
const fromActorId = fromUser.getProperty(UserProps.ActivityPubActorId);
|
|
|
|
if (!fromActorId) {
|
|
|
|
return cb(
|
|
|
|
Errors.MissingProperty(
|
|
|
|
`User missing "${UserProps.ActivityPubActorId}" property`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const followRequest = new ActivityPubObject({
|
|
|
|
id: ActivityPubObject.makeObjectId(webServer, 'follow'),
|
|
|
|
type: WellKnownActivity.Follow,
|
|
|
|
actor: fromActorId,
|
|
|
|
object: toActor.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
return followRequest.sendTo(toActor.inbox, fromUser, webServer, cb);
|
|
|
|
}
|