2021-10-21 20:04:26 +00:00
using System ;
2021-03-05 23:30:08 +00:00
using System.Collections.Generic ;
2013-10-13 02:46:36 +00:00
using System.Linq ;
2023-11-10 02:55:01 +00:00
using FluentValidation.Results ;
2013-10-12 18:44:40 +00:00
using NLog ;
2014-02-26 05:40:47 +00:00
using NzbDrone.Core.Messaging.Events ;
2013-10-12 18:44:40 +00:00
using NzbDrone.Core.ThingiProvider ;
namespace NzbDrone.Core.Notifications
{
public interface INotificationFactory : IProviderFactory < INotification , NotificationDefinition >
{
2023-08-12 19:59:22 +00:00
List < INotification > OnGrabEnabled ( bool filterBlockedNotifications = true ) ;
List < INotification > OnDownloadEnabled ( bool filterBlockedNotifications = true ) ;
List < INotification > OnUpgradeEnabled ( bool filterBlockedNotifications = true ) ;
List < INotification > OnRenameEnabled ( bool filterBlockedNotifications = true ) ;
List < INotification > OnMovieAddedEnabled ( bool filterBlockedNotifications = true ) ;
List < INotification > OnMovieDeleteEnabled ( bool filterBlockedNotifications = true ) ;
List < INotification > OnMovieFileDeleteEnabled ( bool filterBlockedNotifications = true ) ;
List < INotification > OnHealthIssueEnabled ( bool filterBlockedNotifications = true ) ;
List < INotification > OnHealthRestoredEnabled ( bool filterBlockedNotifications = true ) ;
List < INotification > OnApplicationUpdateEnabled ( bool filterBlockedNotifications = true ) ;
List < INotification > OnManualInteractionEnabled ( bool filterBlockedNotifications = true ) ;
2013-10-12 18:44:40 +00:00
}
public class NotificationFactory : ProviderFactory < INotification , NotificationDefinition > , INotificationFactory
{
2023-08-12 19:59:22 +00:00
private readonly INotificationStatusService _notificationStatusService ;
private readonly Logger _logger ;
public NotificationFactory ( INotificationStatusService notificationStatusService , INotificationRepository providerRepository , IEnumerable < INotification > providers , IServiceProvider container , IEventAggregator eventAggregator , Logger logger )
2014-02-26 05:40:47 +00:00
: base ( providerRepository , providers , container , eventAggregator , logger )
2013-10-12 18:44:40 +00:00
{
2023-08-12 19:59:22 +00:00
_notificationStatusService = notificationStatusService ;
_logger = logger ;
2013-10-13 02:46:36 +00:00
}
2013-10-12 18:44:40 +00:00
2023-08-21 18:05:23 +00:00
protected override List < NotificationDefinition > Active ( )
{
return base . Active ( ) . Where ( c = > c . Enable ) . ToList ( ) ;
}
2023-08-12 19:59:22 +00:00
public List < INotification > OnGrabEnabled ( bool filterBlockedNotifications = true )
2013-10-13 02:46:36 +00:00
{
2023-08-12 19:59:22 +00:00
if ( filterBlockedNotifications )
{
return FilterBlockedNotifications ( GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnGrab ) ) . ToList ( ) ;
}
2013-10-13 02:46:36 +00:00
return GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnGrab ) . ToList ( ) ;
}
2023-08-12 19:59:22 +00:00
public List < INotification > OnDownloadEnabled ( bool filterBlockedNotifications = true )
2013-10-13 02:46:36 +00:00
{
2023-08-12 19:59:22 +00:00
if ( filterBlockedNotifications )
{
return FilterBlockedNotifications ( GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnDownload ) ) . ToList ( ) ;
}
2013-10-13 02:46:36 +00:00
return GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnDownload ) . ToList ( ) ;
2013-10-12 18:44:40 +00:00
}
2014-02-11 01:09:31 +00:00
2023-08-12 19:59:22 +00:00
public List < INotification > OnUpgradeEnabled ( bool filterBlockedNotifications = true )
2014-02-11 01:09:31 +00:00
{
2023-08-12 19:59:22 +00:00
if ( filterBlockedNotifications )
{
return FilterBlockedNotifications ( GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnUpgrade ) ) . ToList ( ) ;
}
2014-02-11 01:09:31 +00:00
return GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnUpgrade ) . ToList ( ) ;
}
2015-05-20 23:22:10 +00:00
2023-08-12 19:59:22 +00:00
public List < INotification > OnRenameEnabled ( bool filterBlockedNotifications = true )
2015-05-20 23:22:10 +00:00
{
2023-08-12 19:59:22 +00:00
if ( filterBlockedNotifications )
{
return FilterBlockedNotifications ( GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnRename ) ) . ToList ( ) ;
}
2015-05-20 23:22:10 +00:00
return GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnRename ) . ToList ( ) ;
}
2023-08-12 19:59:22 +00:00
public List < INotification > OnMovieAddedEnabled ( bool filterBlockedNotifications = true )
2022-06-03 01:44:47 +00:00
{
2023-08-12 19:59:22 +00:00
if ( filterBlockedNotifications )
{
return FilterBlockedNotifications ( GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnMovieAdded ) ) . ToList ( ) ;
}
2022-06-03 01:44:47 +00:00
return GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnMovieAdded ) . ToList ( ) ;
}
2023-08-12 19:59:22 +00:00
public List < INotification > OnMovieDeleteEnabled ( bool filterBlockedNotifications = true )
2019-11-04 20:44:33 +00:00
{
2023-08-12 19:59:22 +00:00
if ( filterBlockedNotifications )
{
return FilterBlockedNotifications ( GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnMovieDelete ) ) . ToList ( ) ;
}
2021-03-05 23:30:08 +00:00
return GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnMovieDelete ) . ToList ( ) ;
}
2023-08-12 19:59:22 +00:00
public List < INotification > OnMovieFileDeleteEnabled ( bool filterBlockedNotifications = true )
2021-03-05 23:30:08 +00:00
{
2023-08-12 19:59:22 +00:00
if ( filterBlockedNotifications )
{
return FilterBlockedNotifications ( GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnMovieFileDelete ) ) . ToList ( ) ;
}
2021-03-05 23:30:08 +00:00
return GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnMovieFileDelete ) . ToList ( ) ;
2019-11-04 20:44:33 +00:00
}
2023-08-12 19:59:22 +00:00
public List < INotification > OnMovieFileDeleteForUpgradeEnabled ( bool filterBlockedNotifications = true )
2020-08-16 04:32:53 +00:00
{
2023-08-12 19:59:22 +00:00
if ( filterBlockedNotifications )
{
return FilterBlockedNotifications ( GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnMovieFileDeleteForUpgrade ) ) . ToList ( ) ;
}
2021-03-05 23:30:08 +00:00
return GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnMovieFileDeleteForUpgrade ) . ToList ( ) ;
}
2023-08-12 19:59:22 +00:00
public List < INotification > OnHealthIssueEnabled ( bool filterBlockedNotifications = true )
2021-03-05 23:30:08 +00:00
{
2023-08-12 19:59:22 +00:00
if ( filterBlockedNotifications )
{
return FilterBlockedNotifications ( GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnHealthIssue ) ) . ToList ( ) ;
}
2021-03-05 23:30:08 +00:00
return GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnHealthIssue ) . ToList ( ) ;
2020-08-16 04:32:53 +00:00
}
2023-08-12 19:59:22 +00:00
public List < INotification > OnHealthRestoredEnabled ( bool filterBlockedNotifications = true )
2023-05-08 01:57:14 +00:00
{
2023-08-12 19:59:22 +00:00
if ( filterBlockedNotifications )
{
return FilterBlockedNotifications ( GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnHealthRestored ) ) . ToList ( ) ;
}
2023-05-08 01:57:14 +00:00
return GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnHealthRestored ) . ToList ( ) ;
}
2023-08-12 19:59:22 +00:00
public List < INotification > OnApplicationUpdateEnabled ( bool filterBlockedNotifications = true )
2021-12-21 05:12:12 +00:00
{
2023-08-12 19:59:22 +00:00
if ( filterBlockedNotifications )
{
return FilterBlockedNotifications ( GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnApplicationUpdate ) ) . ToList ( ) ;
}
2021-12-21 05:12:12 +00:00
return GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnApplicationUpdate ) . ToList ( ) ;
}
2023-08-12 19:59:22 +00:00
public List < INotification > OnManualInteractionEnabled ( bool filterBlockedNotifications = true )
2022-10-17 16:19:54 +00:00
{
2023-08-12 19:59:22 +00:00
if ( filterBlockedNotifications )
{
return FilterBlockedNotifications ( GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnManualInteractionRequired ) ) . ToList ( ) ;
}
2022-10-17 16:19:54 +00:00
return GetAvailableProviders ( ) . Where ( n = > ( ( NotificationDefinition ) n . Definition ) . OnManualInteractionRequired ) . ToList ( ) ;
}
2023-08-12 19:59:22 +00:00
private IEnumerable < INotification > FilterBlockedNotifications ( IEnumerable < INotification > 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 ;
}
}
2016-03-25 00:56:29 +00:00
public override void SetProviderCharacteristics ( INotification provider , NotificationDefinition definition )
2015-05-20 23:22:10 +00:00
{
2016-03-25 00:56:29 +00:00
base . SetProviderCharacteristics ( provider , definition ) ;
2015-05-20 23:22:10 +00:00
definition . SupportsOnGrab = provider . SupportsOnGrab ;
definition . SupportsOnDownload = provider . SupportsOnDownload ;
definition . SupportsOnUpgrade = provider . SupportsOnUpgrade ;
definition . SupportsOnRename = provider . SupportsOnRename ;
2022-06-04 18:20:46 +00:00
definition . SupportsOnMovieAdded = provider . SupportsOnMovieAdded ;
2021-03-05 23:30:08 +00:00
definition . SupportsOnMovieDelete = provider . SupportsOnMovieDelete ;
definition . SupportsOnMovieFileDelete = provider . SupportsOnMovieFileDelete ;
definition . SupportsOnMovieFileDeleteForUpgrade = provider . SupportsOnMovieFileDeleteForUpgrade ;
2019-11-04 20:44:33 +00:00
definition . SupportsOnHealthIssue = provider . SupportsOnHealthIssue ;
2023-05-08 01:57:14 +00:00
definition . SupportsOnHealthRestored = provider . SupportsOnHealthRestored ;
2021-12-21 05:12:12 +00:00
definition . SupportsOnApplicationUpdate = provider . SupportsOnApplicationUpdate ;
2022-10-17 16:19:54 +00:00
definition . SupportsOnManualInteractionRequired = provider . SupportsOnManualInteractionRequired ;
2015-05-20 23:22:10 +00:00
}
2023-11-10 02:55:01 +00:00
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 ;
}
2013-10-12 18:44:40 +00:00
}
2019-12-22 21:24:10 +00:00
}