diff --git a/Ombi.Api/EmbyApi.cs b/Ombi.Api/EmbyApi.cs index 83028153a..1f066f133 100644 --- a/Ombi.Api/EmbyApi.cs +++ b/Ombi.Api/EmbyApi.cs @@ -185,7 +185,6 @@ namespace Ombi.Api throw new ArgumentOutOfRangeException(nameof(type), type, null); } - var info = new EmbyInformation(); switch (type) { diff --git a/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs b/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs index 90e32dcd0..7bda0d5af 100644 --- a/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs +++ b/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs @@ -32,6 +32,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; +using Newtonsoft.Json; using NLog; using Ombi.Api; using Ombi.Api.Interfaces; @@ -77,7 +78,7 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter private static readonly Logger Log = LogManager.GetCurrentClassLogger(); - public string GetNewsletterHtml(bool test) + public Newsletter GetNewsletter(bool test) { try { @@ -86,7 +87,7 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter catch (Exception e) { Log.Error(e); - return string.Empty; + return null; } } @@ -97,11 +98,12 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter public List EpisodeInformation { get; set; } } - private string GetHtml(bool test) + private Newsletter GetHtml(bool test) { var sb = new StringBuilder(); - var embySettings = EmbySettings.GetSettings(); + var newsletter = new Newsletter(); + var embySettings = EmbySettings.GetSettings(); var embyContent = Content.GetAll().ToList(); var series = embyContent.Where(x => x.Type == EmbyMediaType.Series).ToList(); @@ -115,7 +117,6 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter var filteredMovies = movie.Where(m => recentlyAdded.All(x => x.ProviderId != m.ProviderId)).ToList(); var filteredEp = episodes.Where(m => recentlyAdded.All(x => x.ProviderId != m.ProviderId)).ToList(); - var info = new List(); foreach (var m in filteredMovies) { @@ -129,32 +130,47 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter }); } GenerateMovieHtml(info, sb); + newsletter.MovieCount = info.Count; info.Clear(); foreach (var t in series) { var i = Api.GetInformation(t.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Series, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); - var ep = filteredEp.Where(x => x.ParentId == t.EmbyId); - - if (ep.Any()) + var ep = filteredEp.Where(x => x.ParentId == t.EmbyId).ToList(); + var item = new EmbyRecentlyAddedModel + { + EmbyContent = t, + EmbyInformation = i, + }; + if (ep.Any() && embySettings.EnableEpisodeSearching) { - var episodeList = new List(); - foreach (var embyEpisodese in ep) + try { - var epInfo = Api.GetInformation(embyEpisodese.EmbyId, Ombi.Api.Models.Emby.EmbyMediaType.Episode, - embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); - episodeList.Add(epInfo.EpisodeInformation); + var episodeList = new List(); + foreach (var embyEpisodese in ep) + { + var epInfo = Api.GetInformation(embyEpisodese.EmbyId, + Ombi.Api.Models.Emby.EmbyMediaType.Episode, + embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); + episodeList.Add(epInfo.EpisodeInformation); + Thread.Sleep(200); // Let's not try and overload the server + } + item.EpisodeInformation = episodeList; } - info.Add(new EmbyRecentlyAddedModel + catch (JsonReaderException) { - EmbyContent = t, - EmbyInformation = i, - EpisodeInformation = episodeList - }); + Log.Error( + "Failed getting episode information, we may have overloaded Emby's api... Waiting and we will skip this one and go to the next"); + Thread.Sleep(500); + } } + + info.Add(item); } GenerateTvHtml(info, sb); + newsletter.TvCount = info.Count; + var template = new RecentlyAddedTemplate(); var html = template.LoadTemplate(sb.ToString()); @@ -180,9 +196,12 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter } } + var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray()); Log.Debug(escapedHtml); - return escapedHtml; + newsletter.Html = escapedHtml; + return newsletter; + } private void GenerateMovieHtml(IEnumerable recentlyAddedMovies, StringBuilder sb) @@ -272,11 +291,12 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter { var seriesItem = t.EmbyInformation.SeriesInformation; var relatedEpisodes = t.EpisodeInformation; - + var endLoop = false; try { var info = TvApi.ShowLookupByTheTvDbId(int.Parse(seriesItem.ProviderIds.Tvdb)); + if(info == null)continue; var banner = info.image?.original; if (!string.IsNullOrEmpty(banner)) @@ -295,30 +315,33 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter Header(sb, 3, title); EndTag(sb, "a"); - var results = relatedEpisodes.GroupBy(p => p.ParentIndexNumber, - (key, g) => new - { - ParentIndexNumber = key, - IndexNumber = g.ToList() - } - ); - // Group the episodes - foreach (var embyEpisodeInformation in results.OrderBy(x => x.ParentIndexNumber)) + if (relatedEpisodes != null) { - var epSb = new StringBuilder(); - for (var i = 0; i < embyEpisodeInformation.IndexNumber.Count; i++) - { - var ep = embyEpisodeInformation.IndexNumber[i]; - if (i < embyEpisodeInformation.IndexNumber.Count) + var results = relatedEpisodes.GroupBy(p => p.ParentIndexNumber, + (key, g) => new { - epSb.Append($"{ep.IndexNumber},"); + ParentIndexNumber = key, + IndexNumber = g.ToList() } - else + ); + // Group the episodes + foreach (var embyEpisodeInformation in results.OrderBy(x => x.ParentIndexNumber)) + { + var epSb = new StringBuilder(); + for (var i = 0; i < embyEpisodeInformation.IndexNumber.Count; i++) { - epSb.Append(ep); + var ep = embyEpisodeInformation.IndexNumber[i]; + if (i < embyEpisodeInformation.IndexNumber.Count) + { + epSb.Append($"{ep.IndexNumber},"); + } + else + { + epSb.Append(ep); + } } + AddParagraph(sb, $"Season: {embyEpisodeInformation.ParentIndexNumber}, Episode: {epSb}"); } - AddParagraph(sb, $"Season: {embyEpisodeInformation.ParentIndexNumber}, Episode: {epSb}"); } if (info.genres.Any()) @@ -327,6 +350,7 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter } AddParagraph(sb, string.IsNullOrEmpty(seriesItem.Overview) ? info.summary : seriesItem.Overview); + endLoop = true; } catch (Exception e) { @@ -334,7 +358,8 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter } finally { - EndLoopHtml(sb); + if(endLoop) + EndLoopHtml(sb); } } sb.Append("

"); diff --git a/Ombi.Services/Jobs/RecentlyAddedNewsletter/IEmbyAddedNewsletter.cs b/Ombi.Services/Jobs/RecentlyAddedNewsletter/IEmbyAddedNewsletter.cs index bef09ce6e..84ba9c561 100644 --- a/Ombi.Services/Jobs/RecentlyAddedNewsletter/IEmbyAddedNewsletter.cs +++ b/Ombi.Services/Jobs/RecentlyAddedNewsletter/IEmbyAddedNewsletter.cs @@ -2,6 +2,6 @@ { public interface IEmbyAddedNewsletter { - string GetNewsletterHtml(bool test); + Newsletter GetNewsletter(bool test); } } \ No newline at end of file diff --git a/Ombi.Services/Jobs/RecentlyAddedNewsletter/IPlexNewsletter.cs b/Ombi.Services/Jobs/RecentlyAddedNewsletter/IPlexNewsletter.cs index f22ccf519..e81632290 100644 --- a/Ombi.Services/Jobs/RecentlyAddedNewsletter/IPlexNewsletter.cs +++ b/Ombi.Services/Jobs/RecentlyAddedNewsletter/IPlexNewsletter.cs @@ -2,6 +2,6 @@ { public interface IPlexNewsletter { - string GetNewsletterHtml(bool test); + Newsletter GetNewsletter(bool test); } } \ No newline at end of file diff --git a/Ombi.Services/Jobs/RecentlyAddedNewsletter/Newsletter.cs b/Ombi.Services/Jobs/RecentlyAddedNewsletter/Newsletter.cs new file mode 100644 index 000000000..3d7c8b960 --- /dev/null +++ b/Ombi.Services/Jobs/RecentlyAddedNewsletter/Newsletter.cs @@ -0,0 +1,37 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2017 Jamie Rees +// File: Newsletter.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 +namespace Ombi.Services.Jobs.RecentlyAddedNewsletter +{ + public class Newsletter + { + public string Html { get; set; } + public int MovieCount { get; set; } + public int TvCount { get; set; } + + public bool Send => MovieCount > 0 || TvCount > 0; + } +} \ No newline at end of file diff --git a/Ombi.Services/Jobs/RecentlyAddedNewsletter/PlexRecentlyAddedNewsletter.cs b/Ombi.Services/Jobs/RecentlyAddedNewsletter/PlexRecentlyAddedNewsletter.cs index 79687636a..e1abb037a 100644 --- a/Ombi.Services/Jobs/RecentlyAddedNewsletter/PlexRecentlyAddedNewsletter.cs +++ b/Ombi.Services/Jobs/RecentlyAddedNewsletter/PlexRecentlyAddedNewsletter.cs @@ -42,11 +42,9 @@ using Ombi.Core.SettingModels; using Ombi.Helpers; using Ombi.Services.Jobs.Templates; using Ombi.Store.Models; -using Ombi.Store.Models.Emby; using Ombi.Store.Models.Plex; using Ombi.Store.Repository; using TMDbLib.Objects.Exceptions; -using EmbyMediaType = Ombi.Store.Models.Plex.EmbyMediaType; using PlexMediaType = Ombi.Store.Models.Plex.PlexMediaType; namespace Ombi.Services.Jobs.RecentlyAddedNewsletter @@ -81,7 +79,7 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter private static readonly Logger Log = LogManager.GetCurrentClassLogger(); - public string GetNewsletterHtml(bool test) + public Newsletter GetNewsletter(bool test) { try { @@ -90,7 +88,7 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter catch (Exception e) { Log.Error(e); - return string.Empty; + return null; } } @@ -100,9 +98,10 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter public PlexContent Content { get; set; } } - private string GetHtml(bool test) + private Newsletter GetHtml(bool test) { var sb = new StringBuilder(); + var newsletter = new Newsletter(); var plexSettings = PlexSettings.GetSettings(); var plexContent = Content.GetAll().ToList(); @@ -134,6 +133,7 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter }); } GenerateMovieHtml(info, sb); + newsletter.MovieCount = info.Count; info.Clear(); foreach (var t in filteredSeries) @@ -168,6 +168,7 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter //} } GenerateTvHtml(info, sb); + newsletter.TvCount = info.Count; var template = new RecentlyAddedTemplate(); var html = template.LoadTemplate(sb.ToString()); @@ -203,7 +204,8 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray()); Log.Debug(escapedHtml); - return escapedHtml; + newsletter.Html = escapedHtml; + return newsletter; } private void GenerateMovieHtml(IEnumerable recentlyAddedMovies, StringBuilder sb) diff --git a/Ombi.Services/Jobs/RecentlyAddedNewsletter/RecentlyAddedNewsletter.cs b/Ombi.Services/Jobs/RecentlyAddedNewsletter/RecentlyAddedNewsletter.cs index 330adf60c..6a15d00f4 100644 --- a/Ombi.Services/Jobs/RecentlyAddedNewsletter/RecentlyAddedNewsletter.cs +++ b/Ombi.Services/Jobs/RecentlyAddedNewsletter/RecentlyAddedNewsletter.cs @@ -137,22 +137,26 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter var embySettings = EmbySettings.GetSettings(); if (embySettings.Enable) { - var html = EmbyNewsletter.GetNewsletterHtml(testEmail); - - var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray()); - Log.Debug(escapedHtml); - SendNewsletter(newletterSettings, escapedHtml, testEmail, "New Content On Emby!"); + var letter = EmbyNewsletter.GetNewsletter(testEmail) ?? new Newsletter(); + if (letter.Send) + { + SendNewsletter(newletterSettings, letter.Html, testEmail, "New Content On Emby!"); + } + else + { + Log.Warn("There is no new content to send the newsletter"); + } } else { var plexSettings = PlexSettings.GetSettings(); if (plexSettings.Enable) { - var html = PlexNewsletter.GetNewsletterHtml(testEmail); - - var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray()); - Log.Debug(escapedHtml); - SendNewsletter(newletterSettings, html, testEmail); + var letter = PlexNewsletter.GetNewsletter(testEmail) ?? new Newsletter(); + if (letter.Send) + { + SendNewsletter(newletterSettings, letter.Html, testEmail); + } } } } 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..725f1a4ab 100644 --- a/Ombi.Services/Ombi.Services.csproj +++ b/Ombi.Services/Ombi.Services.csproj @@ -108,6 +108,7 @@ + @@ -147,6 +148,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.UI/Views/Admin/NewsletterSettings.cshtml b/Ombi.UI/Views/Admin/NewsletterSettings.cshtml index 2802e68d6..32bc10dce 100644 --- a/Ombi.UI/Views/Admin/NewsletterSettings.cshtml +++ b/Ombi.UI/Views/Admin/NewsletterSettings.cshtml @@ -32,7 +32,9 @@
- + +
@@ -84,6 +86,8 @@ $('#recentlyAddedBtn').click(function (e) { e.preventDefault(); + + generateNotify("This could take some time depending on if you have episode searching enabled and also how many new items have been added!", "info"); var base = '@Html.GetBaseUrl()'; var url = createBaseUrl(base, '/admin/testnewsletteradminemail'); $('#testEmailSpinner').attr("class", "fa fa-spinner fa-spin"); @@ -94,11 +98,11 @@ success: function (response) { if (response) { generateNotify(response.message, "success"); - $('#testSendMassEmailSpinner').attr("class", "fa fa-check"); + $('#testEmailSpinner').attr("class", "fa fa-check"); } else { generateNotify(response.message, "danger"); - $('#testSendMassEmailSpinner').attr("class", "fa fa-times"); + $('#testEmailSpinner').attr("class", "fa fa-times"); } }, error: function (e) { diff --git a/Ombi.UI/Views/Admin/Sonarr.cshtml b/Ombi.UI/Views/Admin/Sonarr.cshtml index 92c667708..4f2054a45 100644 --- a/Ombi.UI/Views/Admin/Sonarr.cshtml +++ b/Ombi.UI/Views/Admin/Sonarr.cshtml @@ -1,4 +1,4 @@ -@using Ombi.UI.Helpers + @using Ombi.UI.Helpers @Html.Partial("Shared/Partial/_Sidebar") @{ int port; @@ -16,6 +16,7 @@ { rootFolder = Model.RootPath.Replace("/", "//"); + rootFolder = rootFolder.Replace(@"\", @"\\"); } }
diff --git a/Ombi.UI/Views/Integration/Radarr.cshtml b/Ombi.UI/Views/Integration/Radarr.cshtml index 00a9d4968..05e653ad2 100644 --- a/Ombi.UI/Views/Integration/Radarr.cshtml +++ b/Ombi.UI/Views/Integration/Radarr.cshtml @@ -17,6 +17,7 @@ { rootFolder = Model.RootPath.Replace("/", "//"); + rootFolder = rootFolder.Replace(@"\", @"\\"); } }
diff --git a/Ombi.UI/Views/Shared/Partial/_Navbar.cshtml b/Ombi.UI/Views/Shared/Partial/_Navbar.cshtml index ae4901113..60a8420d3 100644 --- a/Ombi.UI/Views/Shared/Partial/_Navbar.cshtml +++ b/Ombi.UI/Views/Shared/Partial/_Navbar.cshtml @@ -20,6 +20,7 @@ { title = customName; } + var isAdmin = Html.IsAdmin(); } @@ -41,11 +42,11 @@ @Html.GetNavbarUrl(Context, "/search", UI.Layout_Search, "search") @Html.GetNavbarUrl(Context, "/requests", UI.Layout_Requests, "plus-circle") @Html.GetNavbarUrl(Context, "/issues", UI.Layout_Issues, "exclamation", "") - @if (Html.IsAdmin()) + @if (isAdmin) { @Html.GetNavbarUrl(Context, "/usermanagement", UI.Layout_Usermanagement, "users") } - @if (Html.IsAdmin()) + @if (isAdmin) {
  • } @@ -55,7 +56,7 @@ - @if (Html.IsAdmin()) + @if (isAdmin) {
  • @UI.Layout_Welcome @Context.CurrentUser.UserName