[backend/api] Add note import endpoint
This commit is contained in:
parent
d01af426db
commit
0a0fb9bfb4
2 changed files with 64 additions and 0 deletions
|
@ -210,6 +210,20 @@ public class SettingsController(
|
|||
return Accepted();
|
||||
}
|
||||
|
||||
[HttpPost("import/notes")]
|
||||
[EnableRateLimiting("imports")]
|
||||
[ProducesResults(HttpStatusCode.OK)]
|
||||
[ProducesErrors(HttpStatusCode.BadRequest)]
|
||||
public async Task ImportNotes(IFormFile file)
|
||||
{
|
||||
var user = HttpContext.GetUserOrFail();
|
||||
|
||||
if (file.ContentType != "application/json")
|
||||
throw GracefulException.BadRequest("File must be JSON");
|
||||
|
||||
await importExportSvc.ImportNotesAsync(user, file);
|
||||
}
|
||||
|
||||
private async Task<UserSettings> GetOrInitUserSettings()
|
||||
{
|
||||
var user = HttpContext.GetUserOrFail();
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
using System.Text.Json;
|
||||
using Iceshrimp.Backend.Controllers.Web.Schemas;
|
||||
using Iceshrimp.Backend.Core.Configuration;
|
||||
using Iceshrimp.Backend.Core.Database;
|
||||
using Iceshrimp.Backend.Core.Database.Tables;
|
||||
using Iceshrimp.Backend.Core.Extensions;
|
||||
using Iceshrimp.Backend.Core.Middleware;
|
||||
using Iceshrimp.MfmSharp;
|
||||
using Iceshrimp.Shared.Configuration;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using static Iceshrimp.Backend.Core.Federation.ActivityPub.UserResolver;
|
||||
|
@ -14,6 +19,8 @@ public class ImportExportService(
|
|||
IOptions<Config.InstanceSection> instance,
|
||||
CacheService cacheSvc,
|
||||
UserService userSvc,
|
||||
NoteService noteSvc,
|
||||
DriveService driveSvc,
|
||||
ActivityPub.UserResolver userResolver
|
||||
) : IScopedService
|
||||
{
|
||||
|
@ -50,4 +57,47 @@ public class ImportExportService(
|
|||
|
||||
await QueryableTimelineExtensions.ResetHeuristicAsync(user, cacheSvc);
|
||||
}
|
||||
|
||||
public async Task ImportNotesAsync(User user, IFormFile file)
|
||||
{
|
||||
var notes = await JsonSerializer.DeserializeAsync<List<ExportNote>>(file.OpenReadStream(), JsonSerialization.Options);
|
||||
|
||||
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();
|
||||
|
||||
foreach (var exportNote in notes)
|
||||
{
|
||||
var attachments = new List<DriveFile>();
|
||||
foreach (var exportNoteFile in exportNote.Files)
|
||||
{
|
||||
var attachment = await driveSvc.StoreFileAsync(exportNoteFile.Url, user, exportNoteFile.IsSensitive,
|
||||
exportNoteFile.Comment, exportNoteFile.Type);
|
||||
if (attachment != null) attachments.Add(attachment);
|
||||
}
|
||||
|
||||
var note = new NoteService.NoteCreationData
|
||||
{
|
||||
User = user,
|
||||
Visibility = exportNote.Visibility,
|
||||
Text = exportNote.Text,
|
||||
Cw = exportNote.Cw,
|
||||
Attachments = attachments.Count != 0 ? attachments : null,
|
||||
CreatedAt = exportNote.CreatedAt
|
||||
};
|
||||
|
||||
await noteSvc.CreateNoteAsync(note);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue