From 25c5401a9d217ff92a03ad9d069a6f3eaf818279 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 12 Oct 2013 19:46:36 -0700 Subject: [PATCH] More Connects cleanup/fixing --- .../Indexers/FetchAndParseRssService.cs | 1 - src/NzbDrone.Core/Indexers/IndexerBase.cs | 3 +- src/NzbDrone.Core/Indexers/IndexerFactory.cs | 13 ++- src/NzbDrone.Core/Indexers/Newznab/Newznab.cs | 2 + .../Notifications/NotificationFactory.cs | 21 +++- .../Notifications/NotificationService.cs | 108 ++++++++++++++++++ src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + .../ThingiProvider/ProviderFactory.cs | 20 +++- src/UI/Settings/Notifications/ItemView.js | 2 +- 9 files changed, 157 insertions(+), 14 deletions(-) create mode 100644 src/NzbDrone.Core/Notifications/NotificationService.cs diff --git a/src/NzbDrone.Core/Indexers/FetchAndParseRssService.cs b/src/NzbDrone.Core/Indexers/FetchAndParseRssService.cs index 534668b6b..899714e67 100644 --- a/src/NzbDrone.Core/Indexers/FetchAndParseRssService.cs +++ b/src/NzbDrone.Core/Indexers/FetchAndParseRssService.cs @@ -39,7 +39,6 @@ namespace NzbDrone.Core.Indexers _logger.Debug("Available indexers {0}", indexers.Count); - var taskList = new List(); var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None); diff --git a/src/NzbDrone.Core/Indexers/IndexerBase.cs b/src/NzbDrone.Core/Indexers/IndexerBase.cs index 41942d35b..7d42847cd 100644 --- a/src/NzbDrone.Core/Indexers/IndexerBase.cs +++ b/src/NzbDrone.Core/Indexers/IndexerBase.cs @@ -30,7 +30,7 @@ namespace NzbDrone.Core.Indexers } } - public ProviderDefinition Definition { get; set; } + public virtual ProviderDefinition Definition { get; set; } public abstract DownloadProtocol Protocol { get; } @@ -49,7 +49,6 @@ namespace NzbDrone.Core.Indexers public abstract IEnumerable GetDailyEpisodeSearchUrls(string seriesTitle, int tvRageId, DateTime date); public abstract IEnumerable GetSeasonSearchUrls(string seriesTitle, int tvRageId, int seasonNumber, int offset); - public override string ToString() { return GetType().Name; diff --git a/src/NzbDrone.Core/Indexers/IndexerFactory.cs b/src/NzbDrone.Core/Indexers/IndexerFactory.cs index c7763be91..f9e89ab67 100644 --- a/src/NzbDrone.Core/Indexers/IndexerFactory.cs +++ b/src/NzbDrone.Core/Indexers/IndexerFactory.cs @@ -1,6 +1,8 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using NLog; +using NzbDrone.Common.Composition; using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Indexers @@ -15,8 +17,8 @@ namespace NzbDrone.Core.Indexers private readonly IIndexerRepository _providerRepository; private readonly IEnumerable _providers; - public IndexerFactory(IIndexerRepository providerRepository, IEnumerable providers, Logger logger) - : base(providerRepository, providers, logger) + public IndexerFactory(IIndexerRepository providerRepository, IEnumerable providers, IContainer container, Logger logger) + : base(providerRepository, providers, container, logger) { _providerRepository = providerRepository; _providers = providers; @@ -36,5 +38,10 @@ namespace NzbDrone.Core.Indexers _providerRepository.InsertMany(newProviders.Cast().ToList()); } } + + protected override List Active() + { + return base.Active().Where(c => c.Enable).ToList(); + } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Indexers/Newznab/Newznab.cs b/src/NzbDrone.Core/Indexers/Newznab/Newznab.cs index a4c106846..a9cf5eca2 100644 --- a/src/NzbDrone.Core/Indexers/Newznab/Newznab.cs +++ b/src/NzbDrone.Core/Indexers/Newznab/Newznab.cs @@ -52,6 +52,8 @@ namespace NzbDrone.Core.Indexers.Newznab } } + public override ProviderDefinition Definition { get; set; } + private NewznabSettings GetSettings(string url, List categories) { var settings = new NewznabSettings { Url = url }; diff --git a/src/NzbDrone.Core/Notifications/NotificationFactory.cs b/src/NzbDrone.Core/Notifications/NotificationFactory.cs index dfe5aca10..57163b9e0 100644 --- a/src/NzbDrone.Core/Notifications/NotificationFactory.cs +++ b/src/NzbDrone.Core/Notifications/NotificationFactory.cs @@ -1,20 +1,35 @@ using System.Collections.Generic; +using System.Linq; using NLog; +using NzbDrone.Common.Composition; using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications { public interface INotificationFactory : IProviderFactory { - + List OnGrabEnabled(); + List OnDownloadEnabled(); } public class NotificationFactory : ProviderFactory, INotificationFactory { - public NotificationFactory(INotificationRepository providerRepository, IEnumerable providers, Logger logger) - : base(providerRepository, providers, logger) + private IEnumerable _providers; + + public NotificationFactory(INotificationRepository providerRepository, IEnumerable providers, IContainer container, Logger logger) + : base(providerRepository, providers, container, logger) { + _providers = providers; + } + public List OnGrabEnabled() + { + return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnGrab).ToList(); + } + + public List OnDownloadEnabled() + { + return GetAvailableProviders().Where(n => ((NotificationDefinition)n.Definition).OnDownload).ToList(); } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Notifications/NotificationService.cs b/src/NzbDrone.Core/Notifications/NotificationService.cs new file mode 100644 index 000000000..f0b24a264 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/NotificationService.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using NLog; +using NzbDrone.Common.Composition; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Download; +using NzbDrone.Core.MediaFiles.Events; +using NzbDrone.Core.Messaging.Events; +using NzbDrone.Core.ThingiProvider; +using NzbDrone.Core.Tv; +using Omu.ValueInjecter; + +namespace NzbDrone.Core.Notifications +{ + public class NotificationService + : IHandle, + IHandle, + IHandle + { + private readonly INotificationFactory _notificationFactory; + private readonly Logger _logger; + + public NotificationService(INotificationFactory notificationFactory, Logger logger) + { + _notificationFactory = notificationFactory; + _logger = logger; + } + + private string GetMessage(Series series, List episodes, QualityModel quality) + { + if (series.SeriesType == SeriesTypes.Daily) + { + var episode = episodes.First(); + + return String.Format("{0} - {1} - {2} [{3}]", + series.Title, + episode.AirDate, + episode.Title, + quality); + } + + var episodeNumbers = String.Concat(episodes.Select(e => e.EpisodeNumber) + .Select(i => String.Format("x{0:00}", i))); + + var episodeTitles = String.Join(" + ", episodes.Select(e => e.Title)); + + return String.Format("{0} - {1}{2} - {3} {4}", + series.Title, + episodes.First().SeasonNumber, + episodeNumbers, + episodeTitles, + quality); + } + + public void Handle(EpisodeGrabbedEvent message) + { + var messageBody = GetMessage(message.Episode.Series, message.Episode.Episodes, message.Episode.ParsedEpisodeInfo.Quality); + + foreach (var notification in _notificationFactory.OnGrabEnabled()) + { + try + { + notification.OnGrab(messageBody); + } + + catch (Exception ex) + { + _logger.ErrorException("Unable to send OnGrab notification to: " + notification.Definition.Name, ex); + } + } + } + + public void Handle(EpisodeDownloadedEvent message) + { + var messageBody = GetMessage(message.Episode.Series, message.Episode.Episodes, message.Episode.ParsedEpisodeInfo.Quality); + + foreach (var notification in _notificationFactory.OnDownloadEnabled()) + { + try + { + notification.OnDownload(messageBody, message.Episode.Series); + } + + catch (Exception ex) + { + _logger.WarnException("Unable to send OnDownload notification to: " + notification.Definition.Name, ex); + } + } + } + + public void Handle(SeriesRenamedEvent message) + { + foreach (var notification in _notificationFactory.OnDownloadEnabled()) + { + try + { + notification.AfterRename(message.Series); + } + + catch (Exception ex) + { + _logger.WarnException("Unable to send AfterRename notification to: " + notification.Definition.Name, ex); + } + } + } + } +} diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 186c9acb8..aefe434dc 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -272,6 +272,7 @@ + diff --git a/src/NzbDrone.Core/ThingiProvider/ProviderFactory.cs b/src/NzbDrone.Core/ThingiProvider/ProviderFactory.cs index 71b0683ca..5dc21b519 100644 --- a/src/NzbDrone.Core/ThingiProvider/ProviderFactory.cs +++ b/src/NzbDrone.Core/ThingiProvider/ProviderFactory.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using NLog; +using NzbDrone.Common.Composition; using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Messaging.Events; @@ -12,13 +13,18 @@ namespace NzbDrone.Core.ThingiProvider where TProvider : IProvider { private readonly IProviderRepository _providerRepository; + private readonly IContainer _container; private readonly Logger _logger; private readonly List _providers; - protected ProviderFactory(IProviderRepository providerRepository, IEnumerable providers, Logger logger) + protected ProviderFactory(IProviderRepository providerRepository, + IEnumerable providers, + IContainer container, + Logger logger) { _providerRepository = providerRepository; + _container = container; _providers = providers.ToList(); _logger = logger; } @@ -40,8 +46,7 @@ namespace NzbDrone.Core.ThingiProvider public List GetAvailableProviders() { - return All().Where(c => c.Settings.Validate().IsValid) - .Select(GetInstance).ToList(); + return Active().Select(GetInstance).ToList(); } public TProviderDefinition Get(int id) @@ -67,7 +72,9 @@ namespace NzbDrone.Core.ThingiProvider private TProvider GetInstance(TProviderDefinition definition) { var type = GetImplementation(definition); - var instance = (TProvider)Activator.CreateInstance(type); + + //TODO: This doesn't work for things that have non-parameterless constructors + var instance = (TProvider)_container.Resolve(type); instance.Definition = definition; return instance; } @@ -90,6 +97,11 @@ namespace NzbDrone.Core.ThingiProvider { } + protected virtual List Active() + { + return All().Where(c => c.Settings.Validate().IsValid).ToList(); + } + private void RemoveMissingImplementations() { var storedProvider = _providerRepository.All(); diff --git a/src/UI/Settings/Notifications/ItemView.js b/src/UI/Settings/Notifications/ItemView.js index 532f294e8..5cbaa4100 100644 --- a/src/UI/Settings/Notifications/ItemView.js +++ b/src/UI/Settings/Notifications/ItemView.js @@ -3,7 +3,7 @@ define([ 'AppLayout', 'marionette', - 'Settings/Notifications/EditView', + 'Settings/Notifications/NotificationEditView', 'Settings/Notifications/DeleteView' ], function (AppLayout, Marionette, EditView, DeleteView) {