From b52a57b1173f87b3b97ba0a1e0e7de20b6791507 Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Wed, 5 Jul 2017 13:04:26 +0100 Subject: [PATCH] #1462 #865 Had to refactor how we use notificaitons. So we now have more notification fields about the request --- src/Ombi.Core/Engine/Interfaces/BaseEngine.cs | 27 +------- .../Engine/Interfaces/IMovieRequestEngine.cs | 3 - src/Ombi.Core/Engine/MovieRequestEngine.cs | 4 +- src/Ombi.Core/Engine/TvRequestEngine.cs | 4 +- src/Ombi.Core/INotificationHelper.cs | 1 + src/Ombi.Core/NotificationHelper.cs | 20 ++++-- .../EmailBasicTemplate.cs | 18 ++++- .../IEmailBasicTemplate.cs | 2 +- .../Ombi.Notifications.Templates.csproj | 6 ++ .../Templates/BasicTemplate.html | 2 +- .../Agents/DiscordNotification.cs | 52 +++++++++++---- .../Agents/EmailNotification.cs | 62 ++++++++++-------- .../Interfaces/BaseNotification.cs | 65 ++++++++++++++++++- .../Models/NotificationOptions.cs | 6 +- .../NotificationMessageResolver.cs | 63 +++++++++++++----- src/Ombi.Notifications/NotificationService.cs | 11 +++- src/Ombi.Store/Entities/User.cs | 3 + .../Repository/SettingsJsonRepository.cs | 4 +- .../notificationtemplate.component.ts | 5 ++ .../Controllers/External/TesterController.cs | 8 +-- src/Ombi/gulpfile.js | 2 +- 21 files changed, 256 insertions(+), 112 deletions(-) diff --git a/src/Ombi.Core/Engine/Interfaces/BaseEngine.cs b/src/Ombi.Core/Engine/Interfaces/BaseEngine.cs index cc895b7d9..42f3be755 100644 --- a/src/Ombi.Core/Engine/Interfaces/BaseEngine.cs +++ b/src/Ombi.Core/Engine/Interfaces/BaseEngine.cs @@ -1,7 +1,6 @@ using Ombi.Core.Claims; using Ombi.Core.Rule; using Ombi.Core.Rules; -using Ombi.Store.Entities; using System.Collections.Generic; using System.Security.Principal; using System.Threading.Tasks; @@ -29,35 +28,15 @@ namespace Ombi.Core.Engine.Interfaces return User.IsInRole(roleName); } - protected bool ShouldSendNotification(RequestType type) + protected bool ShouldSendNotification(BaseRequest req) { - var sendNotification = !ShouldAutoApprove(type); /*|| !prSettings.IgnoreNotifyForAutoApprovedRequests;*/ + var sendNotification = !req.Approved; /*|| !prSettings.IgnoreNotifyForAutoApprovedRequests;*/ if (HasRole(OmbiClaims.Admin)) sendNotification = false; // Don't bother sending a notification if the user is an admin return sendNotification; } - - public bool ShouldAutoApprove(RequestType requestType) - { - var admin = HasRole(OmbiClaims.Admin); - // if the user is an admin, they go ahead and allow auto-approval - if (admin) return true; - - // check by request type if the category requires approval or not - switch (requestType) - { - case RequestType.Movie: - return HasRole(OmbiClaims.AutoApproveMovie); - - case RequestType.TvShow: - return HasRole(OmbiClaims.AutoApproveTv); - - default: - return false; - } - } - + public async Task> RunRequestRules(BaseRequest model) { var ruleResults = await Rules.StartRequestRules(model); diff --git a/src/Ombi.Core/Engine/Interfaces/IMovieRequestEngine.cs b/src/Ombi.Core/Engine/Interfaces/IMovieRequestEngine.cs index b8e33e50b..46d59be80 100644 --- a/src/Ombi.Core/Engine/Interfaces/IMovieRequestEngine.cs +++ b/src/Ombi.Core/Engine/Interfaces/IMovieRequestEngine.cs @@ -10,9 +10,6 @@ namespace Ombi.Core.Engine.Interfaces { Task RequestMovie(SearchMovieViewModel model); - bool ShouldAutoApprove(RequestType requestType); - - Task> SearchMovieRequest(string search); Task RemoveMovieRequest(int requestId); diff --git a/src/Ombi.Core/Engine/MovieRequestEngine.cs b/src/Ombi.Core/Engine/MovieRequestEngine.cs index 8760cb3ec..95f10ce3d 100644 --- a/src/Ombi.Core/Engine/MovieRequestEngine.cs +++ b/src/Ombi.Core/Engine/MovieRequestEngine.cs @@ -142,6 +142,8 @@ namespace Ombi.Core.Engine $"{fullMovieName} has been successfully added!"); } + + public async Task> GetRequests(int count, int position) { var allRequests = await MovieRepository.Get().Skip(position).Take(count).ToListAsync(); @@ -191,7 +193,7 @@ namespace Ombi.Core.Engine { await MovieRepository.Add(model); - if (ShouldSendNotification(RequestType.Movie)) + if (ShouldSendNotification(model)) { NotificationHelper.NewRequest(model); } diff --git a/src/Ombi.Core/Engine/TvRequestEngine.cs b/src/Ombi.Core/Engine/TvRequestEngine.cs index bc63d2a16..2df8a2e3d 100644 --- a/src/Ombi.Core/Engine/TvRequestEngine.cs +++ b/src/Ombi.Core/Engine/TvRequestEngine.cs @@ -293,9 +293,9 @@ namespace Ombi.Core.Engine private Task AfterRequest(ChildRequests model) { - if (ShouldSendNotification(RequestType.TvShow)) + if (ShouldSendNotification(model)) { - //NotificationHelper.NewRequest(model.ParentRequest); + NotificationHelper.NewRequest(model); } if(model.Approved) diff --git a/src/Ombi.Core/INotificationHelper.cs b/src/Ombi.Core/INotificationHelper.cs index a10f0882b..fead5e475 100644 --- a/src/Ombi.Core/INotificationHelper.cs +++ b/src/Ombi.Core/INotificationHelper.cs @@ -6,5 +6,6 @@ namespace Ombi.Core public interface INotificationHelper { void NewRequest(FullBaseRequest model); + void NewRequest(ChildRequests model); } } \ No newline at end of file diff --git a/src/Ombi.Core/NotificationHelper.cs b/src/Ombi.Core/NotificationHelper.cs index abda4fb0c..3994ccf2d 100644 --- a/src/Ombi.Core/NotificationHelper.cs +++ b/src/Ombi.Core/NotificationHelper.cs @@ -21,14 +21,22 @@ namespace Ombi.Core { var notificationModel = new NotificationOptions { - Title = model.Title, - RequestedUser = model.RequestedUser.Username, + RequestId = model.Id, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest, - RequestType = model.RequestType, - ImgSrc = model.RequestType == RequestType.Movie - ? $"https://image.tmdb.org/t/p/w300/{model.PosterPath}" - : model.PosterPath + RequestType = model.RequestType + }; + BackgroundJob.Enqueue(() => NotificationService.Publish(notificationModel)); + + } + public void NewRequest(ChildRequests model) + { + var notificationModel = new NotificationOptions + { + RequestId = model.Id, + DateTime = DateTime.Now, + NotificationType = NotificationType.NewRequest, + RequestType = model.RequestType }; BackgroundJob.Enqueue(() => NotificationService.Publish(notificationModel)); diff --git a/src/Ombi.Notifications.Templates/EmailBasicTemplate.cs b/src/Ombi.Notifications.Templates/EmailBasicTemplate.cs index 36ecf40e5..52febf14e 100644 --- a/src/Ombi.Notifications.Templates/EmailBasicTemplate.cs +++ b/src/Ombi.Notifications.Templates/EmailBasicTemplate.cs @@ -6,22 +6,34 @@ namespace Ombi.Notifications.Templates { public class EmailBasicTemplate : IEmailBasicTemplate { - public string TemplateLocation => Path.Combine(Directory.GetCurrentDirectory(), "Templates","BasicTemplate.html"); + public string TemplateLocation + { + get + { +#if DEBUG + return Path.Combine(Directory.GetCurrentDirectory(), "bin","Debug", "netcoreapp1.1","Templates", "BasicTemplate.html"); +#else + return Path.Combine(Directory.GetCurrentDirectory(), "Templates","BasicTemplate.html"); +#endif + } + } + + private const string SubjectKey = "{@SUBJECT}"; private const string BodyKey = "{@BODY}"; private const string ImgSrc = "{@IMGSRC}"; private const string DateKey = "{@DATENOW}"; - public string LoadTemplate(string subject, string body, string imgSrc) + public string LoadTemplate(string subject, string body, string img) { try { var sb = new StringBuilder(File.ReadAllText(TemplateLocation)); sb.Replace(SubjectKey, subject); sb.Replace(BodyKey, body); - sb.Replace(ImgSrc, imgSrc); sb.Replace(DateKey, DateTime.Now.ToString("f")); + sb.Replace(ImgSrc, img); return sb.ToString(); } diff --git a/src/Ombi.Notifications.Templates/IEmailBasicTemplate.cs b/src/Ombi.Notifications.Templates/IEmailBasicTemplate.cs index 88f3a2fff..b9ec7b6f3 100644 --- a/src/Ombi.Notifications.Templates/IEmailBasicTemplate.cs +++ b/src/Ombi.Notifications.Templates/IEmailBasicTemplate.cs @@ -2,7 +2,7 @@ { public interface IEmailBasicTemplate { - string LoadTemplate(string subject, string body, string imgSrc); + string LoadTemplate(string subject, string body, string img); string TemplateLocation { get; } } } \ No newline at end of file diff --git a/src/Ombi.Notifications.Templates/Ombi.Notifications.Templates.csproj b/src/Ombi.Notifications.Templates/Ombi.Notifications.Templates.csproj index 9eca54729..6fac75e3b 100644 --- a/src/Ombi.Notifications.Templates/Ombi.Notifications.Templates.csproj +++ b/src/Ombi.Notifications.Templates/Ombi.Notifications.Templates.csproj @@ -4,4 +4,10 @@ netstandard1.4 + + + Always + + + \ No newline at end of file diff --git a/src/Ombi.Notifications.Templates/Templates/BasicTemplate.html b/src/Ombi.Notifications.Templates/Templates/BasicTemplate.html index 036c7cc21..3d745ee7f 100644 --- a/src/Ombi.Notifications.Templates/Templates/BasicTemplate.html +++ b/src/Ombi.Notifications.Templates/Templates/BasicTemplate.html @@ -172,7 +172,7 @@
- Powered by Ombi + Powered by Ombi {@DATENOW}
diff --git a/src/Ombi.Notifications/Agents/DiscordNotification.cs b/src/Ombi.Notifications/Agents/DiscordNotification.cs index 421211b27..47cbcf23e 100644 --- a/src/Ombi.Notifications/Agents/DiscordNotification.cs +++ b/src/Ombi.Notifications/Agents/DiscordNotification.cs @@ -9,13 +9,15 @@ using Ombi.Helpers; using Ombi.Notifications.Interfaces; using Ombi.Notifications.Models; using Ombi.Settings.Settings.Models.Notifications; +using Ombi.Store.Entities; using Ombi.Store.Repository; +using Ombi.Store.Repository.Requests; namespace Ombi.Notifications.Agents { public class DiscordNotification : BaseNotification, IDiscordNotification { - public DiscordNotification(IDiscordApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r) : base(sn, r) + public DiscordNotification(IDiscordApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t) : base(sn, r, m, t) { Api = api; Logger = log; @@ -50,67 +52,89 @@ namespace Ombi.Notifications.Agents protected override async Task NewRequest(NotificationOptions model, DiscordNotificationSettings settings) { - var template = await TemplateRepository.GetTemplate(NotificationAgent.Email, NotificationType.NewRequest); + var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.NewRequest, model); var notification = new NotificationMessage { - Message = template.Message, + Message = parsed.Message, }; + + notification.Other.Add("image", parsed.Image); await Send(notification, settings); } protected override async Task Issue(NotificationOptions model, DiscordNotificationSettings settings) { - var template = await TemplateRepository.GetTemplate(NotificationAgent.Email, NotificationType.Issue); + var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.Issue, model); var notification = new NotificationMessage { - Message = template.Message, + Message = parsed.Message, }; + notification.Other.Add("image", parsed.Image); await Send(notification, settings); } protected override async Task AddedToRequestQueue(NotificationOptions model, DiscordNotificationSettings settings) { - var message = $"Hello! The user '{model.RequestedUser}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; + var user = string.Empty; + var title = string.Empty; + var image = string.Empty; + if (model.RequestType == RequestType.Movie) + { + user = MovieRequest.RequestedUser.UserAlias; + title = MovieRequest.Title; + image = MovieRequest.PosterPath; + } + else + { + user = TvRequest.RequestedUser.UserAlias; + title = TvRequest.ParentRequest.Title; + image = TvRequest.ParentRequest.PosterPath; + } + var message = $"Hello! The user '{user}' has requested {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 }; - notification.Other.Add("image", model.ImgSrc); + notification.Other.Add("image", image); await Send(notification, settings); } protected override async Task RequestDeclined(NotificationOptions model, DiscordNotificationSettings settings) { - var template = await TemplateRepository.GetTemplate(NotificationAgent.Email, NotificationType.RequestDeclined); + var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.RequestDeclined, model); var notification = new NotificationMessage { - Message = template.Message, + Message = parsed.Message, }; + notification.Other.Add("image", parsed.Image); await Send(notification, settings); } protected override async Task RequestApproved(NotificationOptions model, DiscordNotificationSettings settings) { - var template = await TemplateRepository.GetTemplate(NotificationAgent.Email, NotificationType.RequestApproved); + var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.RequestApproved, model); var notification = new NotificationMessage { - Message = template.Message, + Message = parsed.Message, }; + + notification.Other.Add("image", parsed.Image); await Send(notification, settings); } protected override async Task AvailableRequest(NotificationOptions model, DiscordNotificationSettings settings) { - var template = await TemplateRepository.GetTemplate(NotificationAgent.Email, NotificationType.RequestAvailable); + var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.RequestAvailable, model); var notification = new NotificationMessage { - Message = template.Message, + Message = parsed.Message, }; + notification.Other.Add("image", parsed.Image); await Send(notification, settings); } @@ -138,7 +162,7 @@ namespace Ombi.Notifications.Agents } }; } - + await Api.SendMessage(discordBody, settings.WebookId, settings.Token); } catch (Exception e) diff --git a/src/Ombi.Notifications/Agents/EmailNotification.cs b/src/Ombi.Notifications/Agents/EmailNotification.cs index 07f253d56..0ded02299 100644 --- a/src/Ombi.Notifications/Agents/EmailNotification.cs +++ b/src/Ombi.Notifications/Agents/EmailNotification.cs @@ -8,13 +8,15 @@ using Ombi.Notifications.Interfaces; using Ombi.Notifications.Models; using Ombi.Notifications.Templates; using Ombi.Settings.Settings.Models.Notifications; +using Ombi.Store.Entities; using Ombi.Store.Repository; +using Ombi.Store.Repository.Requests; namespace Ombi.Notifications.Agents { public class EmailNotification : BaseNotification, IEmailNotification { - public EmailNotification(ISettingsService settings, INotificationTemplatesRepository r) : base(settings, r) + public EmailNotification(ISettingsService settings, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t) : base(settings, r, m, t) { } @@ -40,23 +42,14 @@ namespace Ombi.Notifications.Agents return true; } - + private async Task LoadTemplate(NotificationType type, NotificationOptions model, EmailNotificationSettings settings) { - var template = await TemplateRepository.GetTemplate(NotificationAgent.Email, type); - if (!template.Enabled) - { - return null; - } - // Need to do the parsing - var resolver = new NotificationMessageResolver(); - var parsed = resolver.ParseMessage(template, new NotificationMessageCurlys(model.RequestedUser, model.Title, DateTime.Now.ToString("D"), - model.NotificationType.ToString(), null)); + var parsed = await LoadTemplate(NotificationAgent.Email, type, model); - var email = new EmailBasicTemplate(); - var html = email.LoadTemplate(parsed.Subject, parsed.Message, model.ImgSrc); - + var html = email.LoadTemplate(parsed.Subject, parsed.Message,parsed.Image); + var message = new NotificationMessage { @@ -67,8 +60,8 @@ namespace Ombi.Notifications.Agents return message; } - - + + protected override async Task NewRequest(NotificationOptions model, EmailNotificationSettings settings) { @@ -77,8 +70,8 @@ namespace Ombi.Notifications.Agents { return; } - - message.Other.Add("PlainTextBody", $"Hello! The user '{model.RequestedUser}' has requested the {model.RequestType} '{model.Title}'! Please log in to approve this request. Request Date: {model.DateTime:f}"); + + //message.Other.Add("PlainTextBody", $"Hello! The user '{model.RequestedUser}' has requested the {model.RequestType} '{model.Title}'! Please log in to approve this request. Request Date: {model.DateTime:f}"); await Send(message, settings); } @@ -91,7 +84,7 @@ namespace Ombi.Notifications.Agents return; } - message.Other.Add("PlainTextBody", $"Hello! The user '{model.RequestedUser}' has reported a new issue {model.Body} for the title {model.Title}!"); + //message.Other.Add("PlainTextBody", $"Hello! The user '{model.RequestedUser}' has reported a new issue {model.Body} for the title {model.Title}!"); await Send(message, settings); } @@ -99,10 +92,24 @@ namespace Ombi.Notifications.Agents protected override async Task AddedToRequestQueue(NotificationOptions model, EmailNotificationSettings settings) { var email = new EmailBasicTemplate(); + var user = string.Empty; + var title = string.Empty; + var img = string.Empty; + if (model.RequestType == RequestType.Movie) + { + user = MovieRequest.RequestedUser.UserAlias; + title = MovieRequest.Title; + img = MovieRequest.PosterPath; + } + else + { + user = TvRequest.RequestedUser.UserAlias; + title = TvRequest.ParentRequest.Title; + img = TvRequest.ParentRequest.PosterPath; + } var html = email.LoadTemplate( "Ombi: A request could not be added.", - $"Hello! The user '{model.RequestedUser}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying", - model.ImgSrc); + $"Hello! The user '{user}' has requested {title} but it could not be added. This has been added into the requests queue and will keep retrying", img); var message = new NotificationMessage { @@ -111,7 +118,7 @@ namespace Ombi.Notifications.Agents To = settings.AdminEmail, }; - message.Other.Add("PlainTextBody", $"Hello! The user '{model.RequestedUser}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"); + //message.Other.Add("PlainTextBody", $"Hello! The user '{model.RequestedUser}' 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); @@ -125,7 +132,7 @@ namespace Ombi.Notifications.Agents return; } - message.Other.Add("PlainTextBody", $"Hello! Your request for {model.Title} has been declined, Sorry!"); + //message.Other.Add("PlainTextBody", $"Hello! Your request for {model.Title} has been declined, Sorry!"); await Send(message, settings); @@ -139,7 +146,7 @@ namespace Ombi.Notifications.Agents return; } - message.Other.Add("PlainTextBody", $"Hello! Your request for {model.Title} has been approved!"); + //message.Other.Add("PlainTextBody", $"Hello! Your request for {model.Title} has been approved!"); await Send(message, settings); } @@ -152,7 +159,7 @@ namespace Ombi.Notifications.Agents return; } - message.Other.Add("PlainTextBody", $"Hello! You requested {model.Title} on Ombi! This is now available on Plex! :)"); + //message.Other.Add("PlainTextBody", $"Hello! You requested {model.Title} on Ombi! This is now available on Plex! :)"); await Send(message, settings); } @@ -164,7 +171,7 @@ namespace Ombi.Notifications.Agents var body = new BodyBuilder { HtmlBody = model.Message, - TextBody = model.Other["PlainTextBody"] + //TextBody = model.Other["PlainTextBody"] }; var message = new MimeMessage @@ -204,8 +211,7 @@ namespace Ombi.Notifications.Agents var email = new EmailBasicTemplate(); var html = email.LoadTemplate( "Test Message", - "This is just a test! Success!", - model.ImgSrc); + "This is just a test! Success!", ""); var message = new NotificationMessage { Message = html, diff --git a/src/Ombi.Notifications/Interfaces/BaseNotification.cs b/src/Ombi.Notifications/Interfaces/BaseNotification.cs index 00f74d099..5627afc63 100644 --- a/src/Ombi.Notifications/Interfaces/BaseNotification.cs +++ b/src/Ombi.Notifications/Interfaces/BaseNotification.cs @@ -1,22 +1,34 @@ using System; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; using Ombi.Core.Settings; using Ombi.Helpers; using Ombi.Notifications.Models; +using Ombi.Store.Entities; +using Ombi.Store.Entities.Requests; using Ombi.Store.Repository; +using Ombi.Store.Repository.Requests; namespace Ombi.Notifications.Interfaces { public abstract class BaseNotification : INotification where T : Settings.Settings.Models.Settings, new() { - protected BaseNotification(ISettingsService settings, INotificationTemplatesRepository templateRepo) + protected BaseNotification(ISettingsService settings, INotificationTemplatesRepository templateRepo, IMovieRequestRepository movie, ITvRequestRepository tv) { Settings = settings; TemplateRepository = templateRepo; + MovieRepository = movie; + TvRepository = tv; } protected ISettingsService Settings { get; } protected INotificationTemplatesRepository TemplateRepository { get; } + protected IMovieRequestRepository MovieRepository { get; } + protected ITvRequestRepository TvRepository { get; } + + protected ChildRequests TvRequest { get; set; } + protected MovieRequests MovieRequest { get; set; } + public abstract string NotificationName { get; } public async Task NotifyAsync(NotificationOptions model) @@ -30,11 +42,18 @@ namespace Ombi.Notifications.Interfaces if (settings == null) await NotifyAsync(model); var notificationSettings = (T)settings; - + if (!ValidateConfiguration(notificationSettings)) { return; } + + // Is this a test? + // The request id for tests is -1 + if (model.RequestId > 0) + { + await LoadRequest(model.RequestId, model.RequestType); + } try { switch (model.NotificationType) @@ -73,12 +92,54 @@ namespace Ombi.Notifications.Interfaces } } + protected virtual async Task LoadRequest(int requestId, RequestType type) + { + if (type == RequestType.Movie) + { + MovieRequest = await MovieRepository.Get().FirstOrDefaultAsync(x => x.Id == requestId); + MovieRequest.PosterPath = $"https://image.tmdb.org/t/p/w300/{MovieRequest.PosterPath}"; + } + else + { + TvRequest = await TvRepository.GetChild().FirstOrDefaultAsync(x => x.Id == requestId); + } + } + private T GetConfiguration() { var settings = Settings.GetSettings(); return settings; } + protected virtual async Task LoadTemplate(NotificationAgent agent, NotificationType type, NotificationOptions model) + { + var template = await TemplateRepository.GetTemplate(agent, type); + if (!template.Enabled) + { + return null; + } + var parsed = Parse(model, template); + + return parsed; + } + + private NotificationMessageContent Parse(NotificationOptions model, NotificationTemplates template) + { + var resolver = new NotificationMessageResolver(); + var curlys = new NotificationMessageCurlys(); + if (model.RequestType == RequestType.Movie) + { + curlys.Setup(MovieRequest); + } + else + { + curlys.Setup(TvRequest); + } + var parsed = resolver.ParseMessage(template, curlys); + + return parsed; + } + protected abstract bool ValidateConfiguration(T settings); protected abstract Task NewRequest(NotificationOptions model, T settings); diff --git a/src/Ombi.Notifications/Models/NotificationOptions.cs b/src/Ombi.Notifications/Models/NotificationOptions.cs index 5982d7e59..92cc1fa51 100644 --- a/src/Ombi.Notifications/Models/NotificationOptions.cs +++ b/src/Ombi.Notifications/Models/NotificationOptions.cs @@ -7,13 +7,9 @@ namespace Ombi.Notifications.Models { public class NotificationOptions { - public string Title { get; set; } - public string Body { get; set; } + public int RequestId { get; set; } public DateTime DateTime { get; set; } = DateTime.Now; public NotificationType NotificationType { get; set; } - public string RequestedUser { get; set; } - public string UserEmail { get; set; } public RequestType RequestType { get; set; } - public string ImgSrc { get; set; } } } \ No newline at end of file diff --git a/src/Ombi.Notifications/NotificationMessageResolver.cs b/src/Ombi.Notifications/NotificationMessageResolver.cs index d6332ac6c..371a79583 100644 --- a/src/Ombi.Notifications/NotificationMessageResolver.cs +++ b/src/Ombi.Notifications/NotificationMessageResolver.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Linq; using Ombi.Store.Entities; +using Ombi.Store.Entities.Requests; namespace Ombi.Notifications { @@ -8,24 +10,49 @@ namespace Ombi.Notifications { public string Subject { get; set; } public string Message { get; set; } + public string Image { get; set; } } public class NotificationMessageCurlys - { - public NotificationMessageCurlys(string requestedUser, string title, string requestedDateTime, string type, string issue) + { + + public void Setup(FullBaseRequest req) + { + RequestedUser = string.IsNullOrEmpty(req.RequestedUser.Alias) + ? req.RequestedUser.Username + : req.RequestedUser.Alias; + Title = req.Title; + RequestedDate = req.RequestedDate.ToString("D"); + Type = req.RequestType.ToString(); + Overview = req.Overview; + Year = req.ReleaseDate.Year.ToString(); + PosterImage = req.PosterPath; + } + + public void Setup(ChildRequests req) { - RequestedUser = requestedUser; - Title = title; - RequestedDate = requestedDateTime; - Type = type; - Issue = issue; + RequestedUser = string.IsNullOrEmpty(req.RequestedUser.Alias) + ? req.RequestedUser.Username + : req.RequestedUser.Alias; + Title = req.ParentRequest.Title; + RequestedDate = req.RequestedDate.ToString("D"); + Type = req.RequestType.ToString(); + Overview = req.ParentRequest.Overview; + Year = req.ParentRequest.ReleaseDate.Year.ToString(); + PosterImage = req.ParentRequest.PosterPath; + // DO Episode and Season Lists } // User Defined - private string RequestedUser { get; } - private string Title { get; } - private string RequestedDate { get; } - private string Type { get; } - private string Issue { get; } + public string RequestedUser { get; set; } + public string Title { get; set; } + public string RequestedDate { get; set; } + public string Type { get; set; } + public string Issue { get; set; } + public string Overview { get; set; } + public string Year { get; set; } + public string EpisodesList { get; set; } + public string SeasonsList { get; set; } + public string PosterImage { get; set; } // System Defined private string LongDate => DateTime.Now.ToString("D"); @@ -44,6 +71,11 @@ namespace Ombi.Notifications {nameof(ShortDate),ShortDate}, {nameof(LongTime),LongTime}, {nameof(ShortTime),ShortTime}, + {nameof(Overview),Overview}, + {nameof(Year),Year}, + {nameof(EpisodesList),EpisodesList}, + {nameof(SeasonsList),SeasonsList}, + {nameof(PosterImage),PosterImage}, }; } @@ -66,7 +98,9 @@ namespace Ombi.Notifications /// public NotificationMessageContent ParseMessage(NotificationTemplates notification, NotificationMessageCurlys c) { - return Resolve(notification.Message, notification.Subject, c.Curlys); + var content = Resolve(notification.Message, notification.Subject, c.Curlys); + content.Image = c.PosterImage; + return content; } /// @@ -84,8 +118,7 @@ namespace Ombi.Notifications body = ReplaceFields(bodyFields, parameters, body); subject = ReplaceFields(subjectFields, parameters, subject); - - return new NotificationMessageContent { Message = body ?? string.Empty, Subject = subject ?? string.Empty }; + return new NotificationMessageContent { Message = body ?? string.Empty, Subject = subject ?? string.Empty}; } /// diff --git a/src/Ombi.Notifications/NotificationService.cs b/src/Ombi.Notifications/NotificationService.cs index 8f93c044d..49e1d24a0 100644 --- a/src/Ombi.Notifications/NotificationService.cs +++ b/src/Ombi.Notifications/NotificationService.cs @@ -52,8 +52,13 @@ namespace Ombi.Notifications /// public async Task Publish(NotificationOptions model) { - var notificationTasks = NotificationAgents.Select(notification => NotifyAsync(notification, model)); + //var notificationTasks = NotificationAgents.Select(notification => NotifyAsync(notification, model)); + var notificationTasks = new List(); + foreach (var agent in NotificationAgents) + { + notificationTasks.Add(NotifyAsync(agent,model)); + } await Task.WhenAll(notificationTasks).ConfigureAwait(false); } @@ -86,6 +91,10 @@ namespace Ombi.Notifications private async Task NotifyAsync(INotification notification, NotificationOptions model, Ombi.Settings.Settings.Models.Settings settings) { + if (model.RequestId == 0) + { + throw new ArgumentException("RequestId is not set"); + } try { await notification.NotifyAsync(model, settings).ConfigureAwait(false); diff --git a/src/Ombi.Store/Entities/User.cs b/src/Ombi.Store/Entities/User.cs index 3b080da81..94d215202 100644 --- a/src/Ombi.Store/Entities/User.cs +++ b/src/Ombi.Store/Entities/User.cs @@ -44,6 +44,9 @@ namespace Ombi.Store.Entities public byte[] Salt { get; set; } public UserType UserType { get; set; } + [NotMapped] + public string UserAlias => string.IsNullOrEmpty(Alias) ? Username : Alias; + [NotMapped] public List Claims { get => JsonConvert.DeserializeObject>(ClaimsSerialized, new ClaimConverter()); diff --git a/src/Ombi.Store/Repository/SettingsJsonRepository.cs b/src/Ombi.Store/Repository/SettingsJsonRepository.cs index e5e0da498..89958665f 100644 --- a/src/Ombi.Store/Repository/SettingsJsonRepository.cs +++ b/src/Ombi.Store/Repository/SettingsJsonRepository.cs @@ -49,7 +49,9 @@ namespace Ombi.Store.Repository public GlobalSettings Get(string pageName) { - return Db.Settings.FirstOrDefault(x => x.SettingsName == pageName); + var entity = Db.Settings.FirstOrDefault(x => x.SettingsName == pageName); + Db.Entry(entity).Reload(); + return entity; } public async Task GetAsync(string settingsName) diff --git a/src/Ombi/ClientApp/app/settings/notifications/notificationtemplate.component.ts b/src/Ombi/ClientApp/app/settings/notifications/notificationtemplate.component.ts index ab888d4e6..fdb952ee3 100644 --- a/src/Ombi/ClientApp/app/settings/notifications/notificationtemplate.component.ts +++ b/src/Ombi/ClientApp/app/settings/notifications/notificationtemplate.component.ts @@ -15,6 +15,11 @@ export class NotificationTemplate { {RequestedDate} : The Date the media was requested
{Title} : The title of the request e.g. Lion King
{Type} : The request type e.g. Movie/Tv Show
+{Overview} : Overview of the requested item
+{Year} : The release year of the request
+{EpisodesList} : A comma seperated list of Episodes requested
+{SeasonsList} : A comma seperated list of seasons requested
+{PosterImage} : The requested poster image link
{LongDate} : 15 June 2017
{ShortDate} : 15/06/2017
{LongTime} : 16:02:34
diff --git a/src/Ombi/Controllers/External/TesterController.cs b/src/Ombi/Controllers/External/TesterController.cs index cb4c0c6d4..17ed01581 100644 --- a/src/Ombi/Controllers/External/TesterController.cs +++ b/src/Ombi/Controllers/External/TesterController.cs @@ -43,7 +43,8 @@ namespace Ombi.Controllers.External public bool Discord([FromBody] DiscordNotificationSettings settings) { settings.Enabled = true; - BackgroundJob.Enqueue(() => Service.PublishTest(new NotificationOptions{NotificationType = NotificationType.Test}, settings, (DiscordNotification)DiscordNotification)); + DiscordNotification.NotifyAsync( + new NotificationOptions {NotificationType = NotificationType.Test, RequestId = -1}, settings); return true; } @@ -60,10 +61,9 @@ namespace Ombi.Controllers.External var notificationModel = new NotificationOptions { NotificationType = NotificationType.Test, - DateTime = DateTime.Now, - ImgSrc = "https://imgs.xkcd.com/comics/shouldnt_be_hard.png" + RequestId = -1 }; - BackgroundJob.Enqueue(() => Service.PublishTest(notificationModel, settings, EmailNotification)); + EmailNotification.NotifyAsync(notificationModel, settings); return true; } diff --git a/src/Ombi/gulpfile.js b/src/Ombi/gulpfile.js index 400aaf897..b3c1f538a 100644 --- a/src/Ombi/gulpfile.js +++ b/src/Ombi/gulpfile.js @@ -1,5 +1,5 @@ /// -'use strict'; +'use strict'; const gulp = require('gulp'); const run = require('gulp-run');