[backend/api] Add follow list export endpoint

This commit is contained in:
pancakes 2024-10-27 19:29:21 +10:00 committed by Laura Hausmann
parent 62aa5f3075
commit bfc36cbc48
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 42 additions and 0 deletions

View file

@ -9,6 +9,7 @@ using Iceshrimp.Backend.Core.Services;
using Iceshrimp.Shared.Schemas.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.EntityFrameworkCore;
namespace Iceshrimp.Backend.Controllers.Web;
@ -65,6 +66,23 @@ public class SettingsController(DatabaseContext db, UserService userSvc) : Contr
await db.ReloadEntityAsync(settings);
return settings;
}
[HttpPost("export/following")]
[ProducesResults(HttpStatusCode.Accepted)]
[ProducesErrors(HttpStatusCode.BadRequest)]
public async Task<AcceptedResult> ExportFollowing()
{
var user = HttpContext.GetUserOrFail();
var followCount = await db.Followings
.CountAsync(p => p.FollowerId == user.Id);
if (followCount < 1)
throw GracefulException.BadRequest("You do not follow any users");
await userSvc.ExportFollowingAsync(user);
return Accepted();
}
[HttpPost("import/following")]
[ProducesResults(HttpStatusCode.Accepted)]

View file

@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using AsyncKeyedLock;
using EntityFramework.Exceptions.Common;
@ -946,6 +947,29 @@ public class UserService(
await db.UserListMembers.Where(p => p.UserList.User == user && p.User == followee).ExecuteDeleteAsync();
}
public async Task ExportFollowingAsync(User user)
{
var followees = await db.Followings
.Include(p => p.Followee)
.Where(p => p.FollowerId == user.Id)
.Select(p => p.Followee)
.Where(p => !p.IsDeleted && !p.IsSystemUser && p.MovedToUri == null)
.OrderBy(p => p.Host)
.ThenBy(p => p.UsernameLower)
.Select(p => p.GetFqn(instance.Value.AccountDomain))
.ToListAsync();
var stream = new MemoryStream(Encoding.UTF8.GetBytes(string.Join("\n", followees)));
await driveSvc.StoreFile(stream, user,
new DriveFileCreationRequest
{
Filename = $"following-{DateTime.UtcNow:yyyy-MM-dd-HH-mm-ss}.csv",
IsSensitive = false,
MimeType = "text/csv"
}, true);
}
public async Task ImportFollowingAsync(User user, List<string> fqns)
{
foreach (var fqn in fqns.Select(fqn => fqn.Split("@")))