[backend/libmfm] Fix HTML markup being dropped for federation & public preview requests

This commit is contained in:
Laura Hausmann 2024-12-03 23:24:29 +01:00
parent 3dabdc09e8
commit 7b61865287
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 14 additions and 9 deletions

View file

@ -1,24 +1,24 @@
using Iceshrimp.Backend.Core.Configuration;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Extensions; using Iceshrimp.Backend.Core.Extensions;
using Iceshrimp.Backend.Core.Helpers.LibMfm.Conversion; using Iceshrimp.Backend.Core.Helpers.LibMfm.Conversion;
using Iceshrimp.Parsing; using Iceshrimp.Parsing;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Options;
namespace Iceshrimp.Backend.Components.PublicPreview.Renderers; namespace Iceshrimp.Backend.Components.PublicPreview.Renderers;
public class MfmRenderer(IOptions<Config.InstanceSection> config) : ISingletonService public class MfmRenderer(MfmConverter converter) : ISingletonService
{ {
private readonly MfmConverter _converter = new(config);
public async Task<MarkupString?> RenderAsync( public async Task<MarkupString?> RenderAsync(
string? text, string? host, List<Note.MentionedUser> mentions, List<Emoji> emoji, string rootElement string? text, string? host, List<Note.MentionedUser> mentions, List<Emoji> emoji, string rootElement
) )
{ {
if (text is null) return null; if (text is null) return null;
var parsed = Mfm.parse(text); var parsed = Mfm.parse(text);
var serialized = await _converter.ToHtmlAsync(parsed, mentions, host, emoji: emoji, rootElement: rootElement);
// Ensure we are rendering HTML markup (AsyncLocal)
converter.SupportsHtmlFormatting.Value = true;
var serialized = await converter.ToHtmlAsync(parsed, mentions, host, emoji: emoji, rootElement: rootElement);
return new MarkupString(serialized); return new MarkupString(serialized);
} }
} }

View file

@ -5,6 +5,7 @@ using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Extensions; using Iceshrimp.Backend.Core.Extensions;
using Iceshrimp.Backend.Core.Federation.Cryptography; using Iceshrimp.Backend.Core.Federation.Cryptography;
using Iceshrimp.Backend.Core.Helpers.LibMfm.Conversion;
using Iceshrimp.Backend.Core.Services; using Iceshrimp.Backend.Core.Services;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@ -21,13 +22,17 @@ public class AuthorizedFetchMiddleware(
SystemUserService systemUserSvc, SystemUserService systemUserSvc,
ActivityPub.FederationControlService fedCtrlSvc, ActivityPub.FederationControlService fedCtrlSvc,
ILogger<AuthorizedFetchMiddleware> logger, ILogger<AuthorizedFetchMiddleware> logger,
IHostApplicationLifetime appLifetime IHostApplicationLifetime appLifetime,
MfmConverter mfmConverter
) : ConditionalMiddleware<AuthorizedFetchAttribute>, IMiddlewareService ) : ConditionalMiddleware<AuthorizedFetchAttribute>, IMiddlewareService
{ {
public static ServiceLifetime Lifetime => ServiceLifetime.Scoped; public static ServiceLifetime Lifetime => ServiceLifetime.Scoped;
public async Task InvokeAsync(HttpContext ctx, RequestDelegate next) public async Task InvokeAsync(HttpContext ctx, RequestDelegate next)
{ {
// Ensure we're rendering HTML markup (AsyncLocal)
mfmConverter.SupportsHtmlFormatting.Value = true;
if (!config.Value.AuthorizedFetch) if (!config.Value.AuthorizedFetch)
{ {
await next(ctx); await next(ctx);