using Cleanuparr.Domain.Entities; using Cleanuparr.Infrastructure.Features.Context; using Cleanuparr.Infrastructure.Services.Interfaces; using Cleanuparr.Persistence.Models.Configuration.QueueCleaner; using Microsoft.Extensions.Logging; namespace Cleanuparr.Infrastructure.Services; public class RuleManager : IRuleManager { private readonly ILogger _logger; public RuleManager(ILogger logger) { _logger = logger; } public StallRule? GetMatchingStallRule(ITorrentItemWrapper torrent) { var stallRules = ContextProvider.Get>(nameof(StallRule)); return GetMatchingQueueRule(torrent, stallRules); } public SlowRule? GetMatchingSlowRule(ITorrentItemWrapper torrent) { var slowRules = ContextProvider.Get>(nameof(SlowRule)); return GetMatchingQueueRule(torrent, slowRules); } private TRule? GetMatchingQueueRule(ITorrentItemWrapper torrent, IReadOnlyList rules) where TRule : QueueRule { if (rules.Count is 0) { return null; } List matchedRules = rules .Where(x => x.Enabled && x.MatchesTorrent(torrent)) .ToList(); if (matchedRules.Count > 1) { _logger.LogWarning("skip | multiple {type} rules matched | {name}", typeof(TRule).Name, torrent.Name); return null; } return matchedRules.FirstOrDefault(); } }