Contactmodule: token- en inzendroute met spamafhandeling, idempotentie en retentietaak
Nieuwe module SlpModularCms.Modules.Contact met eigen DbContext en migratie InitialCreate. GET contact-messages/token en POST contact-messages zijn publiek ([PublicSiteEndpoint]), geven bij echte inzendingen, spam en herhalingen hetzelfde 202 en slaan spam apart op (30 dagen, dagplafond). Standaard staat de module uit (Contact:Enabled=false, 404 met lege body, geen databasetoegang) totdat de rate limiting per client op de basisbranch staat; de rate-limit-sleutel zit in een enkele functie zodat dat later een regel wijzigen is. Mail zit achter een seam (nog een no-op). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SlpModularCms.Modules.Contact.Data;
|
||||
using SlpModularCms.Modules.Contact.Data.Entities;
|
||||
|
||||
namespace SlpModularCms.Modules.Contact.Repositories;
|
||||
|
||||
public interface IContactRepository
|
||||
{
|
||||
Task<IdempotencyRecord?> GetIdempotencyAsync(string siteId, Guid key, CancellationToken cancellationToken);
|
||||
Task<int> CountSpamSinceAsync(string siteId, DateTimeOffset since, CancellationToken cancellationToken);
|
||||
void Add(ContactMessage message);
|
||||
void Add(IdempotencyRecord record);
|
||||
Task SaveChangesAsync(CancellationToken cancellationToken);
|
||||
Task<int> DeleteExpiredMessagesAsync(DateTimeOffset now, CancellationToken cancellationToken);
|
||||
Task<int> DeleteIdempotencyOlderThanAsync(DateTimeOffset cutoff, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
public class ContactRepository(ContactDbContext context) : IContactRepository
|
||||
{
|
||||
private const int BatchSize = 500;
|
||||
|
||||
public Task<IdempotencyRecord?> GetIdempotencyAsync(string siteId, Guid key, CancellationToken cancellationToken)
|
||||
=> context.IdempotencyRecords.FirstOrDefaultAsync(r => r.SiteId == siteId && r.Key == key, cancellationToken);
|
||||
|
||||
public Task<int> CountSpamSinceAsync(string siteId, DateTimeOffset since, CancellationToken cancellationToken)
|
||||
=> context.ContactMessages.CountAsync(
|
||||
m => m.SiteId == siteId && m.Status == ContactMessageStatus.Spam && m.ReceivedAt >= since,
|
||||
cancellationToken);
|
||||
|
||||
public void Add(ContactMessage message) => context.ContactMessages.Add(message);
|
||||
|
||||
public void Add(IdempotencyRecord record) => context.IdempotencyRecords.Add(record);
|
||||
|
||||
public Task SaveChangesAsync(CancellationToken cancellationToken) => context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
public async Task<int> DeleteExpiredMessagesAsync(DateTimeOffset now, CancellationToken cancellationToken)
|
||||
{
|
||||
var total = 0;
|
||||
while (true)
|
||||
{
|
||||
var batch = await context.ContactMessages
|
||||
.Where(m => m.RetentionUntil < now)
|
||||
.Take(BatchSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
if (batch.Count == 0)
|
||||
{
|
||||
return total;
|
||||
}
|
||||
|
||||
context.ContactMessages.RemoveRange(batch);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
total += batch.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> DeleteIdempotencyOlderThanAsync(DateTimeOffset cutoff, CancellationToken cancellationToken)
|
||||
{
|
||||
var total = 0;
|
||||
while (true)
|
||||
{
|
||||
var batch = await context.IdempotencyRecords
|
||||
.Where(r => r.CreatedAt < cutoff)
|
||||
.Take(BatchSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
if (batch.Count == 0)
|
||||
{
|
||||
return total;
|
||||
}
|
||||
|
||||
context.IdempotencyRecords.RemoveRange(batch);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
total += batch.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user