From 13c0b4ed176cd28d86b80039ce17c78224465afe Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Tue, 14 Mar 2017 08:38:37 +0000 Subject: [PATCH] A fix to the about page and also started to rework the notification backend slightly to easily add more notifications --- Ombi.Core/StatusChecker/StatusChecker.cs | 2 +- .../Notification/BaseNotification.cs | 116 ++++++++++++++++++ .../Notification/EmailMessageNotification.cs | 83 ++----------- Ombi.Services/Ombi.Services.csproj | 1 + Ombi.UI/Modules/Admin/AboutModule.cs | 2 +- Ombi.UI/Modules/Admin/SystemStatusModule.cs | 2 +- 6 files changed, 132 insertions(+), 74 deletions(-) create mode 100644 Ombi.Services/Notification/BaseNotification.cs diff --git a/Ombi.Core/StatusChecker/StatusChecker.cs b/Ombi.Core/StatusChecker/StatusChecker.cs index ed86e4e54..f4987a070 100644 --- a/Ombi.Core/StatusChecker/StatusChecker.cs +++ b/Ombi.Core/StatusChecker/StatusChecker.cs @@ -162,7 +162,7 @@ namespace Ombi.Core.StatusChecker } var downloadLink = $"{AppveyorApiUrl}/buildjobs/{jobId}/artifacts/{artifactResult.fileName}"; - var branchDisplay = EnumHelper.GetDisplayValue(branch); + var branchDisplay = EnumHelper.GetBranchValue(branch).DisplayName; var fileversion = AssemblyHelper.GetFileVersion(); var model = new StatusModel diff --git a/Ombi.Services/Notification/BaseNotification.cs b/Ombi.Services/Notification/BaseNotification.cs new file mode 100644 index 000000000..7d4ad8944 --- /dev/null +++ b/Ombi.Services/Notification/BaseNotification.cs @@ -0,0 +1,116 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2017 Jamie Rees +// File: BaseNotification.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System; +using System.Threading.Tasks; +using NLog; +using Ombi.Core; +using Ombi.Core.Models; +using Ombi.Core.SettingModels; +using Ombi.Services.Interfaces; + +namespace Ombi.Services.Notification +{ + public abstract class BaseNotification : INotification where T : Settings, new() where U : new() + { + protected BaseNotification(ISettingsService settings) + { + Settings = settings; + } + + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); + protected ISettingsService Settings { get; } + public abstract string NotificationName { get; } + + public async Task NotifyAsync(NotificationModel model) + { + var configuration = GetConfiguration(); + await NotifyAsync(model, configuration); + } + + public async Task NotifyAsync(NotificationModel model, Settings settings) + { + if (settings == null) await NotifyAsync(model); + + var notificationSettings = (T)settings; + + if (!ValidateConfiguration(notificationSettings)) + { + return; + } + + switch (model.NotificationType) + { + case NotificationType.NewRequest: + await EmailNewRequest(model, notificationSettings); + break; + case NotificationType.Issue: + await EmailIssue(model, notificationSettings); + break; + case NotificationType.RequestAvailable: + await EmailAvailableRequest(model, notificationSettings); + break; + case NotificationType.RequestApproved: + await EmailRequestApproved(model, notificationSettings); + break; + case NotificationType.AdminNote: + throw new NotImplementedException(); + + case NotificationType.Test: + await EmailTest(model, notificationSettings); + break; + case NotificationType.RequestDeclined: + await EmailRequestDeclined(model, notificationSettings); + break; + case NotificationType.ItemAddedToFaultQueue: + await EmailAddedToRequestQueue(model, notificationSettings); + break; + default: + throw new ArgumentOutOfRangeException(); + } + + } + + private T GetConfiguration() + { + var settings = Settings.GetSettings(); + return settings; + } + + + protected abstract bool ValidateConfiguration(T settings); + protected abstract Task EmailNewRequest(NotificationModel model, T settings); + protected abstract Task EmailIssue(NotificationModel model, T settings); + protected abstract Task EmailAddedToRequestQueue(NotificationModel model, T settings); + protected abstract Task EmailRequestDeclined(NotificationModel model, T settings); + protected abstract Task EmailRequestApproved(NotificationModel model, T settings); + protected abstract Task EmailAvailableRequest(NotificationModel model, T settings); + protected abstract Task Send(U message, T settings); + protected abstract Task EmailTest(NotificationModel model, T settings); + + } +} \ No newline at end of file diff --git a/Ombi.Services/Notification/EmailMessageNotification.cs b/Ombi.Services/Notification/EmailMessageNotification.cs index 729726eef..253f414c4 100644 --- a/Ombi.Services/Notification/EmailMessageNotification.cs +++ b/Ombi.Services/Notification/EmailMessageNotification.cs @@ -30,82 +30,23 @@ using System.Threading.Tasks; using MimeKit; using NLog; using Ombi.Core; -using Ombi.Core.Models; using Ombi.Core.Notification.Templates; using Ombi.Core.SettingModels; -using Ombi.Services.Interfaces; using Ombi.Store; using SmtpClient = MailKit.Net.Smtp.SmtpClient; namespace Ombi.Services.Notification { - public class EmailMessageNotification : INotification + public class EmailMessageNotification : BaseNotification { - public EmailMessageNotification(ISettingsService settings) + public EmailMessageNotification(ISettingsService settings) : base(settings) { - EmailNotificationSettings = settings; } private static readonly Logger Log = LogManager.GetCurrentClassLogger(); - private ISettingsService EmailNotificationSettings { get; } - public string NotificationName => "EmailMessageNotification"; + public override string NotificationName => "EmailMessageNotification"; - public async Task NotifyAsync(NotificationModel model) - { - var configuration = GetConfiguration(); - await NotifyAsync(model, configuration); - } - - public async Task NotifyAsync(NotificationModel model, Settings settings) - { - if (settings == null) await NotifyAsync(model); - - var emailSettings = (EmailNotificationSettings)settings; - - if (!ValidateConfiguration(emailSettings)) - { - return; - } - - switch (model.NotificationType) - { - case NotificationType.NewRequest: - await EmailNewRequest(model, emailSettings); - break; - case NotificationType.Issue: - await EmailIssue(model, emailSettings); - break; - case NotificationType.RequestAvailable: - await EmailAvailableRequest(model, emailSettings); - break; - case NotificationType.RequestApproved: - await EmailRequestApproved(model, emailSettings); - break; - case NotificationType.AdminNote: - throw new NotImplementedException(); - - case NotificationType.Test: - await EmailTest(model, emailSettings); - break; - case NotificationType.RequestDeclined: - await EmailRequestDeclined(model, emailSettings); - break; - case NotificationType.ItemAddedToFaultQueue: - await EmailAddedToRequestQueue(model, emailSettings); - break; - default: - throw new ArgumentOutOfRangeException(); - } - - } - - private EmailNotificationSettings GetConfiguration() - { - var settings = EmailNotificationSettings.GetSettings(); - return settings; - } - - private bool ValidateConfiguration(EmailNotificationSettings settings) + protected override bool ValidateConfiguration(EmailNotificationSettings settings) { if (settings.Authentication) { @@ -122,7 +63,7 @@ namespace Ombi.Services.Notification return true; } - private async Task EmailNewRequest(NotificationModel model, EmailNotificationSettings settings) + protected override async Task EmailNewRequest(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( @@ -143,7 +84,7 @@ namespace Ombi.Services.Notification await Send(message, settings); } - private async Task EmailIssue(NotificationModel model, EmailNotificationSettings settings) + protected override async Task EmailIssue(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( @@ -164,7 +105,7 @@ namespace Ombi.Services.Notification await Send(message, settings); } - private async Task EmailAddedToRequestQueue(NotificationModel model, EmailNotificationSettings settings) + protected override async Task EmailAddedToRequestQueue(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( @@ -185,7 +126,7 @@ namespace Ombi.Services.Notification await Send(message, settings); } - private async Task EmailRequestDeclined(NotificationModel model, EmailNotificationSettings settings) + protected override async Task EmailRequestDeclined(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( @@ -206,7 +147,7 @@ namespace Ombi.Services.Notification await Send(message, settings); } - private async Task EmailRequestApproved(NotificationModel model, EmailNotificationSettings settings) + protected override async Task EmailRequestApproved(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( @@ -227,7 +168,7 @@ namespace Ombi.Services.Notification await Send(message, settings); } - private async Task EmailAvailableRequest(NotificationModel model, EmailNotificationSettings settings) + protected override async Task EmailAvailableRequest(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( @@ -247,7 +188,7 @@ namespace Ombi.Services.Notification await Send(message, settings); } - private async Task Send(MimeMessage message, EmailNotificationSettings settings) + protected override async Task Send(MimeMessage message, EmailNotificationSettings settings) { try { @@ -274,7 +215,7 @@ namespace Ombi.Services.Notification } } - private async Task EmailTest(NotificationModel model, EmailNotificationSettings settings) + protected override async Task EmailTest(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( diff --git a/Ombi.Services/Ombi.Services.csproj b/Ombi.Services/Ombi.Services.csproj index 923ea6155..b59ee1f33 100644 --- a/Ombi.Services/Ombi.Services.csproj +++ b/Ombi.Services/Ombi.Services.csproj @@ -144,6 +144,7 @@ + diff --git a/Ombi.UI/Modules/Admin/AboutModule.cs b/Ombi.UI/Modules/Admin/AboutModule.cs index 120f01246..3172a29f1 100644 --- a/Ombi.UI/Modules/Admin/AboutModule.cs +++ b/Ombi.UI/Modules/Admin/AboutModule.cs @@ -112,7 +112,7 @@ namespace Ombi.UI.Modules.Admin vm.DbLocation = SqlConfig.CurrentPath; vm.ApplicationVersion = AssemblyHelper.GetFileVersion(); - vm.Branch = EnumHelper.GetDisplayValue(systemSettings.Branch); + vm.Branch = EnumHelper.GetBranchValue(systemSettings.Branch).DisplayName; vm.LogLevel = LogManager.Configuration.LoggingRules.FirstOrDefault(x => x.NameMatches("database"))?.Levels?.FirstOrDefault()?.Name ?? "Unknown"; return vm; diff --git a/Ombi.UI/Modules/Admin/SystemStatusModule.cs b/Ombi.UI/Modules/Admin/SystemStatusModule.cs index 56c6aa5d3..46b4fc720 100644 --- a/Ombi.UI/Modules/Admin/SystemStatusModule.cs +++ b/Ombi.UI/Modules/Admin/SystemStatusModule.cs @@ -142,7 +142,7 @@ namespace Ombi.UI.Modules.Admin var settings = this.Bind(); - Analytics.TrackEventAsync(Category.Admin, Action.Update, $"Updated Branch {EnumHelper.GetDisplayValue(settings.Branch)}", Username, CookieHelper.GetAnalyticClientId(Cookies)); + Analytics.TrackEventAsync(Category.Admin, Action.Update, $"Updated Branch {EnumHelper.GetBranchValue(settings.Branch).DisplayName}", Username, CookieHelper.GetAnalyticClientId(Cookies)); await SystemSettings.SaveSettingsAsync(settings); // Clear the cache