Hopefully better microformat handling outgoing

This commit is contained in:
Bryan Ashby 2023-02-04 15:17:59 -07:00
parent a24ec5fd67
commit c3335ce062
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
2 changed files with 32 additions and 10 deletions

View File

@ -3,7 +3,7 @@ const ActivityPubObject = require('./object');
const { Errors } = require('../enig_error');
const { getISOTimestampString } = require('../database');
const User = require('../user');
const { messageBodyToHtml, htmlToMessageBody } = require('./util');
const { messageToHtml, htmlToMessageBody } = require('./util');
const { isAnsi } = require('../string_util');
const Log = require('../logger').log;
@ -117,7 +117,7 @@ module.exports = class Note extends ActivityPubObject {
published: getISOTimestampString(message.modTimestamp),
to,
attributedTo: fromActor.id,
content: messageBodyToHtml(message.message.trim()),
content: messageToHtml(message, remoteActor),
source: {
content: message.message,
mediaType: sourceMediaType,
@ -237,7 +237,6 @@ module.exports = class Note extends ActivityPubObject {
message.modTimestamp = moment();
}
// :TODO: replyToMsgId from 'inReplyTo'
message.setRemoteFromUser(this.attributedTo);
message.setExternalFlavor(Message.AddressFlavor.ActivityPub);

View File

@ -25,6 +25,7 @@ exports.localActorId = localActorId;
exports.userFromActorId = userFromActorId;
exports.getUserProfileTemplatedBody = getUserProfileTemplatedBody;
exports.messageBodyToHtml = messageBodyToHtml;
exports.messageToHtml = messageToHtml;
exports.htmlToMessageBody = htmlToMessageBody;
exports.userNameFromSubject = userNameFromSubject;
@ -194,13 +195,6 @@ function getUserProfileTemplatedBody(
);
}
//
// 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/
//
// :TODO: https://docs.joinmastodon.org/spec/microformats/
function messageBodyToHtml(body) {
body = encode(stripAnsiControlCodes(body), { mode: 'nonAsciiPrintable' }).replace(
/\r?\n/g,
@ -210,6 +204,35 @@ function messageBodyToHtml(body) {
return `<p>${body}</p>`;
}
//
// 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/
//
function messageToHtml(message, remoteActor) {
const msg = encode(stripAnsiControlCodes(message.message.trim()), {
mode: 'nonAsciiPrintable',
}).replace(/\r?\n/g, '<br>');
if (message.isPrivate()) {
const toId = remoteActor.id;
return `<p>
<span class="h-card">
<a href="${toId}" class="u-url mention">@ <span>${remoteActor.preferredUsername}</span></a>
</span>
${msg}
</p>`;
}
return `<p>${msg}</p>`;
}
function htmlToMessageBody(html) {
// <br>, </br>, and <br />, <br/> -> \r\n
// </p> -> \r\n