many fixes and improvements - rework for modules/ and common/
feat(emailer): add Postmark and Resend providers
This commit is contained in:
26
backend/Modules/Creators/Services/CreatorLookup.cs
Normal file
26
backend/Modules/Creators/Services/CreatorLookup.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Hutopy.Modules.Creators.Contracts;
|
||||
using Hutopy.Modules.Creators.Data;
|
||||
|
||||
namespace Hutopy.Modules.Creators.Services;
|
||||
|
||||
public sealed class CreatorLookup(
|
||||
CreatorsDbContext context)
|
||||
: ICreatorLookup
|
||||
{
|
||||
public async Task<CreatorReference?> GetCreatorAsync(Guid creatorId, CancellationToken cancellationToken)
|
||||
{
|
||||
Creator? creator = await context
|
||||
.Creators
|
||||
.FirstOrDefaultAsync(c => c.Id == creatorId, cancellationToken);
|
||||
|
||||
return creator is null
|
||||
? null
|
||||
: new CreatorReference(
|
||||
creator.Id,
|
||||
creator.Name,
|
||||
creator.PortraitUrl,
|
||||
creator.IsStripeDetailsSubmitted,
|
||||
creator.IsStripeChargesEnabled,
|
||||
creator.StripeAccountId);
|
||||
}
|
||||
}
|
||||
43
backend/Modules/Creators/Services/SlugPurger.cs
Normal file
43
backend/Modules/Creators/Services/SlugPurger.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Hutopy.Modules.Creators.Data;
|
||||
|
||||
namespace Hutopy.Modules.Creators.Services;
|
||||
|
||||
public class SlugPurger(CreatorsDbContext context)
|
||||
{
|
||||
private static readonly SemaphoreSlim Semaphore = new(1, 1);
|
||||
private static DateTimeOffset s_lastPurgeTime = DateTimeOffset.MinValue;
|
||||
private static readonly TimeSpan MinTimeBetweenPurges = TimeSpan.FromSeconds(10);
|
||||
|
||||
public async Task PurgeExpiredSlugsAsync(CancellationToken ct)
|
||||
{
|
||||
// Try to acquire the semaphore
|
||||
if (!await Semaphore.WaitAsync(0, ct))
|
||||
{
|
||||
// Another purge operation is in progress, skip this one
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
if (now - s_lastPurgeTime < MinTimeBetweenPurges)
|
||||
{
|
||||
// Not enough time has passed since the last purge
|
||||
return;
|
||||
}
|
||||
|
||||
// Delete expired slugs that are not in use
|
||||
await context
|
||||
.Slugs
|
||||
.Where(s => s.ReservedUntil < now && s.UsedBy == null)
|
||||
.ExecuteDeleteAsync(ct);
|
||||
|
||||
// Update the last purge time regardless of whether we found expired slugs or not
|
||||
s_lastPurgeTime = now;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Semaphore.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user