From 4dac1659786ffdd2169f2313532dc1922ffc3d42 Mon Sep 17 00:00:00 2001 From: pancakes Date: Thu, 13 Mar 2025 12:44:23 +1000 Subject: [PATCH] [backend/api] Try reconstructing threads when importing notes and escape mentions --- .../Core/Services/ImportExportService.cs | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/Iceshrimp.Backend/Core/Services/ImportExportService.cs b/Iceshrimp.Backend/Core/Services/ImportExportService.cs index a7648482..d21b3b89 100644 --- a/Iceshrimp.Backend/Core/Services/ImportExportService.cs +++ b/Iceshrimp.Backend/Core/Services/ImportExportService.cs @@ -65,20 +65,22 @@ public class ImportExportService( if (notes == null) throw GracefulException.BadRequest("File is not an Iceshrimp note export"); - notes = notes.Where(p => p is - { - LocalOnly: false, - Poll: null, - RenoteId: null, - ReplyId: null, - Visibility: Note.NoteVisibility.Public or Note.NoteVisibility.Home, - VisibleUserIds: [] - }) - .OrderByDescending(p => p.Id) - .ToList(); + notes = notes.FindAll(p => p is + { + LocalOnly: false, + Poll: null, + Visibility: Note.NoteVisibility.Public or Note.NoteVisibility.Home, + VisibleUserIds: [] + }); + var importedNotes = new Dictionary(); foreach (var exportNote in notes) { + if (exportNote.ReplyId != null && !importedNotes.ContainsKey(exportNote.ReplyId)) + continue; + if (exportNote.RenoteId != null && !importedNotes.ContainsKey(exportNote.RenoteId)) + continue; + var attachments = new List(); foreach (var exportNoteFile in exportNote.Files) { @@ -91,13 +93,17 @@ public class ImportExportService( { User = user, Visibility = exportNote.Visibility, - Text = exportNote.Text, + Text = exportNote.Text?.Replace("@", "@\\"), Cw = exportNote.Cw, + Reply = exportNote.ReplyId != null ? importedNotes.GetValueOrDefault(exportNote.ReplyId) : null, + Renote = exportNote.RenoteId != null ? importedNotes.GetValueOrDefault(exportNote.RenoteId) : null, Attachments = attachments.Count != 0 ? attachments : null, CreatedAt = exportNote.CreatedAt }; - await noteSvc.CreateNoteAsync(note); + var newNote = await noteSvc.CreateNoteAsync(note); + + importedNotes.Add(exportNote.Id, newNote); } } } \ No newline at end of file