[backend] address review

This commit is contained in:
Kopper 2024-12-13 11:48:37 +03:00 committed by Laura Hausmann
parent 1e1364e2bb
commit 149fae3363
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
7 changed files with 55 additions and 56 deletions

View file

@ -81,7 +81,7 @@ public class NoteRenderer(
? await GetAttachmentsAsync([note])
: [..data.Attachments.Where(p => note.FileIds.Contains(p.Id))];
if (user == null && security.Value.PublicPreview == Enums.PublicPreview.RestrictedNoMedia) //TODO
if (user == null && security.Value.PublicPreview == Enums.PublicPreview.RestrictedNoMedia)
attachments = [];
var reactions = data?.Reactions == null
@ -104,8 +104,6 @@ public class NoteRenderer(
var sensitive = note.Cw != null || attachments.Any(p => p.Sensitive);
// TODO: canDisplayInlineMedia oauth_token flag that marks all media as "other" so they end up as links
// (and doesn't remove the attachments)
var inlineMedia = attachments.Select(p => new MfmInlineMedia(p.Type switch
{
AttachmentType.Audio => MfmInlineMedia.MediaType.Audio,
@ -116,6 +114,7 @@ public class NoteRenderer(
string? content = null;
if (data?.Source != true)
{
if (text != null || quoteUri != null || quoteInaccessible || replyInaccessible)
{
(content, inlineMedia) = await mfmConverter.ToHtmlAsync(text ?? "", mentionedUsers, note.UserHost, quoteUri,
@ -127,6 +126,7 @@ public class NoteRenderer(
{
content = "";
}
}
var account = data?.Accounts?.FirstOrDefault(p => p.Id == note.UserId) ??
await userRenderer.RenderAsync(note.User, user);
@ -216,8 +216,6 @@ public class NoteRenderer(
{
var files = attachments.Where(p => edit.FileIds.Contains(p.Id)).ToList();
// TODO: canDisplayInlineMedia oauth_token flag that marks all media as "other" so they end up as links
// (and doesn't remove the attachments)
var inlineMedia = files.Select(p => new MfmInlineMedia(p.Type switch
{
AttachmentType.Audio => MfmInlineMedia.MediaType.Audio,

View file

@ -13,13 +13,13 @@ namespace Iceshrimp.Backend.Core.Database.Migrations
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("""CREATE INDEX IX_drive_file_accessUrl ON drive_file (COALESCE("webpublicUrl", url));""");
migrationBuilder.Sql("""CREATE INDEX "IX_drive_file_accessUrl" ON "drive_file" (COALESCE("webpublicUrl", url));""");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("""DROP INDEX IX_drive_file_accessUrl;""");
migrationBuilder.Sql("""DROP INDEX "IX_drive_file_accessUrl";""");
}
}
}

View file

@ -21,7 +21,7 @@ namespace Iceshrimp.Backend.Core.Database.Migrations
defaultValue: false);
// automatically enable inline media for pleroma clients, as pleroma itself does support it
migrationBuilder.Sql("""UPDATE oauth_token SET "supportsInlineMedia"=TRUE WHERE "isPleroma"=TRUE;""");
migrationBuilder.Sql("""UPDATE "oauth_token" SET "supportsInlineMedia"=TRUE WHERE "isPleroma"=TRUE;""");
}
/// <inheritdoc />

View file

@ -14,7 +14,7 @@ using HtmlParser = AngleSharp.Html.Parser.HtmlParser;
namespace Iceshrimp.Backend.Core.Helpers.LibMfm.Conversion;
public record MfmInlineMedia(MfmInlineMedia.MediaType Type, string Src, string? Alt)
public readonly record struct MfmInlineMedia(MfmInlineMedia.MediaType Type, string Src, string? Alt)
{
public enum MediaType
{
@ -34,6 +34,12 @@ public record MfmInlineMedia(MfmInlineMedia.MediaType Type, string Src, string?
}
}
/// <summary>Resulting data after HTML to MFM conversion</summary>
public readonly record struct HtmlMfmData(string Mfm, List<MfmInlineMedia> InlineMedia);
/// <summary>Resulting data after MFM to HTML conversion</summary>
public readonly record struct MfmHtmlData(string Html, List<MfmInlineMedia> InlineMedia);
public class MfmConverter(
IOptions<Config.InstanceSection> config
) : ISingletonService
@ -41,10 +47,10 @@ public class MfmConverter(
public AsyncLocal<bool> SupportsHtmlFormatting { get; } = new();
public AsyncLocal<bool> SupportsInlineMedia { get; } = new();
public static async Task<(string Mfm, List<MfmInlineMedia> InlineMedia)> FromHtmlAsync(string? html, List<Note.MentionedUser>? mentions = null)
public static async Task<HtmlMfmData> FromHtmlAsync(string? html, List<Note.MentionedUser>? mentions = null)
{
var media = new List<MfmInlineMedia>();
if (html == null) return ("", media);
if (html == null) return new HtmlMfmData("", media);
// Ensure compatibility with AP servers that send both <br> as well as newlines
var regex = new Regex(@"<br\s?\/?>\r?\n", RegexOptions.IgnoreCase);
@ -54,12 +60,12 @@ public class MfmConverter(
html = html.Replace("\u00A0", " ");
var dom = await new HtmlParser().ParseDocumentAsync(html);
if (dom.Body == null) return ("", media);
if (dom.Body == null) return new HtmlMfmData("", media);
var sb = new StringBuilder();
var parser = new MfmHtmlParser(mentions ?? [], media);
dom.Body.ChildNodes.Select(parser.ParseNode).ToList().ForEach(s => sb.Append(s));
return (sb.ToString().Trim(), media);
return new HtmlMfmData(sb.ToString().Trim(), media);
}
public static async Task<List<string>> ExtractMentionsFromHtmlAsync(string? html)
@ -80,7 +86,7 @@ public class MfmConverter(
return parser.Mentions;
}
public async Task<(string Html, List<MfmInlineMedia> InlineMedia)> ToHtmlAsync(
public async Task<MfmHtmlData> ToHtmlAsync(
IEnumerable<IMfmNode> nodes, List<Note.MentionedUser> mentions, string? host, string? quoteUri = null,
bool quoteInaccessible = false, bool replyInaccessible = false, string rootElement = "p",
List<Emoji>? emoji = null, List<MfmInlineMedia>? media = null
@ -109,7 +115,7 @@ public class MfmConverter(
}
var usedMedia = new List<MfmInlineMedia>();
foreach (var node in nodeList) element.AppendNodes(FromMfmNode(document, node, mentions, host, ref usedMedia, emoji, media));
foreach (var node in nodeList) element.AppendNodes(FromMfmNode(document, node, mentions, host, usedMedia, emoji, media));
if (quoteUri != null)
{
@ -149,10 +155,10 @@ public class MfmConverter(
await using var sw = new StringWriter();
await element.ToHtmlAsync(sw);
return (sw.ToString(), usedMedia);
return new MfmHtmlData(sw.ToString(), usedMedia);
}
public async Task<(string Html, List<MfmInlineMedia> InlineMedia)> ToHtmlAsync(
public async Task<MfmHtmlData> ToHtmlAsync(
string mfm, List<Note.MentionedUser> mentions, string? host, string? quoteUri = null,
bool quoteInaccessible = false, bool replyInaccessible = false, string rootElement = "p",
List<Emoji>? emoji = null, List<MfmInlineMedia>? media = null
@ -164,19 +170,19 @@ public class MfmConverter(
}
private INode FromMfmNode(
IDocument document, IMfmNode node, List<Note.MentionedUser> mentions, string? host, ref List<MfmInlineMedia> usedMedia,
IDocument document, IMfmNode node, List<Note.MentionedUser> mentions, string? host, List<MfmInlineMedia> usedMedia,
List<Emoji>? emoji = null, List<MfmInlineMedia>? media = null
)
{
switch (node)
{
case MfmFnNode { Name: "media" } fn when media != null:
case MfmFnNode { Name: "media" } fn when media is { Count: > 0 }:
{
var urlNode = fn.Children.FirstOrDefault();
if (urlNode is MfmUrlNode url)
{
var current = media.FirstOrDefault(m => m.Src == url.Url);
if (current != null)
MfmInlineMedia? maybeCurrent = media.FirstOrDefault(m => m.Src == url.Url);
if (maybeCurrent is { } current)
{
usedMedia.Add(current);
@ -220,7 +226,7 @@ public class MfmConverter(
{
var el = CreateInlineFormattingElement(document, "i");
AddHtmlMarkup(document, el, "*");
AppendChildren(el, document, node, mentions, host, ref usedMedia);
AppendChildren(el, document, node, mentions, host, usedMedia);
AddHtmlMarkup(document, el, "*");
return el;
}
@ -229,21 +235,21 @@ public class MfmConverter(
{
var el = CreateInlineFormattingElement(document, "b");
AddHtmlMarkup(document, el, "**");
AppendChildren(el, document, node, mentions, host, ref usedMedia);
AppendChildren(el, document, node, mentions, host, usedMedia);
AddHtmlMarkup(document, el, "**");
return el;
}
case MfmSmallNode:
{
var el = document.CreateElement("small");
AppendChildren(el, document, node, mentions, host, ref usedMedia);
AppendChildren(el, document, node, mentions, host, usedMedia);
return el;
}
case MfmStrikeNode:
{
var el = CreateInlineFormattingElement(document, "del");
AddHtmlMarkup(document, el, "~~");
AppendChildren(el, document, node, mentions, host, ref usedMedia);
AppendChildren(el, document, node, mentions, host, usedMedia);
AddHtmlMarkup(document, el, "~~");
return el;
}
@ -252,7 +258,7 @@ public class MfmConverter(
{
var el = CreateInlineFormattingElement(document, "i");
AddHtmlMarkup(document, el, "*");
AppendChildren(el, document, node, mentions, host, ref usedMedia);
AppendChildren(el, document, node, mentions, host, usedMedia);
AddHtmlMarkup(document, el, "*");
return el;
}
@ -267,7 +273,7 @@ public class MfmConverter(
case MfmCenterNode:
{
var el = document.CreateElement("div");
AppendChildren(el, document, node, mentions, host, ref usedMedia);
AppendChildren(el, document, node, mentions, host, usedMedia);
return el;
}
case MfmEmojiCodeNode emojiCodeNode:
@ -357,7 +363,7 @@ public class MfmConverter(
{
var el = CreateInlineFormattingElement(document, "blockquote");
AddHtmlMarkup(document, el, "> ");
AppendChildren(el, document, node, mentions, host, ref usedMedia);
AppendChildren(el, document, node, mentions, host, usedMedia);
el.AppendChild(document.CreateElement("br"));
return el;
}
@ -391,7 +397,7 @@ public class MfmConverter(
case MfmPlainNode:
{
var el = document.CreateElement("span");
AppendChildren(el, document, node, mentions, host, ref usedMedia);
AppendChildren(el, document, node, mentions, host, usedMedia);
return el;
}
default:
@ -403,11 +409,11 @@ public class MfmConverter(
private void AppendChildren(
INode element, IDocument document, IMfmNode parent,
List<Note.MentionedUser> mentions, string? host, ref List<MfmInlineMedia> usedMedia,
List<Note.MentionedUser> mentions, string? host, List<MfmInlineMedia> usedMedia,
List<Emoji>? emoji = null, List<MfmInlineMedia>? media = null
)
{
foreach (var node in parent.Children) element.AppendNodes(FromMfmNode(document, node, mentions, host, ref usedMedia, emoji, media));
foreach (var node in parent.Children) element.AppendNodes(FromMfmNode(document, node, mentions, host, usedMedia, emoji, media));
}
private IElement CreateInlineFormattingElement(IDocument document, string name)

View file

@ -87,7 +87,7 @@ internal class HtmlParser(IEnumerable<Note.MentionedUser> mentions, ICollection<
if (node is not HtmlElement el) return node.TextContent;
var src = el.GetAttribute("src");
if (!Uri.IsWellFormedUriString(src, UriKind.Absolute))
if (src == null || !Uri.TryCreate(src, UriKind.Absolute, out var uri) && uri is { Scheme: "http" or "https" })
return node.TextContent;
var alt = el.GetAttribute("alt") ?? el.GetAttribute("title");
@ -102,7 +102,7 @@ internal class HtmlParser(IEnumerable<Note.MentionedUser> mentions, ICollection<
media.Add(new MfmInlineMedia(type, src, alt));
return $"$[media {src} ]";
return $"$[media {src}]";
}
case "P":

View file

@ -494,10 +494,7 @@ public class NoteService(
if (node is MfmFnNode { Name: "media" } fn)
{
var urlNode = fn.Children.FirstOrDefault();
if (urlNode is MfmUrlNode url)
{
urls.Add(url.Url);
}
if (urlNode is MfmUrlNode url) urls.Add(url.Url);
}
urls.AddRange(GetInlineMediaUrls(node.Children));
@ -1075,9 +1072,7 @@ public class NoteService(
List<MfmInlineMedia>? htmlInlineMedia = null;
if (text == null)
{
(text, htmlInlineMedia) = await MfmConverter.FromHtmlAsync(note.Content, mentionData.Mentions);
}
var cw = note.Summary;