//-----------------------------------------------------------------------
//
// Copyright (c) aliasvault. All rights reserved.
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
//
//-----------------------------------------------------------------------
namespace AliasVault.TaskRunner.Tasks;
using AliasServerDb;
using AliasVault.Shared.Server.Services;
using Microsoft.EntityFrameworkCore;
///
/// A maintenance task that deletes emails for disabled aliases after the configured retention period.
///
public class DisabledEmailCleanupTask : IMaintenanceTask
{
private readonly ILogger _logger;
private readonly IAliasServerDbContextFactory _dbContextFactory;
private readonly ServerSettingsService _settingsService;
///
/// Initializes a new instance of the class.
///
/// The logger.
/// The database context factory.
/// The settings service.
public DisabledEmailCleanupTask(
ILogger logger,
IAliasServerDbContextFactory dbContextFactory,
ServerSettingsService settingsService)
{
_logger = logger;
_dbContextFactory = dbContextFactory;
_settingsService = settingsService;
}
///
public string Name => "Disabled Email Cleanup";
///
public async Task ExecuteAsync(CancellationToken cancellationToken)
{
var settings = await _settingsService.GetAllSettingsAsync();
if (settings.DisabledEmailRetentionDays <= 0)
{
_logger.LogDebug("Disabled email cleanup is disabled (retention days set to 0)");
return;
}
await using var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var cutoffDate = DateTime.UtcNow.AddDays(-settings.DisabledEmailRetentionDays);
// Find all aliases that are currently disabled and last modified before the cutoff date.
var disabledAliasAddresses = await dbContext.UserEmailClaims
.Where(x => x.Disabled)
.Select(x => x.Address)
.ToListAsync(cancellationToken);
if (disabledAliasAddresses.Count == 0)
{
_logger.LogDebug("No disabled aliases found that need cleanup");
return;
}
// Delete all emails for these disabled aliases
var deletedCount = await dbContext.Emails
.Where(x => disabledAliasAddresses.Contains(x.To) && x.DateSystem <= cutoffDate)
.ExecuteDeleteAsync(cancellationToken);
if (deletedCount > 0)
{
_logger.LogInformation("Deleted {Count} emails for {AliasCount} disabled aliases.", deletedCount, disabledAliasAddresses.Count);
}
}
}