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,44 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SlpModularCms.Modules.Contact.Data.Entities;
|
||||
|
||||
namespace SlpModularCms.Modules.Contact.Data;
|
||||
|
||||
public class ContactDbContext(DbContextOptions<ContactDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<ContactMessage> ContactMessages => Set<ContactMessage>();
|
||||
public DbSet<IdempotencyRecord> IdempotencyRecords => Set<IdempotencyRecord>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<ContactMessage>(entity =>
|
||||
{
|
||||
entity.ToTable("ContactMessages");
|
||||
entity.HasKey(e => e.Id);
|
||||
entity.Property(e => e.SiteId).IsRequired().HasMaxLength(100);
|
||||
entity.Property(e => e.ReceivedAt).IsRequired();
|
||||
entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
|
||||
entity.Property(e => e.Email).IsRequired().HasMaxLength(254);
|
||||
entity.Property(e => e.Subject).IsRequired().HasMaxLength(100);
|
||||
entity.Property(e => e.Message).IsRequired().HasMaxLength(2000);
|
||||
entity.Property(e => e.Status).IsRequired().HasConversion<string>().HasMaxLength(20);
|
||||
entity.Property(e => e.MailStatus).IsRequired().HasConversion<string>().HasMaxLength(20);
|
||||
entity.Property(e => e.MailError).HasMaxLength(100);
|
||||
entity.Property(e => e.RetentionUntil).IsRequired();
|
||||
|
||||
entity.HasIndex(e => new { e.SiteId, e.Status, e.ReceivedAt });
|
||||
entity.HasIndex(e => e.RetentionUntil);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<IdempotencyRecord>(entity =>
|
||||
{
|
||||
entity.ToTable("ContactIdempotency");
|
||||
entity.HasKey(e => new { e.SiteId, e.Key });
|
||||
entity.Property(e => e.SiteId).IsRequired().HasMaxLength(100);
|
||||
entity.Property(e => e.CreatedAt).IsRequired();
|
||||
entity.Property(e => e.StatusCode).IsRequired();
|
||||
entity.HasIndex(e => e.CreatedAt);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
namespace SlpModularCms.Modules.Contact.Data.Entities;
|
||||
|
||||
public enum ContactMessageStatus
|
||||
{
|
||||
New,
|
||||
Read,
|
||||
Archived,
|
||||
Spam,
|
||||
Converted,
|
||||
}
|
||||
|
||||
public enum ContactMailStatus
|
||||
{
|
||||
/// <summary>Waiting to be mailed (also the state while no mail sender is available).</summary>
|
||||
Pending,
|
||||
Sent,
|
||||
Failed,
|
||||
/// <summary>Never mailed on purpose (spam).</summary>
|
||||
NotApplicable,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A message sent through the website's contact form. Has its own id and status so that it can
|
||||
/// later be turned into an assignment (quotes, invoices); that is not built yet.
|
||||
/// </summary>
|
||||
public class ContactMessage
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string SiteId { get; set; } = string.Empty;
|
||||
public DateTimeOffset ReceivedAt { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string Email { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Opaque value chosen by the site (a package id, a slug, or 'other').</summary>
|
||||
public string Subject { get; set; } = string.Empty;
|
||||
|
||||
public string Message { get; set; } = string.Empty;
|
||||
public ContactMessageStatus Status { get; set; }
|
||||
public ContactMailStatus MailStatus { get; set; }
|
||||
|
||||
/// <summary>Short error code of the last failed mail attempt (never an address or content).</summary>
|
||||
public string? MailError { get; set; }
|
||||
|
||||
public int MailAttempts { get; set; }
|
||||
public DateTimeOffset? NextMailAttemptAt { get; set; }
|
||||
|
||||
/// <summary>Fixed at insert: a later configuration change does not apply retroactively.</summary>
|
||||
public DateTimeOffset RetentionUntil { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>Remembers an Idempotency-Key for 24 hours. Holds no request content.</summary>
|
||||
public class IdempotencyRecord
|
||||
{
|
||||
public string SiteId { get; set; } = string.Empty;
|
||||
public Guid Key { get; set; }
|
||||
public DateTimeOffset CreatedAt { get; set; }
|
||||
public int StatusCode { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user