From 83bb1b58e724e2340605b7183d83d6f552e82611 Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Fri, 16 Dec 2016 09:28:48 +0000 Subject: [PATCH] Small refactorings --- .../EmailNotificationSettings.cs | 1 - .../SettingModels/NotificationSettings.cs | 3 ++ .../PushBulletNotificationSettings.cs | 1 - .../PushoverNotificationSettings.cs | 1 - .../SlackNotificationSettings.cs | 1 - PlexRequests.UI/Bootstrapper.cs | 38 ----------------- PlexRequests.UI/Startup.cs | 42 +++++++++++++++++-- 7 files changed, 41 insertions(+), 46 deletions(-) diff --git a/PlexRequests.Core/SettingModels/EmailNotificationSettings.cs b/PlexRequests.Core/SettingModels/EmailNotificationSettings.cs index 138c59a8c..a8069644a 100644 --- a/PlexRequests.Core/SettingModels/EmailNotificationSettings.cs +++ b/PlexRequests.Core/SettingModels/EmailNotificationSettings.cs @@ -33,7 +33,6 @@ namespace PlexRequests.Core.SettingModels public int EmailPort { get; set; } public string EmailSender { get; set; } public string EmailUsername { get; set; } - public bool Enabled { get; set; } public bool Authentication { get; set; } public bool EnableUserEmailNotifications { get; set; } public string RecipientEmail { get; set; } diff --git a/PlexRequests.Core/SettingModels/NotificationSettings.cs b/PlexRequests.Core/SettingModels/NotificationSettings.cs index 1a808244e..58a1128f0 100644 --- a/PlexRequests.Core/SettingModels/NotificationSettings.cs +++ b/PlexRequests.Core/SettingModels/NotificationSettings.cs @@ -45,6 +45,9 @@ namespace PlexRequests.Core.SettingModels }; } + + public bool Enabled { get; set; } + public List Message { get; set; } } diff --git a/PlexRequests.Core/SettingModels/PushBulletNotificationSettings.cs b/PlexRequests.Core/SettingModels/PushBulletNotificationSettings.cs index 7b3b59a82..b65c5f1b5 100644 --- a/PlexRequests.Core/SettingModels/PushBulletNotificationSettings.cs +++ b/PlexRequests.Core/SettingModels/PushBulletNotificationSettings.cs @@ -30,6 +30,5 @@ namespace PlexRequests.Core.SettingModels { public string AccessToken { get; set; } public string DeviceIdentifier { get; set; } - public bool Enabled { get; set; } } } \ No newline at end of file diff --git a/PlexRequests.Core/SettingModels/PushoverNotificationSettings.cs b/PlexRequests.Core/SettingModels/PushoverNotificationSettings.cs index 0c3e0b976..276a5c96c 100644 --- a/PlexRequests.Core/SettingModels/PushoverNotificationSettings.cs +++ b/PlexRequests.Core/SettingModels/PushoverNotificationSettings.cs @@ -29,7 +29,6 @@ namespace PlexRequests.Core.SettingModels public sealed class PushoverNotificationSettings : NotificationSettings { public string AccessToken { get; set; } - public bool Enabled { get; set; } public string UserToken { get; set; } } } \ No newline at end of file diff --git a/PlexRequests.Core/SettingModels/SlackNotificationSettings.cs b/PlexRequests.Core/SettingModels/SlackNotificationSettings.cs index 8e529d618..80a26ab7e 100644 --- a/PlexRequests.Core/SettingModels/SlackNotificationSettings.cs +++ b/PlexRequests.Core/SettingModels/SlackNotificationSettings.cs @@ -6,7 +6,6 @@ namespace PlexRequests.Core.SettingModels { public sealed class SlackNotificationSettings : NotificationSettings { - public bool Enabled { get; set; } public string WebhookUrl { get; set; } public string Channel { get; set; } public string Username { get; set; } diff --git a/PlexRequests.UI/Bootstrapper.cs b/PlexRequests.UI/Bootstrapper.cs index 7dbb5765a..c3c1795e1 100644 --- a/PlexRequests.UI/Bootstrapper.cs +++ b/PlexRequests.UI/Bootstrapper.cs @@ -31,13 +31,11 @@ using System.Net; using Mono.Data.Sqlite; using Nancy; -using Nancy.Authentication.Forms; using Nancy.Bootstrapper; using Nancy.Bootstrappers.Ninject; using Nancy.Conventions; using Nancy.Cryptography; using Nancy.Diagnostics; -using Nancy.Hosting.Self; using Nancy.Session; using PlexRequests.Api.Interfaces; @@ -105,9 +103,6 @@ namespace PlexRequests.UI ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; - - SubscribeAllObservers(container); - } #if DEBUG @@ -142,39 +137,6 @@ namespace PlexRequests.UI protected override DiagnosticsConfiguration DiagnosticsConfiguration => new DiagnosticsConfiguration { Password = @"password" }; - private void SubscribeAllObservers(IKernel container) - { - var notificationService = container.Get(); - - var emailSettingsService = container.Get>(); - var emailSettings = emailSettingsService.GetSettings(); - if (emailSettings.Enabled) - { - notificationService.Subscribe(new EmailMessageNotification(emailSettingsService)); - } - - var pushbulletService = container.Get>(); - var pushbulletSettings = pushbulletService.GetSettings(); - if (pushbulletSettings.Enabled) - { - notificationService.Subscribe(new PushbulletNotification(container.Get(), pushbulletService)); - } - - var pushoverService = container.Get>(); - var pushoverSettings = pushoverService.GetSettings(); - if (pushoverSettings.Enabled) - { - notificationService.Subscribe(new PushoverNotification(container.Get(), pushoverService)); - } - - var slackService = container.Get>(); - var slackSettings = slackService.GetSettings(); - if (slackSettings.Enabled) - { - notificationService.Subscribe(new SlackNotification(container.Get(), slackService)); - } - } - protected override void RequestStartup(IKernel container, IPipelines pipelines, NancyContext context) { //CORS Enable diff --git a/PlexRequests.UI/Startup.cs b/PlexRequests.UI/Startup.cs index e4d4bf107..8f3542a29 100644 --- a/PlexRequests.UI/Startup.cs +++ b/PlexRequests.UI/Startup.cs @@ -28,13 +28,17 @@ using System; using System.Diagnostics; using Ninject; using Ninject.Planning.Bindings.Resolvers; - +using Ninject.Syntax; using NLog; using Owin; +using PlexRequests.Api.Interfaces; using PlexRequests.Core; using PlexRequests.Core.Migration; +using PlexRequests.Core.SettingModels; +using PlexRequests.Services.Interfaces; using PlexRequests.Services.Jobs; +using PlexRequests.Services.Notification; using PlexRequests.Store.Models; using PlexRequests.Store.Repository; using PlexRequests.UI.Helpers; @@ -89,10 +93,8 @@ namespace PlexRequests.UI jobSettings.Update(scheduledJobse); } scheduler.StartScheduler(); - - - + SubscribeAllObservers(kernel); } catch (Exception exception) @@ -101,5 +103,37 @@ namespace PlexRequests.UI throw; } } + + private void SubscribeAllObservers(IResolutionRoot container) + { + var notificationService = container.Get(); + + var emailSettingsService = container.Get>(); + var emailSettings = emailSettingsService.GetSettings(); + SubScribeOvserver(emailSettings, notificationService ,new EmailMessageNotification(emailSettingsService)); + + + var pushbulletService = container.Get>(); + var pushbulletSettings = pushbulletService.GetSettings(); + SubScribeOvserver(pushbulletSettings, notificationService, new PushbulletNotification(container.Get(), pushbulletService)); + + + var pushoverService = container.Get>(); + var pushoverSettings = pushoverService.GetSettings(); + SubScribeOvserver(pushoverSettings, notificationService, new PushoverNotification(container.Get(), pushoverService)); + + var slackService = container.Get>(); + var slackSettings = slackService.GetSettings(); + SubScribeOvserver(slackSettings, notificationService, new SlackNotification(container.Get(), slackService)); + } + + private void SubScribeOvserver(T settings, INotificationService notificationService, INotification notification) + where T : NotificationSettings + { + if (settings.Enabled) + { + notificationService.Subscribe(notification); + } + } } } \ No newline at end of file