From 9742b42fdf634a3400c6a29c0f5e149f55c7688c Mon Sep 17 00:00:00 2001 From: Drewster727 Date: Mon, 28 Mar 2016 13:44:11 -0500 Subject: [PATCH] #96 add the necessary back-end code to produce a test message for all notification types (still have to add the test buttons for pushbullet/pushover) --- .../Notification/EmailMessageNotification.cs | 5 +- .../Notification/PushbulletNotification.cs | 53 +++++++++++++------ .../Notification/PushoverNotification.cs | 52 ++++++++++++------ 3 files changed, 76 insertions(+), 34 deletions(-) diff --git a/PlexRequests.Services/Notification/EmailMessageNotification.cs b/PlexRequests.Services/Notification/EmailMessageNotification.cs index ca0d581cd..4a359fb23 100644 --- a/PlexRequests.Services/Notification/EmailMessageNotification.cs +++ b/PlexRequests.Services/Notification/EmailMessageNotification.cs @@ -60,10 +60,7 @@ namespace PlexRequests.Services.Notification var emailSettings = (EmailNotificationSettings)settings; - if (!ValidateConfiguration(emailSettings)) - { - return; - } + if (!ValidateConfiguration(emailSettings)) return; switch (model.NotificationType) { diff --git a/PlexRequests.Services/Notification/PushbulletNotification.cs b/PlexRequests.Services/Notification/PushbulletNotification.cs index aed796fc1..521855dca 100644 --- a/PlexRequests.Services/Notification/PushbulletNotification.cs +++ b/PlexRequests.Services/Notification/PushbulletNotification.cs @@ -51,18 +51,25 @@ namespace PlexRequests.Services.Notification public string NotificationName => "PushbulletNotification"; public async Task NotifyAsync(NotificationModel model) { - if (!ValidateConfiguration()) - { - return; - } + var configuration = GetSettings(); + await NotifyAsync(model, configuration); + } + + public async Task NotifyAsync(NotificationModel model, Settings settings) + { + if (settings == null) await NotifyAsync(model); + + var pushSettings = (PushbulletNotificationSettings)settings; + + if (!ValidateConfiguration(pushSettings)) return; switch (model.NotificationType) { case NotificationType.NewRequest: - await PushNewRequestAsync(model); + await PushNewRequestAsync(model, pushSettings); break; case NotificationType.Issue: - await PushIssueAsync(model); + await PushIssueAsync(model, pushSettings); break; case NotificationType.RequestAvailable: break; @@ -70,18 +77,21 @@ namespace PlexRequests.Services.Notification break; case NotificationType.AdminNote: break; + case NotificationType.Test: + await PushTestAsync(model, pushSettings); + break; default: throw new ArgumentOutOfRangeException(); } } - private bool ValidateConfiguration() + private bool ValidateConfiguration(PushbulletNotificationSettings settings) { - if (!Settings.Enabled) + if (!settings.Enabled) { return false; } - if (string.IsNullOrEmpty(Settings.AccessToken)) + if (string.IsNullOrEmpty(settings.AccessToken)) { return false; } @@ -93,13 +103,13 @@ namespace PlexRequests.Services.Notification return SettingsService.GetSettings(); } - private async Task PushNewRequestAsync(NotificationModel model) + private async Task PushNewRequestAsync(NotificationModel model, PushbulletNotificationSettings settings) { var message = $"{model.Title} has been requested by user: {model.User}"; var pushTitle = $"Plex Requests: {model.Title} has been requested!"; try { - var result = await PushbulletApi.PushAsync(Settings.AccessToken, pushTitle, message, Settings.DeviceIdentifier); + var result = await PushbulletApi.PushAsync(settings.AccessToken, pushTitle, message, settings.DeviceIdentifier); if (result == null) { Log.Error("Pushbullet api returned a null value, the notification did not get pushed"); @@ -111,13 +121,13 @@ namespace PlexRequests.Services.Notification } } - private async Task PushIssueAsync(NotificationModel model) + private async Task PushIssueAsync(NotificationModel model, PushbulletNotificationSettings settings) { var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}"; var pushTitle = $"Plex Requests: A new issue has been reported for {model.Title}"; try { - var result = await PushbulletApi.PushAsync(Settings.AccessToken, pushTitle, message, Settings.DeviceIdentifier); + var result = await PushbulletApi.PushAsync(settings.AccessToken, pushTitle, message, settings.DeviceIdentifier); if (result != null) { Log.Error("Pushbullet api returned a null value, the notification did not get pushed"); @@ -129,9 +139,22 @@ namespace PlexRequests.Services.Notification } } - public Task NotifyAsync(NotificationModel model, Settings settings) + private async Task PushTestAsync(NotificationModel model, PushbulletNotificationSettings settings) { - throw new NotImplementedException(); + var message = "This is just a test! Success!"; + var pushTitle = "Plex Requests: Test Message!"; + try + { + var result = await PushbulletApi.PushAsync(settings.AccessToken, pushTitle, message, settings.DeviceIdentifier); + if (result != null) + { + Log.Error("Pushbullet api returned a null value, the notification did not get pushed"); + } + } + catch (Exception e) + { + Log.Error(e); + } } } } \ No newline at end of file diff --git a/PlexRequests.Services/Notification/PushoverNotification.cs b/PlexRequests.Services/Notification/PushoverNotification.cs index ef3671014..47854b1d5 100644 --- a/PlexRequests.Services/Notification/PushoverNotification.cs +++ b/PlexRequests.Services/Notification/PushoverNotification.cs @@ -51,18 +51,25 @@ namespace PlexRequests.Services.Notification public string NotificationName => "PushoverNotification"; public async Task NotifyAsync(NotificationModel model) { - if (!ValidateConfiguration()) - { - return; - } + var configuration = GetSettings(); + await NotifyAsync(model, configuration); + } + + public async Task NotifyAsync(NotificationModel model, Settings settings) + { + if (settings == null) await NotifyAsync(model); + + var pushSettings = (PushoverNotificationSettings)settings; + + if (!ValidateConfiguration(pushSettings)) return; switch (model.NotificationType) { case NotificationType.NewRequest: - await PushNewRequestAsync(model); + await PushNewRequestAsync(model, pushSettings); break; case NotificationType.Issue: - await PushIssueAsync(model); + await PushIssueAsync(model, pushSettings); break; case NotificationType.RequestAvailable: break; @@ -70,18 +77,21 @@ namespace PlexRequests.Services.Notification break; case NotificationType.AdminNote: break; + case NotificationType.Test: + await PushTestAsync(model, pushSettings); + break; default: throw new ArgumentOutOfRangeException(); } } - private bool ValidateConfiguration() + private bool ValidateConfiguration(PushoverNotificationSettings settings) { - if (!Settings.Enabled) + if (!settings.Enabled) { return false; } - if (string.IsNullOrEmpty(Settings.AccessToken) || string.IsNullOrEmpty(Settings.UserToken)) + if (string.IsNullOrEmpty(settings.AccessToken) || string.IsNullOrEmpty(settings.UserToken)) { return false; } @@ -93,12 +103,12 @@ namespace PlexRequests.Services.Notification return SettingsService.GetSettings(); } - private async Task PushNewRequestAsync(NotificationModel model) + private async Task PushNewRequestAsync(NotificationModel model, PushoverNotificationSettings settings) { var message = $"Plex Requests: {model.Title} has been requested by user: {model.User}"; try { - var result = await PushoverApi.PushAsync(Settings.AccessToken, message, Settings.UserToken); + var result = await PushoverApi.PushAsync(settings.AccessToken, message, settings.UserToken); if (result?.status != 1) { Log.Error("Pushover api returned a status that was not 1, the notification did not get pushed"); @@ -110,12 +120,12 @@ namespace PlexRequests.Services.Notification } } - private async Task PushIssueAsync(NotificationModel model) + private async Task PushIssueAsync(NotificationModel model, PushoverNotificationSettings settings) { var message = $"Plex Requests: A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}"; try { - var result = await PushoverApi.PushAsync(Settings.AccessToken, message, Settings.UserToken); + var result = await PushoverApi.PushAsync(settings.AccessToken, message, settings.UserToken); if (result?.status != 1) { Log.Error("Pushover api returned a status that was not 1, the notification did not get pushed"); @@ -127,9 +137,21 @@ namespace PlexRequests.Services.Notification } } - public Task NotifyAsync(NotificationModel model, Settings settings) + private async Task PushTestAsync(NotificationModel model, PushoverNotificationSettings settings) { - throw new NotImplementedException(); + var message = $"Plex Requests: Test Message!"; + try + { + var result = await PushoverApi.PushAsync(settings.AccessToken, message, settings.UserToken); + if (result?.status != 1) + { + Log.Error("Pushover api returned a status that was not 1, the notification did not get pushed"); + } + } + catch (Exception e) + { + Log.Error(e); + } } } } \ No newline at end of file