feat(backend): initial data export support
obviously it's missing things that haven't been added yet
This commit is contained in:
parent
f0ae648492
commit
903be2709c
15 changed files with 502 additions and 24 deletions
74
Foxnouns.Backend/Controllers/ExportsController.cs
Normal file
74
Foxnouns.Backend/Controllers/ExportsController.cs
Normal file
|
@ -0,0 +1,74 @@
|
|||
using Coravel.Queuing.Interfaces;
|
||||
using Foxnouns.Backend.Database;
|
||||
using Foxnouns.Backend.Database.Models;
|
||||
using Foxnouns.Backend.Jobs;
|
||||
using Foxnouns.Backend.Middleware;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NodaTime;
|
||||
|
||||
namespace Foxnouns.Backend.Controllers;
|
||||
|
||||
[Route("/api/internal/data-exports")]
|
||||
[Authorize("identify")]
|
||||
public class ExportsController(
|
||||
ILogger logger,
|
||||
Config config,
|
||||
IClock clock,
|
||||
DatabaseContext db,
|
||||
IQueue queue
|
||||
) : ApiControllerBase
|
||||
{
|
||||
private static readonly Duration MinimumTimeBetween = Duration.FromDays(1);
|
||||
private readonly ILogger _logger = logger.ForContext<ExportsController>();
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetDataExportsAsync()
|
||||
{
|
||||
var export = await db
|
||||
.DataExports.Where(d => d.UserId == CurrentUser!.Id)
|
||||
.OrderByDescending(d => d.Id)
|
||||
.FirstOrDefaultAsync();
|
||||
if (export == null)
|
||||
return Ok(new DataExportResponse(null, null));
|
||||
|
||||
return Ok(
|
||||
new DataExportResponse(
|
||||
ExportUrl(CurrentUser!.Id, export.Filename),
|
||||
export.Id.Time + DataExport.Expiration
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private string ExportUrl(Snowflake userId, string filename) =>
|
||||
$"{config.MediaBaseUrl}/data-exports/{userId}/{filename}.zip";
|
||||
|
||||
private record DataExportResponse(string? Url, Instant? ExpiresAt);
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> QueueDataExportAsync()
|
||||
{
|
||||
var snowflakeToCheck = Snowflake.FromInstant(
|
||||
clock.GetCurrentInstant() - MinimumTimeBetween
|
||||
);
|
||||
_logger.Debug(
|
||||
"Checking if user {UserId} has data exports newer than {Snowflake}",
|
||||
CurrentUser!.Id,
|
||||
snowflakeToCheck
|
||||
);
|
||||
if (
|
||||
await db.DataExports.AnyAsync(d =>
|
||||
d.UserId == CurrentUser.Id && d.Id > snowflakeToCheck
|
||||
)
|
||||
)
|
||||
{
|
||||
throw new ApiError.BadRequest("You can't request a new data export so soon.");
|
||||
}
|
||||
|
||||
queue.QueueInvocableWithPayload<CreateDataExportInvocable, CreateDataExportPayload>(
|
||||
new CreateDataExportPayload(CurrentUser.Id)
|
||||
);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue