[backend/masto-client] Render quote uris inline (ISH-177)

This commit is contained in:
Laura Hausmann 2024-03-13 03:40:31 +01:00
parent abf93138e1
commit e65c76ca39
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 33 additions and 9 deletions

View file

@ -28,12 +28,16 @@ public class NoteRenderer(
var quote = note is { Renote: not null, IsQuote: true } && recurse > 0 var quote = note is { Renote: not null, IsQuote: true } && recurse > 0
? await RenderAsync(note.Renote, user, data, --recurse) ? await RenderAsync(note.Renote, user, data, --recurse)
: null; : null;
var text = note.Text; var text = note.Text;
string? quoteUri = null;
if (note is { Renote: not null, IsQuote: true } && text != null) if (note is { Renote: not null, IsQuote: true } && text != null)
{ {
var quoteUri = note.Renote?.Url ?? note.Renote?.Uri ?? note.Renote?.GetPublicUriOrNull(config.Value); var qUri = note.Renote?.Url ?? note.Renote?.Uri ?? note.Renote?.GetPublicUriOrNull(config.Value);
if (quoteUri != null) var alt = note.Renote?.Uri;
text += $"\n\nRE: {quoteUri}"; //TODO: render as inline quote
if (qUri != null && !text.Contains(qUri) && (alt == null || qUri == alt || !text.Contains(alt)))
quoteUri = qUri;
} }
var liked = data?.LikedNotes?.Contains(note.Id) ?? var liked = data?.LikedNotes?.Contains(note.Id) ??
@ -69,7 +73,7 @@ public class NoteRenderer(
.ToList(); .ToList();
var content = text != null && data?.Source != true var content = text != null && data?.Source != true
? await mfmConverter.ToHtmlAsync(text, mentionedUsers, note.UserHost) ? await mfmConverter.ToHtmlAsync(text, mentionedUsers, note.UserHost, quoteUri)
: null; : null;
var account = data?.Accounts?.FirstOrDefault(p => p.Id == note.UserId) ?? var account = data?.Accounts?.FirstOrDefault(p => p.Id == note.UserId) ??

View file

@ -54,7 +54,9 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
return parser.Mentions; return parser.Mentions;
} }
public async Task<string> ToHtmlAsync(IEnumerable<MfmNode> nodes, List<Note.MentionedUser> mentions, string? host) public async Task<string> ToHtmlAsync(
IEnumerable<MfmNode> nodes, List<Note.MentionedUser> mentions, string? host, string? quoteUri = null
)
{ {
var context = BrowsingContext.New(); var context = BrowsingContext.New();
var document = await context.OpenNewAsync(); var document = await context.OpenNewAsync();
@ -62,15 +64,33 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
foreach (var node in nodes) element.AppendNodes(FromMfmNode(document, node, mentions, host)); foreach (var node in nodes) element.AppendNodes(FromMfmNode(document, node, mentions, host));
if (quoteUri != null)
{
var a = document.CreateElement("a");
a.SetAttribute("href", quoteUri);
a.TextContent = quoteUri.StartsWith("https://") ? quoteUri[8..] : quoteUri[7..];
var quote = document.CreateElement("span");
quote.ClassList.Add("quote-inline");
quote.AppendChild(document.CreateElement("br"));
quote.AppendChild(document.CreateElement("br"));
var re = document.CreateElement("span");
re.TextContent = "RE: ";
quote.AppendChild(re);
quote.AppendChild(a);
element.AppendChild(quote);
}
await using var sw = new StringWriter(); await using var sw = new StringWriter();
await element.ToHtmlAsync(sw); await element.ToHtmlAsync(sw);
return sw.ToString(); return sw.ToString();
} }
public async Task<string> ToHtmlAsync(string mfm, List<Note.MentionedUser> mentions, string? host) public async Task<string> ToHtmlAsync(
string mfm, List<Note.MentionedUser> mentions, string? host, string? quoteUri = null
)
{ {
var nodes = MfmParser.Parse(mfm); var nodes = MfmParser.Parse(mfm);
return await ToHtmlAsync(nodes, mentions, host); return await ToHtmlAsync(nodes, mentions, host, quoteUri);
} }
private INode FromMfmNode(IDocument document, MfmNode node, List<Note.MentionedUser> mentions, string? host) private INode FromMfmNode(IDocument document, MfmNode node, List<Note.MentionedUser> mentions, string? host)
@ -265,7 +285,7 @@ public class MfmConverter(IOptions<Config.InstanceSection> config)
var markupNode = new MfmTextNode { Text = chars }; var markupNode = new MfmTextNode { Text = chars };
node.Children = node.Children.Prepend(markupNode).Append(markupNode); node.Children = node.Children.Prepend(markupNode).Append(markupNode);
} }
private void AddHtmlMarkupStartOnly(MfmNode node, string chars) private void AddHtmlMarkupStartOnly(MfmNode node, string chars)
{ {
if (SupportsHtmlFormatting) return; if (SupportsHtmlFormatting) return;