[frontend/mfm] Hide host part of mentions to local users

This commit is contained in:
pancakes 2024-11-22 00:06:03 +10:00 committed by Laura Hausmann
parent b581714387
commit 63aadfdf07
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 19 additions and 12 deletions

View file

@ -1,5 +1,7 @@
@using Iceshrimp.Frontend.Core.Miscellaneous @using Iceshrimp.Frontend.Core.Miscellaneous
@using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Shared.Schemas.Web @using Iceshrimp.Shared.Schemas.Web
@inject MetadataService MetadataService
@TextBody @TextBody
@code { @code {
@ -12,7 +14,8 @@
{ {
if (Text != null) 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) 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);
} }
} }
} }

View file

@ -9,19 +9,19 @@ namespace Iceshrimp.Frontend.Core.Miscellaneous;
public static class MfmRenderer public static class MfmRenderer
{ {
public static async Task<MarkupString> RenderStringAsync( public static async Task<MarkupString> RenderStringAsync(
string text, List<EmojiResponse> emoji, bool simple = false string text, List<EmojiResponse> emoji, string accountDomain, bool simple = false
) )
{ {
var res = simple ? Mfm.parseSimple(text) : Mfm.parse(text); var res = simple ? Mfm.parseSimple(text) : Mfm.parse(text);
var context = BrowsingContext.New(); var context = BrowsingContext.New();
var document = await context.OpenNewAsync(); var document = await context.OpenNewAsync();
var renderedMfm = RenderMultipleNodes(res, document, emoji, simple); var renderedMfm = RenderMultipleNodes(res, document, emoji, accountDomain, simple);
var html = renderedMfm.ToHtml(); var html = renderedMfm.ToHtml();
return new MarkupString(html); return new MarkupString(html);
} }
private static INode RenderMultipleNodes( private static INode RenderMultipleNodes(
IEnumerable<MfmNodeTypes.MfmNode> nodes, IDocument document, List<EmojiResponse> emoji, bool simple IEnumerable<MfmNodeTypes.MfmNode> nodes, IDocument document, List<EmojiResponse> emoji, string accountDomain, bool simple
) )
{ {
var el = document.CreateElement("span"); var el = document.CreateElement("span");
@ -31,7 +31,7 @@ public static class MfmRenderer
{ {
try try
{ {
el.AppendNodes(RenderNode(node, document, emoji, simple)); el.AppendNodes(RenderNode(node, document, emoji, accountDomain, simple));
} }
catch (NotImplementedException e) catch (NotImplementedException e)
{ {
@ -45,7 +45,7 @@ public static class MfmRenderer
} }
private static INode RenderNode( private static INode RenderNode(
MfmNodeTypes.MfmNode node, IDocument document, List<EmojiResponse> emoji, bool simple MfmNodeTypes.MfmNode node, IDocument document, List<EmojiResponse> emoji, string accountDomain, bool simple
) )
{ {
// Hard wrap makes this impossible to read // Hard wrap makes this impossible to read
@ -66,7 +66,7 @@ public static class MfmRenderer
MfmNodeTypes.MfmItalicNode mfmItalicNode => MfmItalicNode(mfmItalicNode, document), MfmNodeTypes.MfmItalicNode mfmItalicNode => MfmItalicNode(mfmItalicNode, document),
MfmNodeTypes.MfmLinkNode mfmLinkNode => MfmLinkNode(mfmLinkNode, document), MfmNodeTypes.MfmLinkNode mfmLinkNode => MfmLinkNode(mfmLinkNode, document),
MfmNodeTypes.MfmMathInlineNode mfmMathInlineNode => throw new NotImplementedException($"{mfmMathInlineNode.GetType()}"), 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.MfmPlainNode mfmPlainNode => MfmPlainNode(mfmPlainNode, document),
MfmNodeTypes.MfmSmallNode mfmSmallNode => MfmSmallNode(mfmSmallNode, document), MfmNodeTypes.MfmSmallNode mfmSmallNode => MfmSmallNode(mfmSmallNode, document),
MfmNodeTypes.MfmStrikeNode mfmStrikeNode => MfmStrikeNode(mfmStrikeNode, document), MfmNodeTypes.MfmStrikeNode mfmStrikeNode => MfmStrikeNode(mfmStrikeNode, document),
@ -83,7 +83,7 @@ public static class MfmRenderer
{ {
try try
{ {
rendered.AppendNodes(RenderNode(childNode, document, emoji, simple)); rendered.AppendNodes(RenderNode(childNode, document, emoji, accountDomain, simple));
} }
catch (NotImplementedException e) catch (NotImplementedException e)
{ {
@ -220,16 +220,19 @@ public static class MfmRenderer
return el; 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"); 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"; link.ClassName = "mention";
var userPart = document.CreateElement("span"); var userPart = document.CreateElement("span");
userPart.ClassName = "user"; userPart.ClassName = "user";
userPart.TextContent = $"@{node.Username}"; userPart.TextContent = $"@{node.Username}";
link.AppendChild(userPart); link.AppendChild(userPart);
if (node.Host != null) if (node.Host != null && node.Host.Value != accountDomain)
{ {
var hostPart = document.CreateElement("span"); var hostPart = document.CreateElement("span");
hostPart.ClassName = "host"; hostPart.ClassName = "host";