[backend/api] Add follow list export endpoint
This commit is contained in:
parent
62aa5f3075
commit
bfc36cbc48
2 changed files with 42 additions and 0 deletions
|
@ -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;
|
||||
|
||||
|
@ -66,6 +67,23 @@ public class SettingsController(DatabaseContext db, UserService userSvc) : Contr
|
|||
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)]
|
||||
public async Task<AcceptedResult> ImportFollowing(IFormFile 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("@")))
|
||||
|
|
Loading…
Add table
Reference in a new issue