From dc63693bf645d968fb5b3af6e8ffce7a5c0d5e39 Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Wed, 15 Mar 2017 08:42:48 +0000 Subject: [PATCH] Remoddeled the notificaiton settings to make it easier to add more. This is some techinical changes that no one except me will ever notice :( --- .../Notification/BaseNotification.cs | 77 +++++----- .../Notification/DiscordNotification.cs | 145 ++++++++---------- .../Notification/EmailMessageNotification.cs | 116 ++++++++------ .../Notification/NotificationMessage.cs | 40 +++++ .../Notification/PushbulletNotification.cs | 125 +++++++-------- .../Notification/PushoverNotification.cs | 111 ++++++-------- .../Notification/SlackNotification.cs | 137 ++++++++--------- Ombi.Services/Ombi.Services.csproj | 1 + Ombi.UI/Modules/UserLoginModule.cs | 26 ++-- Ombi.UI/Modules/UserManagementModule.cs | 40 ++--- Ombi.Updater/Ombi.Updater.csproj | 2 +- 11 files changed, 408 insertions(+), 412 deletions(-) create mode 100644 Ombi.Services/Notification/NotificationMessage.cs diff --git a/Ombi.Services/Notification/BaseNotification.cs b/Ombi.Services/Notification/BaseNotification.cs index 7d4ad8944..4131522ea 100644 --- a/Ombi.Services/Notification/BaseNotification.cs +++ b/Ombi.Services/Notification/BaseNotification.cs @@ -35,7 +35,7 @@ using Ombi.Services.Interfaces; namespace Ombi.Services.Notification { - public abstract class BaseNotification : INotification where T : Settings, new() where U : new() + public abstract class BaseNotification : INotification where T : Settings, new() { protected BaseNotification(ISettingsService settings) { @@ -63,34 +63,41 @@ namespace Ombi.Services.Notification return; } - switch (model.NotificationType) + try { - 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(); + switch (model.NotificationType) + { + case NotificationType.NewRequest: + await NewRequest(model, notificationSettings); + break; + case NotificationType.Issue: + await Issue(model, notificationSettings); + break; + case NotificationType.RequestAvailable: + await AvailableRequest(model, notificationSettings); + break; + case NotificationType.RequestApproved: + await RequestApproved(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(); + case NotificationType.Test: + await Test(model, notificationSettings); + break; + case NotificationType.RequestDeclined: + await RequestDeclined(model, notificationSettings); + break; + case NotificationType.ItemAddedToFaultQueue: + await AddedToRequestQueue(model, notificationSettings); + break; + default: + throw new ArgumentOutOfRangeException(); + } + } + catch (NotImplementedException) + { + // Do nothing, it's not implimented meaning it might not be ready or even used } } @@ -103,14 +110,14 @@ namespace Ombi.Services.Notification 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); + protected abstract Task NewRequest(NotificationModel model, T settings); + protected abstract Task Issue(NotificationModel model, T settings); + protected abstract Task AddedToRequestQueue(NotificationModel model, T settings); + protected abstract Task RequestDeclined(NotificationModel model, T settings); + protected abstract Task RequestApproved(NotificationModel model, T settings); + protected abstract Task AvailableRequest(NotificationModel model, T settings); + protected abstract Task Send(NotificationMessage model, T settings); + protected abstract Task Test(NotificationModel model, T settings); } } \ No newline at end of file diff --git a/Ombi.Services/Notification/DiscordNotification.cs b/Ombi.Services/Notification/DiscordNotification.cs index 4cfdfdbb5..2997e57eb 100644 --- a/Ombi.Services/Notification/DiscordNotification.cs +++ b/Ombi.Services/Notification/DiscordNotification.cs @@ -29,110 +29,101 @@ using System; using System.Threading.Tasks; using NLog; using Ombi.Api.Interfaces; -using Ombi.Api.Models.Notifications; using Ombi.Core; -using Ombi.Core.Models; using Ombi.Core.SettingModels; -using Ombi.Services.Interfaces; namespace Ombi.Services.Notification { - public class DiscordNotification : INotification + public class DiscordNotification : BaseNotification { - public DiscordNotification(IDiscordApi api, ISettingsService sn) + public DiscordNotification(IDiscordApi api, ISettingsService sn) : base(sn) { Api = api; - Settings = sn; } - public string NotificationName => "DiscordNotification"; + public override string NotificationName => "DiscordNotification"; private IDiscordApi Api { get; } - private ISettingsService Settings { get; } - private static Logger Log = LogManager.GetCurrentClassLogger(); + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); - - public async Task NotifyAsync(NotificationModel model) + protected override bool ValidateConfiguration(DiscordNotificationSettings settings) { - var settings = Settings.GetSettings(); - - await NotifyAsync(model, settings); - } - - public async Task NotifyAsync(NotificationModel model, Settings settings) - { - if (settings == null) await NotifyAsync(model); - - var pushSettings = (DiscordNotificationSettings)settings; - if (!ValidateConfiguration(pushSettings)) + if (!settings.Enabled) { - Log.Error("Settings for Slack was not correct, we cannot push a notification"); - return; + return false; } - - switch (model.NotificationType) + if (string.IsNullOrEmpty(settings.WebhookUrl)) { - case NotificationType.NewRequest: - await PushNewRequestAsync(model, pushSettings); - break; - case NotificationType.Issue: - await PushIssueAsync(model, pushSettings); - break; - case NotificationType.RequestAvailable: - break; - case NotificationType.RequestApproved: - break; - case NotificationType.AdminNote: - break; - case NotificationType.Test: - await PushTest(pushSettings); - break; - case NotificationType.RequestDeclined: - await PushRequestDeclinedAsync(model, pushSettings); - break; - case NotificationType.ItemAddedToFaultQueue: - await PushFaultQueue(model, pushSettings); - break; - default: - throw new ArgumentOutOfRangeException(); + return false; } + try + { + var a = settings.Token; + var b = settings.WebookId; + } + catch (IndexOutOfRangeException) + { + return false; + } + return true; } - private async Task PushNewRequestAsync(NotificationModel model, DiscordNotificationSettings settings) + protected override async Task NewRequest(NotificationModel model, DiscordNotificationSettings settings) { var message = $"{model.Title} has been requested by user: {model.User}"; - await Push(settings, message); + + var notification = new NotificationMessage + { + Message = message, + }; + await Send(notification, settings); } - private async Task PushRequestDeclinedAsync(NotificationModel model, DiscordNotificationSettings settings) + protected override async Task Issue(NotificationModel model, DiscordNotificationSettings settings) { - var message = $"Hello! Your request for {model.Title} has been declined, Sorry!"; - await Push(settings, message); + var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}"; + var notification = new NotificationMessage + { + Message = message, + }; + await Send(notification, settings); } - private async Task PushIssueAsync(NotificationModel model, DiscordNotificationSettings settings) + protected override async Task AddedToRequestQueue(NotificationModel model, DiscordNotificationSettings settings) { - var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}"; - await Push(settings, message); + var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; + var notification = new NotificationMessage + { + Message = message, + }; + await Send(notification, settings); } - private async Task PushTest(DiscordNotificationSettings settings) + protected override async Task RequestDeclined(NotificationModel model, DiscordNotificationSettings settings) { - var message = $"This is a test from Ombi, if you can see this then we have successfully pushed a notification!"; - await Push(settings, message); + var message = $"Hello! Your request for {model.Title} has been declined, Sorry!"; + var notification = new NotificationMessage + { + Message = message, + }; + await Send(notification, settings); } - private async Task PushFaultQueue(NotificationModel model, DiscordNotificationSettings settings) + protected override Task RequestApproved(NotificationModel model, DiscordNotificationSettings settings) { - var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; - await Push(settings, message); + throw new NotImplementedException(); + } + + protected override Task AvailableRequest(NotificationModel model, DiscordNotificationSettings settings) + { + throw new NotImplementedException(); } - private async Task Push(DiscordNotificationSettings config, string message) + protected override async Task Send(NotificationMessage model, DiscordNotificationSettings settings) { try { - await Api.SendMessageAsync(message, config.WebookId, config.Token, config.Username); + await Api.SendMessageAsync(model.Message, settings.WebookId, settings.Token, settings.Username); } catch (Exception e) { @@ -140,26 +131,14 @@ namespace Ombi.Services.Notification } } - private bool ValidateConfiguration(DiscordNotificationSettings settings) + protected override async Task Test(NotificationModel model, DiscordNotificationSettings settings) { - if (!settings.Enabled) - { - return false; - } - if (string.IsNullOrEmpty(settings.WebhookUrl)) - { - return false; - } - try - { - var a = settings.Token; - var b = settings.WebookId; - } - catch (IndexOutOfRangeException) + var message = $"This is a test from Ombi, if you can see this then we have successfully pushed a notification!"; + var notification = new NotificationMessage { - return false; - } - return true; + Message = message, + }; + await Send(notification, settings); } } } \ No newline at end of file diff --git a/Ombi.Services/Notification/EmailMessageNotification.cs b/Ombi.Services/Notification/EmailMessageNotification.cs index 253f414c4..7f1a80f8d 100644 --- a/Ombi.Services/Notification/EmailMessageNotification.cs +++ b/Ombi.Services/Notification/EmailMessageNotification.cs @@ -37,7 +37,7 @@ using SmtpClient = MailKit.Net.Smtp.SmtpClient; namespace Ombi.Services.Notification { - public class EmailMessageNotification : BaseNotification + public class EmailMessageNotification : BaseNotification { public EmailMessageNotification(ISettingsService settings) : base(settings) { @@ -63,135 +63,150 @@ namespace Ombi.Services.Notification return true; } - protected override async Task EmailNewRequest(NotificationModel model, EmailNotificationSettings settings) + protected override async Task NewRequest(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( $"Ombi: New {model.RequestType.GetString()?.ToLower()} request for {model.Title}!", $"Hello! The user '{model.User}' has requested the {model.RequestType.GetString()?.ToLower()} '{model.Title}'! Please log in to approve this request. Request Date: {model.DateTime.ToString("f")}", model.ImgSrc); - var body = new BodyBuilder { HtmlBody = html, TextBody = $"Hello! The user '{model.User}' has requested the {model.RequestType.GetString()?.ToLower()} '{model.Title}'! Please log in to approve this request. Request Date: {model.DateTime.ToString("f")}" }; + - var message = new MimeMessage + var message = new NotificationMessage { - Body = body.ToMessageBody(), - Subject = $"Ombi: New {model.RequestType.GetString()?.ToLower()} request for {model.Title}!" + Message = html, + Subject = $"Ombi: New {model.RequestType.GetString()?.ToLower()} request for {model.Title}!", + To = model.UserEmail, }; - message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender)); - message.To.Add(new MailboxAddress(settings.RecipientEmail, settings.RecipientEmail)); + message.Other.Add("PlainTextBody", $"Hello! The user '{model.User}' has requested the {model.RequestType.GetString()?.ToLower()} '{model.Title}'! Please log in to approve this request. Request Date: {model.DateTime.ToString("f")}"); await Send(message, settings); } - protected override async Task EmailIssue(NotificationModel model, EmailNotificationSettings settings) + protected override async Task Issue(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( $"Ombi: New issue for {model.Title}!", $"Hello! The user '{model.User}' has reported a new issue {model.Body} for the title {model.Title}!", model.ImgSrc); - var body = new BodyBuilder { HtmlBody = html, TextBody = $"Hello! The user '{model.User}' has reported a new issue {model.Body} for the title {model.Title}!"}; - var message = new MimeMessage + var message = new NotificationMessage { - Body = body.ToMessageBody(), - Subject = $"Ombi: New issue for {model.Title}!" + Message = html, + Subject = $"Ombi: New issue for {model.Title}!", + To = model.UserEmail, }; - message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender)); - message.To.Add(new MailboxAddress(settings.RecipientEmail, settings.RecipientEmail)); + + message.Other.Add("PlainTextBody", $"Hello! The user '{model.User}' has reported a new issue {model.Body} for the title {model.Title}!"); + await Send(message, settings); } - protected override async Task EmailAddedToRequestQueue(NotificationModel model, EmailNotificationSettings settings) + protected override async Task AddedToRequestQueue(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( "Ombi: A request could not be added.", $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying", model.ImgSrc); - var body = new BodyBuilder { HtmlBody = html, TextBody = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying" }; - var message = new MimeMessage + var message = new NotificationMessage { - Body = body.ToMessageBody(), - Subject = $"Ombi: A request could not be added" + Message = html, + Subject = $"Ombi: A request could not be added", + To = model.UserEmail, }; - message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender)); - message.To.Add(new MailboxAddress(settings.RecipientEmail, settings.RecipientEmail)); + + message.Other.Add("PlainTextBody", $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"); await Send(message, settings); } - protected override async Task EmailRequestDeclined(NotificationModel model, EmailNotificationSettings settings) + protected override async Task RequestDeclined(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( "Ombi: Your request has been declined", $"Hello! Your request for {model.Title} has been declined, Sorry!", model.ImgSrc); - var body = new BodyBuilder { HtmlBody = html, TextBody = $"Hello! Your request for {model.Title} has been declined, Sorry!", }; - var message = new MimeMessage + var message = new NotificationMessage { - Body = body.ToMessageBody(), - Subject = $"Ombi: Your request has been declined" + Message = html, + Subject = $"Ombi: Your request has been declined", + To = model.UserEmail, }; - message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender)); - message.To.Add(new MailboxAddress(model.UserEmail, model.UserEmail)); + + message.Other.Add("PlainTextBody", $"Hello! Your request for {model.Title} has been declined, Sorry!"); await Send(message, settings); } - protected override async Task EmailRequestApproved(NotificationModel model, EmailNotificationSettings settings) + protected override async Task RequestApproved(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( "Ombi: Your request has been approved!", $"Hello! Your request for {model.Title} has been approved!", model.ImgSrc); - var body = new BodyBuilder { HtmlBody = html, TextBody = $"Hello! Your request for {model.Title} has been approved!", }; - var message = new MimeMessage + var message = new NotificationMessage { - Body = body.ToMessageBody(), - Subject = $"Ombi: Your request has been approved!" + Message = html, + Subject = $"Ombi: Your request has been approved!", + To = model.UserEmail, }; - message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender)); - message.To.Add(new MailboxAddress(model.UserEmail, model.UserEmail)); + message.Other.Add("PlainTextBody", $"Hello! Your request for {model.Title} has been approved!"); await Send(message, settings); } - protected override async Task EmailAvailableRequest(NotificationModel model, EmailNotificationSettings settings) + protected override async Task AvailableRequest(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( $"Ombi: {model.Title} is now available!", $"Hello! You requested {model.Title} on Ombi! This is now available on Plex! :)", model.ImgSrc); - var body = new BodyBuilder { HtmlBody = html, TextBody = $"Hello! You requested {model.Title} on Ombi! This is now available on Plex! :)" }; - var message = new MimeMessage + + var message = new NotificationMessage { - Body = body.ToMessageBody(), - Subject = $"Ombi: {model.Title} is now available!" + Message = html, + Subject = $"Ombi: {model.Title} is now available!", + To = model.UserEmail, }; - message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender)); - message.To.Add(new MailboxAddress(model.UserEmail, model.UserEmail)); + + message.Other.Add("PlainTextBody", $"Hello! You requested {model.Title} on Ombi! This is now available on Plex! :)"); await Send(message, settings); } - protected override async Task Send(MimeMessage message, EmailNotificationSettings settings) + protected override async Task Send(NotificationMessage model, EmailNotificationSettings settings) { try { + var body = new BodyBuilder + { + HtmlBody = model.Message, + TextBody = model.Other["PlainTextBody"] + }; + + var message = new MimeMessage + { + Body = body.ToMessageBody(), + Subject = model.Subject + }; + message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender)); + message.To.Add(new MailboxAddress(model.To, model.To)); + using (var client = new SmtpClient()) { client.Connect(settings.EmailHost, settings.EmailPort); // Let MailKit figure out the correct SecureSocketOptions. @@ -215,20 +230,21 @@ namespace Ombi.Services.Notification } } - protected override async Task EmailTest(NotificationModel model, EmailNotificationSettings settings) + protected override async Task Test(NotificationModel model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); var html = email.LoadTemplate( "Test Message", "This is just a test! Success!", model.ImgSrc); - var body = new BodyBuilder { HtmlBody = html, }; - var message = new MimeMessage + var message = new NotificationMessage { - Body = body.ToMessageBody() + Message = html, + Subject = $"Ombi: Test", + To = model.UserEmail, }; - message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender)); - message.To.Add(new MailboxAddress(settings.RecipientEmail, settings.RecipientEmail)); + + message.Other.Add("PlainTextBody", "This is just a test! Success!"); await Send(message, settings); } diff --git a/Ombi.Services/Notification/NotificationMessage.cs b/Ombi.Services/Notification/NotificationMessage.cs new file mode 100644 index 000000000..d11d1da63 --- /dev/null +++ b/Ombi.Services/Notification/NotificationMessage.cs @@ -0,0 +1,40 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2017 Jamie Rees +// File: NotificationMessage.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.Collections.Generic; + +namespace Ombi.Services.Notification +{ + public class NotificationMessage + { + public string Subject { get; set; } + public string Message { get; set; } + public string To { get; set; } + + public Dictionary Other { get; set; } = new Dictionary(); + } +} \ No newline at end of file diff --git a/Ombi.Services/Notification/PushbulletNotification.cs b/Ombi.Services/Notification/PushbulletNotification.cs index 53ce6e0ff..336af1771 100644 --- a/Ombi.Services/Notification/PushbulletNotification.cs +++ b/Ombi.Services/Notification/PushbulletNotification.cs @@ -30,67 +30,24 @@ using System.Threading.Tasks; using NLog; using Ombi.Api.Interfaces; using Ombi.Core; -using Ombi.Core.Models; using Ombi.Core.SettingModels; -using Ombi.Services.Interfaces; using Ombi.Store; namespace Ombi.Services.Notification { - public class PushbulletNotification : INotification + public class PushbulletNotification : BaseNotification { - public PushbulletNotification(IPushbulletApi pushbulletApi, ISettingsService settings) + public PushbulletNotification(IPushbulletApi pushbulletApi, ISettingsService settings) : base(settings) { PushbulletApi = pushbulletApi; - SettingsService = settings; } private IPushbulletApi PushbulletApi { get; } private ISettingsService SettingsService { get; } - private static Logger Log = LogManager.GetCurrentClassLogger(); - public string NotificationName => "PushbulletNotification"; - public async Task NotifyAsync(NotificationModel model) - { - 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, pushSettings); - break; - case NotificationType.Issue: - await PushIssueAsync(model, pushSettings); - break; - case NotificationType.RequestAvailable: - break; - case NotificationType.RequestApproved: - break; - case NotificationType.AdminNote: - break; - case NotificationType.Test: - await PushTestAsync(pushSettings); - break; - case NotificationType.RequestDeclined: - break; - case NotificationType.ItemAddedToFaultQueue: - await PushFaultQueue(model, pushSettings); - break; - default: - throw new ArgumentOutOfRangeException(); - } - } - - private bool ValidateConfiguration(PushbulletNotificationSettings settings) + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); + public override string NotificationName => "PushbulletNotification"; + + protected override bool ValidateConfiguration(PushbulletNotificationSettings settings) { if (!settings.Enabled) { @@ -103,44 +60,64 @@ namespace Ombi.Services.Notification return true; } - private PushbulletNotificationSettings GetSettings() - { - return SettingsService.GetSettings(); - } - - private async Task PushNewRequestAsync(NotificationModel model, PushbulletNotificationSettings settings) + protected override async Task NewRequest(NotificationModel model, PushbulletNotificationSettings settings) { var message = $"The {model.RequestType.GetString()?.ToLower()} '{model.Title}' has been requested by user: {model.User}"; var pushTitle = $"Ombi: The {model.RequestType.GetString()?.ToLower()} {model.Title} has been requested!"; - await Push(settings, message, pushTitle); + var notification = new NotificationMessage + { + Message = message, + Subject = pushTitle + }; + await Send(notification, settings); } + - private async Task PushIssueAsync(NotificationModel model, PushbulletNotificationSettings settings) + protected override async Task Issue(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 = $"Ombi: A new issue has been reported for {model.Title}"; - await Push(settings, message, pushTitle); + var notification = new NotificationMessage + { + Message = message, + Subject = pushTitle + }; + await Send(notification, settings); } - private async Task PushTestAsync(PushbulletNotificationSettings settings) + protected override async Task AddedToRequestQueue(NotificationModel model, PushbulletNotificationSettings settings) { - var message = "This is just a test! Success!"; - var pushTitle = "Ombi: Test Message!"; - await Push(settings, message, pushTitle); + + var message = $"Hello!The user '{model.User}' has requested { model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; + var pushTitle = $"Ombi: A request could not be added."; + var notification = new NotificationMessage + { + Message = message, + Subject = pushTitle + }; + await Send(notification, settings); + } + + protected override Task RequestDeclined(NotificationModel model, PushbulletNotificationSettings settings) + { + throw new NotImplementedException(); } - private async Task PushFaultQueue(NotificationModel model, PushbulletNotificationSettings settings) + protected override Task RequestApproved(NotificationModel model, PushbulletNotificationSettings settings) { - var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; - var pushTitle = $"Ombi: The {model.RequestType.GetString()?.ToLower()} {model.Title} has been requested but could not be added!"; - await Push(settings, message, pushTitle); + throw new NotImplementedException(); } - private async Task Push(PushbulletNotificationSettings settings, string message, string title) + protected override Task AvailableRequest(NotificationModel model, PushbulletNotificationSettings settings) + { + throw new NotImplementedException(); + } + + protected override async Task Send(NotificationMessage model, PushbulletNotificationSettings settings) { try { - var result = await PushbulletApi.PushAsync(settings.AccessToken, title, message, settings.DeviceIdentifier); + var result = await PushbulletApi.PushAsync(settings.AccessToken, model.Subject, model.Message, settings.DeviceIdentifier); if (result != null) { Log.Error("Pushbullet api returned a null value, the notification did not get pushed"); @@ -151,5 +128,17 @@ namespace Ombi.Services.Notification Log.Error(e); } } + + protected override async Task Test(NotificationModel model, PushbulletNotificationSettings settings) + { + var message = "This is just a test! Success!"; + var pushTitle = "Ombi: Test Message!"; + var notification = new NotificationMessage + { + Message = message, + Subject = pushTitle + }; + await Send(notification,settings); + } } } \ No newline at end of file diff --git a/Ombi.Services/Notification/PushoverNotification.cs b/Ombi.Services/Notification/PushoverNotification.cs index 3535579b5..4f546c21d 100644 --- a/Ombi.Services/Notification/PushoverNotification.cs +++ b/Ombi.Services/Notification/PushoverNotification.cs @@ -30,67 +30,24 @@ using System.Threading.Tasks; using NLog; using Ombi.Api.Interfaces; using Ombi.Core; -using Ombi.Core.Models; using Ombi.Core.SettingModels; -using Ombi.Services.Interfaces; using Ombi.Store; namespace Ombi.Services.Notification { - public class PushoverNotification : INotification + public class PushoverNotification : BaseNotification { - public PushoverNotification(IPushoverApi pushoverApi, ISettingsService settings) + public PushoverNotification(IPushoverApi pushoverApi, ISettingsService settings) : base(settings) { PushoverApi = pushoverApi; - SettingsService = settings; } private IPushoverApi PushoverApi { get; } - private ISettingsService SettingsService { get; } private static Logger Log = LogManager.GetCurrentClassLogger(); - public string NotificationName => "PushoverNotification"; - public async Task NotifyAsync(NotificationModel model) - { - var configuration = GetSettings(); - await NotifyAsync(model, configuration); - } - - public async Task NotifyAsync(NotificationModel model, Settings settings) - { - if (settings == null) await NotifyAsync(model); + public override string NotificationName => "PushoverNotification"; - var pushSettings = (PushoverNotificationSettings)settings; - if (!ValidateConfiguration(pushSettings)) return; - - switch (model.NotificationType) - { - case NotificationType.NewRequest: - await PushNewRequestAsync(model, pushSettings); - break; - case NotificationType.Issue: - await PushIssueAsync(model, pushSettings); - break; - case NotificationType.RequestAvailable: - break; - case NotificationType.RequestApproved: - break; - case NotificationType.AdminNote: - break; - case NotificationType.Test: - await PushTestAsync(model, pushSettings); - break; - case NotificationType.RequestDeclined: - break; - case NotificationType.ItemAddedToFaultQueue: - await PushFaultQueue(model, pushSettings); - break; - default: - throw new ArgumentOutOfRangeException(); - } - } - - private bool ValidateConfiguration(PushoverNotificationSettings settings) + protected override bool ValidateConfiguration(PushoverNotificationSettings settings) { if (!settings.Enabled) { @@ -103,40 +60,57 @@ namespace Ombi.Services.Notification return true; } - private PushoverNotificationSettings GetSettings() + protected override async Task NewRequest(NotificationModel model, PushoverNotificationSettings settings) { - return SettingsService.GetSettings(); + var message = $"Ombi: The {model.RequestType.GetString()?.ToLower()} '{model.Title}' has been requested by user: {model.User}"; + var notification = new NotificationMessage + { + Message = message, + }; + await Send(notification, settings); } - private async Task PushNewRequestAsync(NotificationModel model, PushoverNotificationSettings settings) + protected override async Task Issue(NotificationModel model, PushoverNotificationSettings settings) { - var message = $"Ombi: The {model.RequestType.GetString()?.ToLower()} '{model.Title}' has been requested by user: {model.User}"; - await Push(settings, message); + var message = $"Ombi: A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}"; + var notification = new NotificationMessage + { + Message = message, + }; + await Send(notification, settings); } - private async Task PushIssueAsync(NotificationModel model, PushoverNotificationSettings settings) + protected override async Task AddedToRequestQueue(NotificationModel model, PushoverNotificationSettings settings) { - var message = $"Ombi: A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}"; - await Push(settings, message); + var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; + + var notification = new NotificationMessage + { + Message = message, + }; + await Send(notification, settings); } - private async Task PushTestAsync(NotificationModel model, PushoverNotificationSettings settings) + protected override Task RequestDeclined(NotificationModel model, PushoverNotificationSettings settings) { - var message = $"Ombi: Test Message!"; - await Push(settings, message); + throw new NotImplementedException(); } - private async Task PushFaultQueue(NotificationModel model, PushoverNotificationSettings settings) + protected override Task RequestApproved(NotificationModel model, PushoverNotificationSettings settings) { - var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; - await Push(settings, message); + throw new NotImplementedException(); + } + + protected override Task AvailableRequest(NotificationModel model, PushoverNotificationSettings settings) + { + throw new NotImplementedException(); } - private async Task Push(PushoverNotificationSettings settings, string message) + protected override async Task Send(NotificationMessage model, PushoverNotificationSettings settings) { try { - var result = await PushoverApi.PushAsync(settings.AccessToken, message, settings.UserToken); + var result = await PushoverApi.PushAsync(settings.AccessToken, model.Message, settings.UserToken); if (result?.status != 1) { Log.Error("Pushover api returned a status that was not 1, the notification did not get pushed"); @@ -147,5 +121,16 @@ namespace Ombi.Services.Notification Log.Error(e); } } + + protected override async Task Test(NotificationModel model, PushoverNotificationSettings settings) + { + + var message = $"Ombi: Test Message!"; + var notification = new NotificationMessage + { + Message = message, + }; + await Send(notification, settings); + } } } \ No newline at end of file diff --git a/Ombi.Services/Notification/SlackNotification.cs b/Ombi.Services/Notification/SlackNotification.cs index 585c6b06a..eb6e28442 100644 --- a/Ombi.Services/Notification/SlackNotification.cs +++ b/Ombi.Services/Notification/SlackNotification.cs @@ -31,101 +31,97 @@ using NLog; using Ombi.Api.Interfaces; using Ombi.Api.Models.Notifications; using Ombi.Core; -using Ombi.Core.Models; using Ombi.Core.SettingModels; -using Ombi.Services.Interfaces; namespace Ombi.Services.Notification { - public class SlackNotification : INotification + public class SlackNotification : BaseNotification { - public SlackNotification(ISlackApi api, ISettingsService sn) + public SlackNotification(ISlackApi api, ISettingsService sn) : base(sn) { Api = api; - Settings = sn; } - public string NotificationName => "SlackNotification"; + public override string NotificationName => "SlackNotification"; private ISlackApi Api { get; } - private ISettingsService Settings { get; } - private static Logger Log = LogManager.GetCurrentClassLogger(); + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); - public async Task NotifyAsync(NotificationModel model) + protected override bool ValidateConfiguration(SlackNotificationSettings settings) { - var settings = Settings.GetSettings(); - - await NotifyAsync(model, settings); - } - - public async Task NotifyAsync(NotificationModel model, Settings settings) - { - if (settings == null) await NotifyAsync(model); - - var pushSettings = (SlackNotificationSettings)settings; - if (!ValidateConfiguration(pushSettings)) + if (!settings.Enabled) { - Log.Error("Settings for Slack was not correct, we cannot push a notification"); - return; + return false; } - - switch (model.NotificationType) + if (string.IsNullOrEmpty(settings.WebhookUrl)) { - case NotificationType.NewRequest: - await PushNewRequestAsync(model, pushSettings); - break; - case NotificationType.Issue: - await PushIssueAsync(model, pushSettings); - break; - case NotificationType.RequestAvailable: - break; - case NotificationType.RequestApproved: - break; - case NotificationType.AdminNote: - break; - case NotificationType.Test: - await PushTest(pushSettings); - break; - case NotificationType.RequestDeclined: - break; - case NotificationType.ItemAddedToFaultQueue: - await PushFaultQueue(model, pushSettings); - break; - default: - throw new ArgumentOutOfRangeException(); + return false; } + try + { + var a = settings.Team; + var b = settings.Service; + var c = settings.Token; + } + catch (IndexOutOfRangeException) + { + return false; + } + return true; } - private async Task PushNewRequestAsync(NotificationModel model, SlackNotificationSettings settings) + protected override async Task NewRequest(NotificationModel model, SlackNotificationSettings settings) { var message = $"{model.Title} has been requested by user: {model.User}"; - await Push(settings, message); + var notification = new NotificationMessage + { + Message = message, + }; + await Send(notification, settings); } - private async Task PushIssueAsync(NotificationModel model, SlackNotificationSettings settings) + protected override async Task Issue(NotificationModel model, SlackNotificationSettings settings) { var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}"; - await Push(settings, message); + var notification = new NotificationMessage + { + Message = message, + }; + await Send(notification, settings); } - private async Task PushTest(SlackNotificationSettings settings) + protected override async Task AddedToRequestQueue(NotificationModel model, SlackNotificationSettings settings) { - var message = $"This is a test from Ombi, if you can see this then we have successfully pushed a notification!"; - await Push(settings, message); + var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; + + var notification = new NotificationMessage + { + Message = message, + }; + await Send(notification, settings); } - private async Task PushFaultQueue(NotificationModel model, SlackNotificationSettings settings) + protected override Task RequestDeclined(NotificationModel model, SlackNotificationSettings settings) { - var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; - await Push(settings, message); + throw new NotImplementedException(); + } + + protected override Task RequestApproved(NotificationModel model, SlackNotificationSettings settings) + { + throw new NotImplementedException(); + } + + protected override Task AvailableRequest(NotificationModel model, SlackNotificationSettings settings) + { + throw new NotImplementedException(); } - private async Task Push(SlackNotificationSettings config, string message) + protected override async Task Send(NotificationMessage model, SlackNotificationSettings config) { try { - var notification = new SlackNotificationBody { username = config.Username, channel = config.Channel ?? string.Empty, text = message }; + var notification = new SlackNotificationBody { username = config.Username, channel = config.Channel ?? string.Empty, text = model.Message }; var result = await Api.PushAsync(config.Team, config.Token, config.Service, notification); if (!result.Equals("ok")) @@ -140,27 +136,14 @@ namespace Ombi.Services.Notification } } - private bool ValidateConfiguration(SlackNotificationSettings settings) + protected override async Task Test(NotificationModel model, SlackNotificationSettings settings) { - if (!settings.Enabled) - { - return false; - } - if (string.IsNullOrEmpty(settings.WebhookUrl)) - { - return false; - } - try - { - var a = settings.Team; - var b = settings.Service; - var c = settings.Token; - } - catch (IndexOutOfRangeException) + var message = $"This is a test from Ombi, if you can see this then we have successfully pushed a notification!"; + var notification = new NotificationMessage { - return false; - } - return true; + Message = message, + }; + await Send(notification, settings); } } } \ No newline at end of file diff --git a/Ombi.Services/Ombi.Services.csproj b/Ombi.Services/Ombi.Services.csproj index b59ee1f33..fa3da8625 100644 --- a/Ombi.Services/Ombi.Services.csproj +++ b/Ombi.Services/Ombi.Services.csproj @@ -147,6 +147,7 @@ + diff --git a/Ombi.UI/Modules/UserLoginModule.cs b/Ombi.UI/Modules/UserLoginModule.cs index cd234ca57..386c1ded2 100644 --- a/Ombi.UI/Modules/UserLoginModule.cs +++ b/Ombi.UI/Modules/UserLoginModule.cs @@ -175,14 +175,6 @@ namespace Ombi.UI.Modules isOwner = true; userId = GetOwnerId(plexSettings.PlexAuthToken, username); } - UsersModel dbUser = await IsDbuser(username); - if (dbUser != null) // in the db? - { - var perms = (Permissions) dbUser.Permissions; - authenticated = true; - isOwner = perms.HasFlag(Permissions.Administrator); - userId = dbUser.UserGuid; - } Log.Debug("Friends list result = {0}", authenticated); } else if (!settings.UserAuthentication) // No auth, let them pass! @@ -207,14 +199,6 @@ namespace Ombi.UI.Modules authenticated = true; isOwner = true; } - UsersModel dbUser = await IsDbuser(username); - if (dbUser != null) // in the db? - { - var perms = (Permissions)dbUser.Permissions; - authenticated = true; - isOwner = perms.HasFlag(Permissions.Administrator); - userId = dbUser.UserGuid; - } Log.Debug("Friends list result = {0}", authenticated); } else if (!settings.UserAuthentication) // No auth, let them pass! @@ -222,6 +206,16 @@ namespace Ombi.UI.Modules authenticated = true; } } + + UsersModel dbUser = await IsDbuser(username); + if (dbUser != null) // in the db? + { + var perms = (Permissions)dbUser.Permissions; + authenticated = true; + isOwner = perms.HasFlag(Permissions.Administrator); + userId = dbUser.UserGuid; + } + if (settings.UsePassword || isOwner || Security.HasPermissions(username, Permissions.Administrator)) { Session[SessionKeys.UserLoginName] = username; diff --git a/Ombi.UI/Modules/UserManagementModule.cs b/Ombi.UI/Modules/UserManagementModule.cs index d7ea20364..e619bc025 100644 --- a/Ombi.UI/Modules/UserManagementModule.cs +++ b/Ombi.UI/Modules/UserManagementModule.cs @@ -78,19 +78,21 @@ namespace Ombi.UI.Modules private async Task LoadUsers() { - + var model = new List(); var plexSettings = await PlexSettings.GetSettingsAsync(); var embySettings = await EmbySettings.GetSettingsAsync(); if (plexSettings.Enable) { - return await LoadPlexUsers(); + model.AddRange(await LoadPlexUsers()); } if (embySettings.Enable) { - return await LoadEmbyUsers(); + model.AddRange(await LoadEmbyUsers()); } - return null; + model.AddRange(await LoadLocalUsers()); + + return Response.AsJson(model); } private async Task CreateUser() @@ -547,19 +549,26 @@ namespace Ombi.UI.Modules return retVal; } - private async Task LoadPlexUsers() + private async Task> LoadLocalUsers() { - var localUsers = await UserMapper.GetUsersAsync(); - var plexDbUsers = await PlexUsersRepository.GetAllAsync(); - var model = new List(); - var userLogins = UserLoginsRepo.GetAll().ToList(); + var localUsers = await UserMapper.GetUsersAsync(); var userLogins = UserLoginsRepo.GetAll().ToList(); + var model = new List(); foreach (var user in localUsers) { var userDb = userLogins.FirstOrDefault(x => x.UserId == user.UserGuid); model.Add(MapLocalUser(user, userDb?.LastLoggedIn ?? DateTime.MinValue)); } + return model; + } + + private async Task> LoadPlexUsers() + { + var plexDbUsers = await PlexUsersRepository.GetAllAsync(); + var model = new List(); + + var userLogins = UserLoginsRepo.GetAll().ToList(); var plexSettings = await PlexSettings.GetSettingsAsync(); if (!string.IsNullOrEmpty(plexSettings.PlexAuthToken)) @@ -595,23 +604,16 @@ namespace Ombi.UI.Modules model.Add(MapPlexAdmin(account, dbUser, userDb?.LastLoggedIn ?? DateTime.MinValue)); } } - return Response.AsJson(model); + return model; } - private async Task LoadEmbyUsers() + private async Task> LoadEmbyUsers() { - var localUsers = await UserMapper.GetUsersAsync(); var embyDbUsers = await EmbyRepository.GetAllAsync(); var model = new List(); var userLogins = UserLoginsRepo.GetAll().ToList(); - foreach (var user in localUsers) - { - var userDb = userLogins.FirstOrDefault(x => x.UserId == user.UserGuid); - model.Add(MapLocalUser(user, userDb?.LastLoggedIn ?? DateTime.MinValue)); - } - var embySettings = await EmbySettings.GetSettingsAsync(); if (!string.IsNullOrEmpty(embySettings.ApiKey)) { @@ -631,7 +633,7 @@ namespace Ombi.UI.Modules } } } - return Response.AsJson(model); + return model; } } } diff --git a/Ombi.Updater/Ombi.Updater.csproj b/Ombi.Updater/Ombi.Updater.csproj index d01a42b60..421bfa786 100644 --- a/Ombi.Updater/Ombi.Updater.csproj +++ b/Ombi.Updater/Ombi.Updater.csproj @@ -59,7 +59,7 @@ - (ROBOCOPY "$(ProjectDir)bin\$(ConfigurationName)\UpdateService" "$(SolutionDir)Ombi.UI\bin\$(ConfigurationName)\UpdateService" "*")^& IF %25ERRORLEVEL%25 GEQ 16 exit 1 + (ROBOCOPY "$(ProjectDir)bin\$(ConfigurationName)\UpdateService" "$(SolutionDir)Ombi.UI\bin\$(ConfigurationName)\UpdateService" "*")^& IF %25ERRORLEVEL%25 LEQ 2 exit 0 exit 0 \ No newline at end of file