[backend/masto-client] Respect SupportsHtmlFormatting setting (ISH-88)

This commit is contained in:
Laura Hausmann 2024-03-01 22:33:16 +01:00
parent b979605470
commit f07217b390
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 40 additions and 12 deletions

View file

@ -57,12 +57,12 @@ public static class ServiceExtensions
.AddScoped<NotificationRenderer>() .AddScoped<NotificationRenderer>()
.AddScoped<ActivityPubController>() .AddScoped<ActivityPubController>()
.AddScoped<FollowupTaskService>() .AddScoped<FollowupTaskService>()
.AddScoped<InstanceService>(); .AddScoped<InstanceService>()
.AddScoped<MfmConverter>();
// Singleton = instantiated once across application lifetime // Singleton = instantiated once across application lifetime
services services
.AddSingleton<HttpClient, CustomHttpClient>() .AddSingleton<HttpClient, CustomHttpClient>()
.AddSingleton<MfmConverter>()
.AddSingleton<HttpRequestService>() .AddSingleton<HttpRequestService>()
.AddSingleton<CronService>() .AddSingleton<CronService>()
.AddSingleton<QueueService>() .AddSingleton<QueueService>()

View file

@ -17,6 +17,8 @@ namespace Iceshrimp.Backend.Core.Helpers.LibMfm.Conversion;
public class MfmConverter(IOptions<Config.InstanceSection> config) public class MfmConverter(IOptions<Config.InstanceSection> config)
{ {
public bool SupportsHtmlFormatting { private get; set; } = true;
public static async Task<string?> FromHtmlAsync(string? html, List<Note.MentionedUser>? mentions = null) public static async Task<string?> FromHtmlAsync(string? html, List<Note.MentionedUser>? mentions = null)
{ {
if (html == null) return null; if (html == null) return null;
@ -77,7 +79,8 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
{ {
case MfmBoldNode: case MfmBoldNode:
{ {
var el = document.CreateElement("b"); var el = CreateInlineFormattingElement(document, "b");
AddHtmlMarkup(node, "**");
AppendChildren(el, document, node, mentions, host); AppendChildren(el, document, node, mentions, host);
return el; return el;
} }
@ -89,21 +92,23 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
} }
case MfmStrikeNode: case MfmStrikeNode:
{ {
var el = document.CreateElement("del"); var el = CreateInlineFormattingElement(document, "del");
AddHtmlMarkup(node, "~~");
AppendChildren(el, document, node, mentions, host); AppendChildren(el, document, node, mentions, host);
return el; return el;
} }
case MfmItalicNode: case MfmItalicNode:
case MfmFnNode: case MfmFnNode:
{ {
var el = document.CreateElement("i"); var el = CreateInlineFormattingElement(document, "i");
AddHtmlMarkup(node, "*");
AppendChildren(el, document, node, mentions, host); AppendChildren(el, document, node, mentions, host);
return el; return el;
} }
case MfmCodeBlockNode codeBlockNode: case MfmCodeBlockNode codeBlockNode:
{ {
var el = document.CreateElement("pre"); var el = CreateInlineFormattingElement(document, "pre");
var inner = document.CreateElement("code"); var inner = CreateInlineFormattingElement(document, "code");
inner.TextContent = codeBlockNode.Code; inner.TextContent = codeBlockNode.Code;
el.AppendNodes(inner); el.AppendNodes(inner);
return el; return el;
@ -132,19 +137,19 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
} }
case MfmInlineCodeNode inlineCodeNode: case MfmInlineCodeNode inlineCodeNode:
{ {
var el = document.CreateElement("code"); var el = CreateInlineFormattingElement(document, "code");
el.TextContent = inlineCodeNode.Code; el.TextContent = inlineCodeNode.Code;
return el; return el;
} }
case MfmMathInlineNode mathInlineNode: case MfmMathInlineNode mathInlineNode:
{ {
var el = document.CreateElement("code"); var el = CreateInlineFormattingElement(document, "code");
el.TextContent = mathInlineNode.Formula; el.TextContent = mathInlineNode.Formula;
return el; return el;
} }
case MfmMathBlockNode mathBlockNode: case MfmMathBlockNode mathBlockNode:
{ {
var el = document.CreateElement("code"); var el = CreateInlineFormattingElement(document, "code");
el.TextContent = mathBlockNode.Formula; el.TextContent = mathBlockNode.Formula;
return el; return el;
} }
@ -188,7 +193,8 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
} }
case MfmQuoteNode: case MfmQuoteNode:
{ {
var el = document.CreateElement("blockquote"); var el = CreateInlineFormattingElement(document, "blockquote");
AddHtmlMarkupStartOnly(node, "> ");
AppendChildren(el, document, node, mentions, host); AppendChildren(el, document, node, mentions, host);
return el; return el;
} }
@ -247,4 +253,23 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
{ {
foreach (var node in parent.Children) element.AppendNodes(FromMfmNode(document, node, mentions, host)); foreach (var node in parent.Children) element.AppendNodes(FromMfmNode(document, node, mentions, host));
} }
private IElement CreateInlineFormattingElement(IDocument document, string name)
{
return document.CreateElement(SupportsHtmlFormatting ? name : "span");
}
private void AddHtmlMarkup(MfmNode node, string chars)
{
if (SupportsHtmlFormatting) return;
var markupNode = new MfmTextNode { Text = chars };
node.Children = node.Children.Prepend(markupNode).Append(markupNode);
}
private void AddHtmlMarkupStartOnly(MfmNode node, string chars)
{
if (SupportsHtmlFormatting) return;
var markupNode = new MfmTextNode { Text = chars };
node.Children = node.Children.Prepend(markupNode);
}
} }

View file

@ -2,12 +2,13 @@ using Iceshrimp.Backend.Controllers.Mastodon.Attributes;
using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Helpers; using Iceshrimp.Backend.Core.Helpers;
using Iceshrimp.Backend.Core.Helpers.LibMfm.Conversion;
using Iceshrimp.Backend.Core.Services; using Iceshrimp.Backend.Core.Services;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace Iceshrimp.Backend.Core.Middleware; namespace Iceshrimp.Backend.Core.Middleware;
public class AuthenticationMiddleware(DatabaseContext db, UserService userSvc) : IMiddleware public class AuthenticationMiddleware(DatabaseContext db, UserService userSvc, MfmConverter mfmConverter) : IMiddleware
{ {
public async Task InvokeAsync(HttpContext ctx, RequestDelegate next) public async Task InvokeAsync(HttpContext ctx, RequestDelegate next)
{ {
@ -58,6 +59,8 @@ public class AuthenticationMiddleware(DatabaseContext db, UserService userSvc) :
userSvc.UpdateOauthTokenMetadata(oauthToken); userSvc.UpdateOauthTokenMetadata(oauthToken);
ctx.SetOauthToken(oauthToken); ctx.SetOauthToken(oauthToken);
mfmConverter.SupportsHtmlFormatting = oauthToken.SupportsHtmlFormatting;
} }
else else
{ {