From 63aadfdf07f5369986668a79dfc7932ec973c830 Mon Sep 17 00:00:00 2001 From: pancakes Date: Fri, 22 Nov 2024 00:06:03 +1000 Subject: [PATCH] [frontend/mfm] Hide host part of mentions to local users --- Iceshrimp.Frontend/Components/MfmText.razor | 8 +++++-- .../Core/Miscellaneous/RenderMfm.cs | 23 +++++++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Iceshrimp.Frontend/Components/MfmText.razor b/Iceshrimp.Frontend/Components/MfmText.razor index 7787725a..d00302ba 100644 --- a/Iceshrimp.Frontend/Components/MfmText.razor +++ b/Iceshrimp.Frontend/Components/MfmText.razor @@ -1,5 +1,7 @@ @using Iceshrimp.Frontend.Core.Miscellaneous +@using Iceshrimp.Frontend.Core.Services @using Iceshrimp.Shared.Schemas.Web +@inject MetadataService MetadataService @TextBody @code { @@ -12,7 +14,8 @@ { if (Text != null) { - TextBody = await MfmRenderer.RenderStringAsync(Text, Emoji, Simple); + var instance = await MetadataService.Instance.Value; + TextBody = await MfmRenderer.RenderStringAsync(Text, Emoji, instance.AccountDomain, Simple); } } @@ -20,7 +23,8 @@ { if (Text != null) { - TextBody = await MfmRenderer.RenderStringAsync(Text, Emoji, Simple); + var instance = await MetadataService.Instance.Value; + TextBody = await MfmRenderer.RenderStringAsync(Text, Emoji, instance.AccountDomain, Simple); } } } \ No newline at end of file diff --git a/Iceshrimp.Frontend/Core/Miscellaneous/RenderMfm.cs b/Iceshrimp.Frontend/Core/Miscellaneous/RenderMfm.cs index ebea355f..42ee16d3 100644 --- a/Iceshrimp.Frontend/Core/Miscellaneous/RenderMfm.cs +++ b/Iceshrimp.Frontend/Core/Miscellaneous/RenderMfm.cs @@ -9,19 +9,19 @@ namespace Iceshrimp.Frontend.Core.Miscellaneous; public static class MfmRenderer { public static async Task RenderStringAsync( - string text, List emoji, bool simple = false + string text, List emoji, string accountDomain, bool simple = false ) { var res = simple ? Mfm.parseSimple(text) : Mfm.parse(text); var context = BrowsingContext.New(); var document = await context.OpenNewAsync(); - var renderedMfm = RenderMultipleNodes(res, document, emoji, simple); + var renderedMfm = RenderMultipleNodes(res, document, emoji, accountDomain, simple); var html = renderedMfm.ToHtml(); return new MarkupString(html); } private static INode RenderMultipleNodes( - IEnumerable nodes, IDocument document, List emoji, bool simple + IEnumerable nodes, IDocument document, List emoji, string accountDomain, bool simple ) { var el = document.CreateElement("span"); @@ -31,7 +31,7 @@ public static class MfmRenderer { try { - el.AppendNodes(RenderNode(node, document, emoji, simple)); + el.AppendNodes(RenderNode(node, document, emoji, accountDomain, simple)); } catch (NotImplementedException e) { @@ -45,7 +45,7 @@ public static class MfmRenderer } private static INode RenderNode( - MfmNodeTypes.MfmNode node, IDocument document, List emoji, bool simple + MfmNodeTypes.MfmNode node, IDocument document, List emoji, string accountDomain, bool simple ) { // Hard wrap makes this impossible to read @@ -66,7 +66,7 @@ public static class MfmRenderer MfmNodeTypes.MfmItalicNode mfmItalicNode => MfmItalicNode(mfmItalicNode, document), MfmNodeTypes.MfmLinkNode mfmLinkNode => MfmLinkNode(mfmLinkNode, document), MfmNodeTypes.MfmMathInlineNode mfmMathInlineNode => throw new NotImplementedException($"{mfmMathInlineNode.GetType()}"), - MfmNodeTypes.MfmMentionNode mfmMentionNode => MfmMentionNode(mfmMentionNode, document), + MfmNodeTypes.MfmMentionNode mfmMentionNode => MfmMentionNode(mfmMentionNode, document, accountDomain), MfmNodeTypes.MfmPlainNode mfmPlainNode => MfmPlainNode(mfmPlainNode, document), MfmNodeTypes.MfmSmallNode mfmSmallNode => MfmSmallNode(mfmSmallNode, document), MfmNodeTypes.MfmStrikeNode mfmStrikeNode => MfmStrikeNode(mfmStrikeNode, document), @@ -83,7 +83,7 @@ public static class MfmRenderer { try { - rendered.AppendNodes(RenderNode(childNode, document, emoji, simple)); + rendered.AppendNodes(RenderNode(childNode, document, emoji, accountDomain, simple)); } catch (NotImplementedException e) { @@ -220,16 +220,19 @@ public static class MfmRenderer return el; } - private static INode MfmMentionNode(MfmNodeTypes.MfmMentionNode node, IDocument document) + private static INode MfmMentionNode(MfmNodeTypes.MfmMentionNode node, IDocument document, string accountDomain) { var link = document.CreateElement("a"); - link.SetAttribute("href", $"/@{node.Acct}"); + link.SetAttribute("href", + node.Host != null && node.Host.Value != accountDomain + ? $"/@{node.Acct}" + : $"/@{node.Username}"); link.ClassName = "mention"; var userPart = document.CreateElement("span"); userPart.ClassName = "user"; userPart.TextContent = $"@{node.Username}"; link.AppendChild(userPart); - if (node.Host != null) + if (node.Host != null && node.Host.Value != accountDomain) { var hostPart = document.CreateElement("span"); hostPart.ClassName = "host";