using System; using System.Collections.Generic; using System.Linq; using FluentValidation.Results; using NLog; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications { public interface INotificationFactory : IProviderFactory { List OnGrabEnabled(bool filterBlockedNotifications = true); List OnReleaseImportEnabled(bool filterBlockedNotifications = true); List OnUpgradeEnabled(bool filterBlockedNotifications = true); List OnRenameEnabled(bool filterBlockedNotifications = true); List OnArtistAddEnabled(bool filterBlockedNotifications = true); List OnArtistDeleteEnabled(bool filterBlockedNotifications = true); List OnAlbumDeleteEnabled(bool filterBlockedNotifications = true); List OnHealthIssueEnabled(bool filterBlockedNotifications = true); List OnHealthRestoredEnabled(bool filterBlockedNotifications = true); List OnDownloadFailureEnabled(bool filterBlockedNotifications = true); List OnImportFailureEnabled(bool filterBlockedNotifications = true); List OnTrackRetagEnabled(bool filterBlockedNotifications = true); List OnApplicationUpdateEnabled(bool filterBlockedNotifications = true); } public class NotificationFactory : ProviderFactory, INotificationFactory { private readonly INotificationStatusService _notificationStatusService; private readonly Logger _logger; public NotificationFactory(INotificationStatusService notificationStatusService, INotificationRepository providerRepository, IEnumerable providers, IServiceProvider container, IEventAggregator eventAggregator, Logger logger) : base(providerRepository, providers, container, eventAggregator, logger) { _notificationStatusService = notificationStatusService; _logger = logger; } protected override List Active() { return base.Active().Where(c => c.Enable).ToList(); } public List OnGrabEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnGrab)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnGrab).ToList(); } public List OnReleaseImportEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnReleaseImport)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnReleaseImport).ToList(); } public List OnUpgradeEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnUpgrade)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnUpgrade).ToList(); } public List OnRenameEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnRename)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnRename).ToList(); } public List OnArtistAddEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnArtistAdd)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnArtistAdd).ToList(); } public List OnArtistDeleteEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnArtistDelete)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnArtistDelete).ToList(); } public List OnAlbumDeleteEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnAlbumDelete)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnAlbumDelete).ToList(); } public List OnHealthIssueEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnHealthIssue)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnHealthIssue).ToList(); } public List OnHealthRestoredEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnHealthRestored)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnHealthRestored).ToList(); } public List OnDownloadFailureEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnDownloadFailure)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnDownloadFailure).ToList(); } public List OnImportFailureEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnImportFailure)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnImportFailure).ToList(); } public List OnTrackRetagEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnTrackRetag)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnTrackRetag).ToList(); } public List OnApplicationUpdateEnabled(bool filterBlockedNotifications = true) { if (filterBlockedNotifications) { return FilterBlockedNotifications(GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnApplicationUpdate)).ToList(); } return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnApplicationUpdate).ToList(); } private IEnumerable FilterBlockedNotifications(IEnumerable notifications) { var blockedNotifications = _notificationStatusService.GetBlockedProviders().ToDictionary(v => v.ProviderId, v => v); foreach (var notification in notifications) { if (blockedNotifications.TryGetValue(notification.Definition.Id, out var notificationStatus)) { _logger.Debug("Temporarily ignoring notification {0} till {1} due to recent failures.", notification.Definition.Name, notificationStatus.DisabledTill.Value.ToLocalTime()); continue; } yield return notification; } } public override void SetProviderCharacteristics(INotification provider, NotificationDefinition definition) { base.SetProviderCharacteristics(provider, definition); definition.SupportsOnGrab = provider.SupportsOnGrab; definition.SupportsOnReleaseImport = provider.SupportsOnReleaseImport; definition.SupportsOnUpgrade = provider.SupportsOnUpgrade; definition.SupportsOnRename = provider.SupportsOnRename; definition.SupportsOnArtistAdd = provider.SupportsOnArtistAdd; definition.SupportsOnArtistDelete = provider.SupportsOnArtistDelete; definition.SupportsOnAlbumDelete = provider.SupportsOnAlbumDelete; definition.SupportsOnHealthIssue = provider.SupportsOnHealthIssue; definition.SupportsOnHealthRestored = provider.SupportsOnHealthRestored; definition.SupportsOnDownloadFailure = provider.SupportsOnDownloadFailure; definition.SupportsOnImportFailure = provider.SupportsOnImportFailure; definition.SupportsOnTrackRetag = provider.SupportsOnTrackRetag; definition.SupportsOnApplicationUpdate = provider.SupportsOnApplicationUpdate; } public override ValidationResult Test(NotificationDefinition definition) { var result = base.Test(definition); if (definition.Id == 0) { return result; } if (result == null || result.IsValid) { _notificationStatusService.RecordSuccess(definition.Id); } else { _notificationStatusService.RecordFailure(definition.Id); } return result; } } }