Add OBS podcast ingestion and account withdrawal privacy controls

Support scoped RTMPS publishing through MediaMTX, authenticated replay, resource accounting and PostIt activation/revocation. Close withdrawn accounts, erase managed files and purge eligible payment residues after one year. Include migrations, documentation and regression tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Paul Schneider 2026-11-08 07:43:15 +00:00
commit b157dce4e4
Signed by: notazof
GPG key ID: AD4B9B545FBD7D12
54 changed files with 12996 additions and 50 deletions

View file

@ -0,0 +1,208 @@
#nullable enable
using System.Text.Json;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Yavsc.Models;
using Yavsc.Server.Helpers;
using Yavsc.Services;
namespace Yavsc.Web.Services;
public interface IAccountWithdrawalService
{
Task WithdrawAsync(string userId, CancellationToken ct);
}
public sealed class AccountWithdrawalService(ApplicationDbContext db, UserManager<ApplicationUser> users,
IPayPalSubscriptionGateway paypal, IOptions<SiteSettings> site, IOptions<PodcastOptions> podcasts,
TimeProvider clock, ILogger<AccountWithdrawalService> logger) : IAccountWithdrawalService
{
public async Task WithdrawAsync(string userId, CancellationToken ct)
{
var user = await users.FindByIdAsync(userId)
?? throw new SubscriptionOperationException(404, "Compte introuvable.");
if (user.WithdrawnAt is not null) return;
var userName = user.UserName ?? throw new InvalidOperationException("Withdrawal requires a storage owner name.");
var personalDirectory = Child(site.Value.UserFilesRoot, userName);
var avatarFiles = new[] { ".png", ".s.png", ".xs.png" }.Select(ext => Child(site.Value.Avatars, userName + ext)).ToArray();
var started = await ResourceUsageTracker.WithUserLockAsync(db, userId, async () =>
{
await db.Entry(user).ReloadAsync(ct);
if (user.WithdrawnAt is not null) return false;
user.WithdrawalRequestedAt ??= clock.GetUtcNow();
user.LockoutEnabled = true;
user.LockoutEnd = DateTimeOffset.MaxValue;
user.SecurityStamp = Guid.NewGuid().ToString();
await db.SaveChangesAsync(ct);
return true;
}, ct);
if (!started) return;
var subscriptions = await db.ResourceSubscriptions.Where(s => s.UserId == userId).ToListAsync(ct);
foreach (var subscription in subscriptions)
{
if (subscription.State is "CANCELLED" or "EXPIRED") continue;
if (subscription.PayPalId is null)
{
if (subscription.State == "CREATING")
throw new SubscriptionOperationException(409, "Une creation PayPal doit etre rapprochee avant de confirmer le retrait.");
}
else
{
var remote = await paypal.GetAsync(subscription.PayPalId, ct);
if (remote.State is not ("CANCELLED" or "EXPIRED"))
await paypal.CancelAsync(subscription.PayPalId, ct);
}
subscription.State = "CANCELLED";
subscription.NextBillingAt = null;
await db.SaveChangesAsync(ct);
}
DeleteDirectory(personalDirectory);
foreach (var path in avatarFiles)
{
RejectLink(path);
if (Directory.Exists(Path.GetDirectoryName(path))) File.Delete(path);
}
await DeletePodcastsAsync(userId, ct);
await ResourceUsageTracker.WithUserLockAsync(db, userId, async () =>
{
await db.Entry(user).ReloadAsync(ct);
if (user.WithdrawnAt is not null) return true;
db.DeviceDeclaration.RemoveRange(await db.DeviceDeclaration.Where(d => d.DeviceOwnerId == userId).ToListAsync(ct));
db.UserLogins.RemoveRange(await db.UserLogins.Where(d => d.UserId == userId).ToListAsync(ct));
db.UserTokens.RemoveRange(await db.UserTokens.Where(d => d.UserId == userId).ToListAsync(ct));
db.UserClaims.RemoveRange(await db.UserClaims.Where(d => d.UserId == userId).ToListAsync(ct));
db.UserRoles.RemoveRange(await db.UserRoles.Where(d => d.UserId == userId).ToListAsync(ct));
db.BankIdentity.RemoveRange(await db.BankIdentity.Where(d => d.UserId == userId).ToListAsync(ct));
db.Contact.RemoveRange(await db.Contact.Where(d => d.OwnerId == userId || d.UserId == userId).ToListAsync(ct));
db.ResourceUsageRecords.RemoveRange(await db.ResourceUsageRecords.Where(d => d.UserId == userId).ToListAsync(ct));
db.ResourceStorageRecords.RemoveRange(await db.ResourceStorageRecords.Where(d => d.UserId == userId).ToListAsync(ct));
var posts = await db.BlogSpot.Where(p => p.AuthorId == userId).ToListAsync(ct);
foreach (var post in posts)
{
post.Title = "Contenu retire";
post.Article = null;
post.Photo = null;
}
var comments = await db.Comment.Where(p => p.AuthorId == userId).ToListAsync(ct);
foreach (var comment in comments) comment.Article = "Contenu retire";
var performer = await db.Performers.SingleOrDefaultAsync(p => p.PerformerId == userId, ct);
if (performer is not null)
{
performer.Active = false;
performer.AcceptNotifications = false;
performer.AcceptPublicContact = false;
performer.UseGeoLocalizationToReduceDistanceWithClients = false;
performer.WebSite = "";
performer.DonationPayPalEmail = null;
}
var flows = await db.LiveFlow.Where(f => f.OwnerId == userId).ToListAsync(ct);
foreach (var flow in flows)
{
flow.Title = "Contenu retire";
flow.Pitch = "";
flow.PublicAnnouncement = "";
flow.DifferedFileName = "";
flow.PodcastDeleted = true;
flow.ObsKeyHash = null;
flow.ObsEnabledUntil = null;
flow.ObsConnectionId = null;
flow.ObsLastError = null;
}
var fileIds = await db.UploadedFiles.Where(f => f.OwnerId == userId
|| db.BlogAttachedFiles.Any(a => a.FileId == f.Id && a.Post.AuthorId == userId))
.Select(f => f.Id).ToListAsync(ct);
var files = await db.UploadedFiles.Where(f => fileIds.Contains(f.Id)).ToListAsync(ct);
foreach (var file in files)
{
file.Path = "";
file.Length = 0;
file.ContentType = "application/octet-stream";
}
user.UserName = "withdrawn-" + Guid.NewGuid().ToString("N");
user.NormalizedUserName = user.UserName.ToUpperInvariant();
user.Email = user.UserName + "@invalid.invalid";
user.NormalizedEmail = user.Email.ToUpperInvariant();
user.EmailConfirmed = false;
user.FullName = null;
user.PhoneNumber = null;
user.PhoneNumberConfirmed = false;
user.PasswordHash = null;
user.TwoFactorEnabled = false;
user.PostalAddressId = null;
user.DedicatedGoogleCalendar = null;
user.Avatar = Constants.DefaultAvatar;
user.AllowMonthlyEmail = false;
user.DiskUsage = 0;
user.PremiumDiskQuotaUntil = null;
user.DiscoveryDiskQuotaUntil = null;
user.WithdrawnAt = clock.GetUtcNow();
await db.SaveChangesAsync(ct);
return true;
}, ct);
logger.LogInformation("Account withdrawal completed, files erased and identity pseudonymized");
}
private async Task DeletePodcastsAsync(string userId, CancellationToken ct)
{
var root = Path.GetFullPath(podcasts.Value.RootPath);
if (!Directory.Exists(root)) return;
RejectLink(root);
foreach (var directory in Directory.EnumerateDirectories(root))
{
if (!Guid.TryParseExact(Path.GetFileName(directory), "N", out _)) continue;
RejectLink(directory);
var index = Path.Combine(directory, "index.json");
RejectLink(index);
if (!File.Exists(index)) continue;
await using var stream = File.OpenRead(index);
using var document = await JsonDocument.ParseAsync(stream, cancellationToken: ct);
var podcast = document.RootElement.GetProperty("Manifest").GetProperty("Podcast");
var owner = podcast.GetProperty("OwnerId").GetString();
if (owner == userId)
{
if (podcast.TryGetProperty("State", out var state) && state.GetString() == "Live"
|| podcast.TryGetProperty("Replay", out var replay) && replay.ValueKind == JsonValueKind.Object
&& replay.TryGetProperty("State", out var replayState) && replayState.GetString() == "Processing")
throw new SubscriptionOperationException(409, "Un podcast est encore en direct ou en cours de conversion ; attendre son arret avant de reprendre le retrait.");
await stream.DisposeAsync();
DeleteDirectory(directory);
}
}
}
private static string Child(string root, string name)
{
if (string.IsNullOrWhiteSpace(root) || string.IsNullOrWhiteSpace(name) || name != Path.GetFileName(name)
|| name is "." or ".." || name.Contains('\\'))
throw new InvalidOperationException("Invalid withdrawal storage configuration or user path.");
var fullRoot = Path.GetFullPath(root);
RejectLink(fullRoot);
var child = Path.GetFullPath(Path.Combine(fullRoot, name));
if (!child.StartsWith(fullRoot.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.Ordinal))
throw new InvalidOperationException("Withdrawal path escapes its storage root.");
return child;
}
private static void RejectLink(string path)
{
if ((File.Exists(path) || Directory.Exists(path)) && File.GetAttributes(path).HasFlag(FileAttributes.ReparsePoint))
throw new IOException("Withdrawal refuses symbolic links in managed storage.");
}
private static void DeleteDirectory(string path)
{
if (!Directory.Exists(path)) return;
RejectLink(path);
foreach (var entry in Directory.EnumerateFileSystemEntries(path))
{
RejectLink(entry);
if (Directory.Exists(entry)) DeleteDirectory(entry);
else File.Delete(entry);
}
Directory.Delete(path);
}
}

