[backend/core] Fix CW edits not being processed correctly, replace CW line endings during note ingest

This commit is contained in:
Laura Hausmann 2024-12-15 00:34:29 +01:00
parent 2453e0f673
commit 010dd2bb96
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 39 additions and 8 deletions

View file

@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Infrastructure;
#nullable disable
namespace Iceshrimp.Backend.Core.Database.Migrations
{
/// <inheritdoc />
[DbContext(typeof(DatabaseContext))]
[Migration("20241214232528_NormalizeCwLineEndings")]
public partial class NormalizeCwLineEndings : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("""
UPDATE "note" SET "text" = regexp_replace("cw", '\r\n', '\n', 'g') WHERE "cw" ~ '\r\n';
UPDATE "note" SET "text" = regexp_replace("cw", '\r', '\n', 'g') WHERE "cw" ~ '\r';
""");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View file

@ -180,7 +180,7 @@ public class NoteService(
mentionsResolver.ResolveMentions(data.ParsedText, data.User.Host, mentions, splitDomainMapping);
}
data.Cw = data.Cw?.Trim();
data.Cw = data.Cw?.ReplaceLineEndings("\n").Trim();
if (data.Cw != null && string.IsNullOrWhiteSpace(data.Cw))
data.Cw = null;
@ -554,7 +554,7 @@ public class NoteService(
mentionsResolver.ResolveMentions(data.ParsedText, note.User.Host, mentions, splitDomainMapping);
}
data.Cw = data.Cw?.Trim();
data.Cw = data.Cw?.ReplaceLineEndings("\n").Trim();
if (data.Cw != null && string.IsNullOrWhiteSpace(data.Cw))
data.Cw = null;
@ -576,7 +576,6 @@ public class NoteService(
if (note.User.IsLocalUser && data.ParsedText != null)
data.Emoji = (await emojiSvc.ResolveEmojiAsync(data.ParsedText)).Select(p => p.Id).ToList();
if (data.Emoji != null && !note.Emojis.IsEquivalent(data.Emoji))
note.Emojis = data.Emoji;
else if (data.Emoji == null && note.Emojis.Count != 0)
@ -622,7 +621,6 @@ public class NoteService(
}
var attachments = data.Attachments?.ToList() ?? [];
var inlineMediaUrls = data.ParsedText != null ? GetInlineMediaUrls(data.ParsedText) : [];
var newMediaUrls = data.Attachments?.Select(p => p.Url) ?? [];
var missingUrls = inlineMediaUrls.Except(newMediaUrls).ToArray();
@ -648,6 +646,9 @@ public class NoteService(
note.CombinedAltText = string.Join(' ', combinedAltText);
}
note.Text = data.Text = data.ParsedText?.Serialize();
note.Cw = data.Cw;
var isPollEdited = false;
var poll = data.Poll;
@ -665,7 +666,11 @@ public class NoteService(
await EnqueuePollExpiryTaskAsync(note.Poll);
}
if (!note.Poll.Choices.SequenceEqual(poll.Choices) || note.Poll.Multiple != poll.Multiple)
if (
!note.Poll.Choices.SequenceEqual(poll.Choices)
|| note.Poll.Multiple != poll.Multiple
|| note.Text != noteEdit.Text
)
{
isPollEdited = true;
@ -720,8 +725,6 @@ public class NoteService(
}
}
note.Text = data.Text = data.ParsedText?.Serialize();
var isEdit = data.ASNote is not ASQuestion
|| poll == null
|| isPollEdited