✨ Push token clean up when invalid
This commit is contained in:
35
DysonNetwork.Ring/Services/PushSubFlushHandler.cs
Normal file
35
DysonNetwork.Ring/Services/PushSubFlushHandler.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using DysonNetwork.Shared.Cache;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Quartz;
|
||||
|
||||
namespace DysonNetwork.Ring.Services;
|
||||
|
||||
public class PushSubRemovalRequest
|
||||
{
|
||||
public Guid SubId { get; set; }
|
||||
}
|
||||
|
||||
public class PushSubFlushHandler(IServiceProvider sp) : IFlushHandler<PushSubRemovalRequest>
|
||||
{
|
||||
public async Task FlushAsync(IReadOnlyList<PushSubRemovalRequest> items)
|
||||
{
|
||||
using var scope = sp.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDatabase>();
|
||||
var logger = scope.ServiceProvider.GetRequiredService<ILogger<PushSubFlushHandler>>();
|
||||
|
||||
var tokenIds = items.Select(x => x.SubId).Distinct().ToList();
|
||||
|
||||
var count = await db.PushSubscriptions
|
||||
.Where(s => tokenIds.Contains(s.Id))
|
||||
.ExecuteDeleteAsync();
|
||||
logger.LogInformation("Removed {Count} invalid push notification tokens...", count);
|
||||
}
|
||||
}
|
||||
|
||||
public class PushSubFlushJob(FlushBufferService fbs, PushSubFlushHandler hdl) : IJob
|
||||
{
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
await fbs.FlushAsync(hdl);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user