View file

@ -0,0 +1,38 @@
using Yavsc.Server.Helpers;
namespace Yavsc.Web.Services;
public sealed class PaymentResiduePurgeWorker(IServiceScopeFactory scopes,
ILogger<PaymentResiduePurgeWorker> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
var delay = TimeSpan.FromMinutes(1);
try
{
using var scope = scopes.CreateScope();
var residues = scope.ServiceProvider.GetRequiredService<IPaymentResidueService>();
await residues.PurgeAsync(stoppingToken);
var clock = scope.ServiceProvider.GetRequiredService<TimeProvider>();
var now = clock.GetUtcNow();
var expiry = await residues.GetNextExpiryAsync(stoppingToken);
if (expiry is not null)
{
var until = expiry.Value - now;
if (until < delay) delay = until > TimeSpan.Zero ? until : TimeSpan.Zero;
}
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
return;
}
catch (Exception ex)
{
logger.LogError(ex, "Automatic payment residue purge failed; retrying in one minute");
}
await Task.Delay(delay, stoppingToken);
}
}
}

View file

@ -38,6 +38,7 @@ public sealed class ResourceSubscriptionService(ApplicationDbContext db, IPayPal
throw new SubscriptionOperationException(400, "Pack inconnu.");
return ResourceUsageTracker.WithUserLockAsync(db, userId, async () =>
{
await RequireActiveAccountAsync(userId, ct);
var existing = await db.ResourceSubscriptions.Where(s => s.UserId == userId
&& s.State != "CANCELLED" && s.State != "EXPIRED").SingleOrDefaultAsync(ct);
if (existing is not null)
@ -69,6 +70,7 @@ public sealed class ResourceSubscriptionService(ApplicationDbContext db, IPayPal
public Task BindAsync(ResourceSubscription subscription, PayPalSubscriptionDetails remote, CancellationToken ct)
=> ResourceUsageTracker.WithUserLockAsync(db, subscription.UserId, async () =>
{
await RequireActiveAccountAsync(subscription.UserId, ct, allowPendingWithdrawal: true);
await db.Entry(subscription).ReloadAsync(ct);
CheckBinding(subscription, remote);
subscription.PayPalId = remote.Id;
@ -87,6 +89,7 @@ public sealed class ResourceSubscriptionService(ApplicationDbContext db, IPayPal
var transactions = await paypal.TransactionsAsync(remote.Id, subscription.CreatedAt, clock.GetUtcNow(), ct);
await ResourceUsageTracker.WithUserLockAsync(db, subscription.UserId, async () =>
{
await RequireActiveAccountAsync(subscription.UserId, ct);
await db.Entry(subscription).ReloadAsync(ct);
CheckBinding(subscription, remote);
subscription.State = remote.State;
@ -174,4 +177,11 @@ public sealed class ResourceSubscriptionService(ApplicationDbContext db, IPayPal
|| remote.State is not ("APPROVAL_PENDING" or "APPROVED" or "ACTIVE" or "SUSPENDED" or "CANCELLED" or "EXPIRED"))
throw new SubscriptionOperationException(409, "L'abonnement PayPal ne correspond pas au compte et au pack attendus.");
}
private async Task RequireActiveAccountAsync(string userId, CancellationToken ct, bool allowPendingWithdrawal = false)
{
if (await db.Users.AnyAsync(u => u.Id == userId &&
(u.WithdrawnAt != null || !allowPendingWithdrawal && u.WithdrawalRequestedAt != null), ct))
throw new SubscriptionOperationException(409, "Le compte est retire ; aucun nouveau credit ne peut etre attribue.");
}
}