From f1bca13586e4eb8ffe014211aba963a073870e85 Mon Sep 17 00:00:00 2001 From: TidusJar Date: Mon, 21 Jan 2019 13:16:35 +0000 Subject: [PATCH] Added the request queue to the notifications UI so you can turn it off per notification agent #2747 --- .../Agents/DiscordNotification.cs | 138 +++--------------- .../Agents/EmailNotification.cs | 33 +---- .../Agents/MattermostNotification.cs | 133 +++-------------- .../Agents/MobileNotification.cs | 20 +-- .../Agents/PushbulletNotification.cs | 123 ++++------------ .../Agents/PushoverNotification.cs | 122 +++------------- .../Agents/SlackNotification.cs | 128 +++------------- .../Agents/TelegramNotification.cs | 123 +++------------- src/Ombi.Store/Context/OmbiContext.cs | 10 +- 9 files changed, 169 insertions(+), 661 deletions(-) diff --git a/src/Ombi.Notifications/Agents/DiscordNotification.cs b/src/Ombi.Notifications/Agents/DiscordNotification.cs index c0671fc90..77580e5e4 100644 --- a/src/Ombi.Notifications/Agents/DiscordNotification.cs +++ b/src/Ombi.Notifications/Agents/DiscordNotification.cs @@ -56,148 +56,42 @@ namespace Ombi.Notifications.Agents protected override async Task NewRequest(NotificationOptions model, DiscordNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.NewRequest, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.NewRequest} is disabled for {NotificationAgent.Discord}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.NewRequest); } protected override async Task NewIssue(NotificationOptions model, DiscordNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.Issue, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.Issue} is disabled for {NotificationAgent.Discord}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.Issue); } protected override async Task IssueComment(NotificationOptions model, DiscordNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.IssueComment, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Discord}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.IssueComment); } protected override async Task IssueResolved(NotificationOptions model, DiscordNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.IssueResolved, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.IssueResolved} is disabled for {NotificationAgent.Discord}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.IssueResolved); } protected override async Task AddedToRequestQueue(NotificationOptions model, DiscordNotificationSettings settings) { - 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 if (model.RequestType == RequestType.TvShow) - { - user = TvRequest.RequestedUser.UserAlias; - title = TvRequest.ParentRequest.Title; - image = TvRequest.ParentRequest.PosterPath; - } - else if (model.RequestType == RequestType.Album) - { - user = AlbumRequest.RequestedUser.UserAlias; - title = AlbumRequest.Title; - image = AlbumRequest.Cover; - } - 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", image); - await Send(notification, settings); + await Run(model, settings, NotificationType.ItemAddedToFaultQueue); } protected override async Task RequestDeclined(NotificationOptions model, DiscordNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.RequestDeclined, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestDeclined} is disabled for {NotificationAgent.Discord}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestDeclined); } protected override async Task RequestApproved(NotificationOptions model, DiscordNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.RequestApproved, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestApproved} is disabled for {NotificationAgent.Discord}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestApproved); } protected override async Task AvailableRequest(NotificationOptions model, DiscordNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.RequestAvailable, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestAvailable} is disabled for {NotificationAgent.Discord}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestAvailable); } protected override async Task Send(NotificationMessage model, DiscordNotificationSettings settings) @@ -242,5 +136,21 @@ namespace Ombi.Notifications.Agents }; await Send(notification, settings); } + + private async Task Run(NotificationOptions model, DiscordNotificationSettings settings, NotificationType type) + { + var parsed = await LoadTemplate(NotificationAgent.Discord, type, model); + if (parsed.Disabled) + { + Logger.LogInformation($"Template {type} is disabled for {NotificationAgent.Discord}"); + return; + } + var notification = new NotificationMessage + { + Message = parsed.Message, + }; + notification.Other.Add("image", parsed.Image); + await Send(notification, settings); + } } } diff --git a/src/Ombi.Notifications/Agents/EmailNotification.cs b/src/Ombi.Notifications/Agents/EmailNotification.cs index 9621fed43..a7f9334fb 100644 --- a/src/Ombi.Notifications/Agents/EmailNotification.cs +++ b/src/Ombi.Notifications/Agents/EmailNotification.cs @@ -89,7 +89,6 @@ namespace Ombi.Notifications.Agents } else { - // Send to admin message.To = settings.AdminEmail; } @@ -183,37 +182,21 @@ 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) + if (!model.Recipient.HasValue()) { - user = MovieRequest.RequestedUser.UserAlias; - title = MovieRequest.Title; - img = $"https://image.tmdb.org/t/p/w300/{MovieRequest.PosterPath}"; + return; } - else + var message = await LoadTemplate(NotificationType.ItemAddedToFaultQueue, model, settings); + if (message == null) { - user = TvRequest.RequestedUser.UserAlias; - title = TvRequest.ParentRequest.Title; - img = TvRequest.ParentRequest.PosterPath; + return; } - var html = email.LoadTemplate( - $"{Customization.ApplicationName}: A request could not be added.", - $"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, Customization.Logo); - - var message = new NotificationMessage - { - Message = html, - Subject = $"{Customization.ApplicationName}: A request could not be added", - To = settings.AdminEmail, - }; - - var plaintext = $"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 plaintext = await LoadPlainTextMessage(NotificationType.ItemAddedToFaultQueue, model, settings); message.Other.Add("PlainTextBody", plaintext); + // Issues resolved should be sent to the user + message.To = settings.AdminEmail; await Send(message, settings); } diff --git a/src/Ombi.Notifications/Agents/MattermostNotification.cs b/src/Ombi.Notifications/Agents/MattermostNotification.cs index 62c5505c5..f64a0fe21 100644 --- a/src/Ombi.Notifications/Agents/MattermostNotification.cs +++ b/src/Ombi.Notifications/Agents/MattermostNotification.cs @@ -46,20 +46,7 @@ namespace Ombi.Notifications.Agents protected override async Task NewRequest(NotificationOptions model, MattermostNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Mattermost, NotificationType.NewRequest, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.NewRequest} is disabled for {NotificationAgent.Mattermost}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - - AddOtherInformation(model, notification, parsed); - //notification.Other.Add("overview", model.RequestType == RequestType.Movie ? base.MovieRequest.Overview : TvRequest.); - await Send(notification, settings); + await Run(model, settings, NotificationType.NewRequest); } private void AddOtherInformation(NotificationOptions model, NotificationMessage notification, @@ -71,125 +58,37 @@ namespace Ombi.Notifications.Agents protected override async Task NewIssue(NotificationOptions model, MattermostNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Mattermost, NotificationType.Issue, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.Issue} is disabled for {NotificationAgent.Mattermost}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - AddOtherInformation(model, notification, parsed); - await Send(notification, settings); + await Run(model, settings, NotificationType.Issue); } protected override async Task IssueComment(NotificationOptions model, MattermostNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Mattermost, NotificationType.IssueComment, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Mattermost}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - AddOtherInformation(model, notification, parsed); - await Send(notification, settings); + await Run(model, settings, NotificationType.IssueComment); } protected override async Task IssueResolved(NotificationOptions model, MattermostNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Mattermost, NotificationType.IssueResolved, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.IssueResolved} is disabled for {NotificationAgent.Mattermost}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - AddOtherInformation(model, notification, parsed); - await Send(notification, settings); + await Run(model, settings, NotificationType.IssueResolved); } protected override async Task AddedToRequestQueue(NotificationOptions model, MattermostNotificationSettings settings) { - 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", image); - await Send(notification, settings); + await Run(model, settings, NotificationType.ItemAddedToFaultQueue); } protected override async Task RequestDeclined(NotificationOptions model, MattermostNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Mattermost, NotificationType.RequestDeclined, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestDeclined} is disabled for {NotificationAgent.Mattermost}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - AddOtherInformation(model, notification, parsed); - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestDeclined); } protected override async Task RequestApproved(NotificationOptions model, MattermostNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Mattermost, NotificationType.RequestApproved, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestApproved} is disabled for {NotificationAgent.Mattermost}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - - AddOtherInformation(model, notification, parsed); - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestApproved); } protected override async Task AvailableRequest(NotificationOptions model, MattermostNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Mattermost, NotificationType.RequestAvailable, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestAvailable} is disabled for {NotificationAgent.Mattermost}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - AddOtherInformation(model, notification, parsed); - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestAvailable); } protected override async Task Send(NotificationMessage model, MattermostNotificationSettings settings) @@ -228,5 +127,21 @@ namespace Ombi.Notifications.Agents }; await Send(notification, settings); } + + private async Task Run(NotificationOptions model, MattermostNotificationSettings settings, NotificationType type) + { + var parsed = await LoadTemplate(NotificationAgent.Mattermost, type, model); + if (parsed.Disabled) + { + Logger.LogInformation($"Template {type} is disabled for {NotificationAgent.Mattermost}"); + return; + } + var notification = new NotificationMessage + { + Message = parsed.Message, + }; + AddOtherInformation(model, notification, parsed); + await Send(notification, settings); + } } } diff --git a/src/Ombi.Notifications/Agents/MobileNotification.cs b/src/Ombi.Notifications/Agents/MobileNotification.cs index de904ecfd..4e3e55fc4 100644 --- a/src/Ombi.Notifications/Agents/MobileNotification.cs +++ b/src/Ombi.Notifications/Agents/MobileNotification.cs @@ -130,23 +130,18 @@ namespace Ombi.Notifications.Agents protected override async Task AddedToRequestQueue(NotificationOptions model, MobileNotificationSettings settings) { - string user; - string title; - if (model.RequestType == RequestType.Movie) - { - user = MovieRequest.RequestedUser.UserAlias; - title = MovieRequest.Title; - } - else + + var parsed = await LoadTemplate(NotificationAgent.Mobile, NotificationType.ItemAddedToFaultQueue, model); + if (parsed.Disabled) { - user = TvRequest.RequestedUser.UserAlias; - title = TvRequest.ParentRequest.Title; + _logger.LogInformation($"Template {NotificationType.ItemAddedToFaultQueue} is disabled for {NotificationAgent.Mobile}"); + return; } - 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 + Message = parsed.Message, }; + // Get admin devices var playerIds = await GetAdmins(NotificationType.Test); await Send(playerIds, notification, settings, model); @@ -294,6 +289,5 @@ namespace Ombi.Notifications.Agents } } } - } } \ No newline at end of file diff --git a/src/Ombi.Notifications/Agents/PushbulletNotification.cs b/src/Ombi.Notifications/Agents/PushbulletNotification.cs index 8440ad319..91a8120b2 100644 --- a/src/Ombi.Notifications/Agents/PushbulletNotification.cs +++ b/src/Ombi.Notifications/Agents/PushbulletNotification.cs @@ -44,131 +44,43 @@ namespace Ombi.Notifications.Agents protected override async Task NewRequest(NotificationOptions model, PushbulletSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushbullet, NotificationType.NewRequest, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.NewRequest} is disabled for {NotificationAgent.Pushbullet}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - - await Send(notification, settings); + await Run(model, settings, NotificationType.NewRequest); } + protected override async Task NewIssue(NotificationOptions model, PushbulletSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushbullet, NotificationType.Issue, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.Issue} is disabled for {NotificationAgent.Pushbullet}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.Issue); } protected override async Task IssueComment(NotificationOptions model, PushbulletSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushbullet, NotificationType.IssueComment, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Pushbullet}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.IssueComment); } protected override async Task IssueResolved(NotificationOptions model, PushbulletSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushbullet, NotificationType.IssueResolved, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.IssueResolved} is disabled for {NotificationAgent.Pushbullet}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.IssueResolved); } protected override async Task AddedToRequestQueue(NotificationOptions model, PushbulletSettings settings) { - string user; - string title; - if (model.RequestType == RequestType.Movie) - { - user = MovieRequest.RequestedUser.UserAlias; - title = MovieRequest.Title; - } - else - { - user = TvRequest.RequestedUser.UserAlias; - title = TvRequest.ParentRequest.Title; - } - 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 - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.ItemAddedToFaultQueue); } protected override async Task RequestDeclined(NotificationOptions model, PushbulletSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushbullet, NotificationType.RequestDeclined, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestDeclined} is disabled for {NotificationAgent.Pushbullet}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestDeclined); } protected override async Task RequestApproved(NotificationOptions model, PushbulletSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushbullet, NotificationType.RequestApproved, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestApproved} is disabled for {NotificationAgent.Pushbullet}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestApproved); } protected override async Task AvailableRequest(NotificationOptions model, PushbulletSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushbullet, NotificationType.RequestAvailable, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestAvailable} is disabled for {NotificationAgent.Pushbullet}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestAvailable); } protected override async Task Send(NotificationMessage model, PushbulletSettings settings) @@ -192,5 +104,22 @@ namespace Ombi.Notifications.Agents }; await Send(notification, settings); } + + private async Task Run(NotificationOptions model, PushbulletSettings settings, NotificationType type) + { + var parsed = await LoadTemplate(NotificationAgent.Pushbullet, type, model); + if (parsed.Disabled) + { + Logger.LogInformation($"Template {type} is disabled for {NotificationAgent.Pushbullet}"); + return; + } + + var notification = new NotificationMessage + { + Message = parsed.Message, + }; + + await Send(notification, settings); + } } } diff --git a/src/Ombi.Notifications/Agents/PushoverNotification.cs b/src/Ombi.Notifications/Agents/PushoverNotification.cs index 0d1675f82..d41e6d911 100644 --- a/src/Ombi.Notifications/Agents/PushoverNotification.cs +++ b/src/Ombi.Notifications/Agents/PushoverNotification.cs @@ -45,132 +45,42 @@ namespace Ombi.Notifications.Agents protected override async Task NewRequest(NotificationOptions model, PushoverSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushover, NotificationType.NewRequest, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.NewRequest} is disabled for {NotificationAgent.Pushover}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - - await Send(notification, settings); + await Run(model, settings, NotificationType.NewRequest); } protected override async Task NewIssue(NotificationOptions model, PushoverSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushover, NotificationType.Issue, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.Issue} is disabled for {NotificationAgent.Pushover}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.Issue); } protected override async Task IssueComment(NotificationOptions model, PushoverSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushover, NotificationType.IssueComment, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Pushover}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.IssueComment); } protected override async Task IssueResolved(NotificationOptions model, PushoverSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushover, NotificationType.IssueResolved, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.IssueResolved} is disabled for {NotificationAgent.Pushover}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.IssueResolved); } protected override async Task AddedToRequestQueue(NotificationOptions model, PushoverSettings settings) { - string user; - string title; - if (model.RequestType == RequestType.Movie) - { - user = MovieRequest.RequestedUser.UserAlias; - title = MovieRequest.Title; - } - else - { - user = TvRequest.RequestedUser.UserAlias; - title = TvRequest.ParentRequest.Title; - } - 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 - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.ItemAddedToFaultQueue); } protected override async Task RequestDeclined(NotificationOptions model, PushoverSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushover, NotificationType.RequestDeclined, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestDeclined} is disabled for {NotificationAgent.Pushover}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestDeclined); } protected override async Task RequestApproved(NotificationOptions model, PushoverSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushover, NotificationType.RequestApproved, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestApproved} is disabled for {NotificationAgent.Pushover}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestApproved); } protected override async Task AvailableRequest(NotificationOptions model, PushoverSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Pushover, NotificationType.RequestAvailable, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestAvailable} is disabled for {NotificationAgent.Pushover}"); - return; - } - - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestAvailable); } protected override async Task Send(NotificationMessage model, PushoverSettings settings) @@ -195,5 +105,21 @@ namespace Ombi.Notifications.Agents }; await Send(notification, settings); } + + private async Task Run(NotificationOptions model, PushoverSettings settings, NotificationType type) + { + var parsed = await LoadTemplate(NotificationAgent.Pushover, type, model); + if (parsed.Disabled) + { + Logger.LogInformation($"Template {type} is disabled for {NotificationAgent.Pushover}"); + return; + } + + var notification = new NotificationMessage + { + Message = parsed.Message, + }; + await Send(notification, settings); + } } } diff --git a/src/Ombi.Notifications/Agents/SlackNotification.cs b/src/Ombi.Notifications/Agents/SlackNotification.cs index fdbd7ba64..58b2da651 100644 --- a/src/Ombi.Notifications/Agents/SlackNotification.cs +++ b/src/Ombi.Notifications/Agents/SlackNotification.cs @@ -54,138 +54,42 @@ namespace Ombi.Notifications.Agents protected override async Task NewRequest(NotificationOptions model, SlackNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Slack, NotificationType.NewRequest, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.NewRequest} is disabled for {NotificationAgent.Slack}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.NewRequest); } protected override async Task NewIssue(NotificationOptions model, SlackNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Slack, NotificationType.Issue, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.Issue} is disabled for {NotificationAgent.Slack}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.Issue); } protected override async Task IssueComment(NotificationOptions model, SlackNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Slack, NotificationType.IssueComment, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Slack}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.IssueComment); } protected override async Task IssueResolved(NotificationOptions model, SlackNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Slack, NotificationType.IssueResolved, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.IssueResolved} is disabled for {NotificationAgent.Slack}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.IssueResolved); } protected override async Task AddedToRequestQueue(NotificationOptions model, SlackNotificationSettings settings) { - var user = string.Empty; - var title = string.Empty; - if (model.RequestType == RequestType.Movie) - { - user = MovieRequest.RequestedUser.UserAlias; - title = MovieRequest.Title; - } - else - { - user = TvRequest.RequestedUser.UserAlias; - title = TvRequest.ParentRequest.Title; - } - 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 - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.ItemAddedToFaultQueue); } protected override async Task RequestDeclined(NotificationOptions model, SlackNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Slack, NotificationType.RequestDeclined, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestDeclined} is disabled for {NotificationAgent.Slack}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestAvailable); } protected override async Task RequestApproved(NotificationOptions model, SlackNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Slack, NotificationType.RequestApproved, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestApproved} is disabled for {NotificationAgent.Slack}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestApproved); } protected override async Task AvailableRequest(NotificationOptions model, SlackNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Slack, NotificationType.RequestAvailable, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestAvailable} is disabled for {NotificationAgent.Slack}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestAvailable); } protected override async Task Send(NotificationMessage model, SlackNotificationSettings settings) @@ -218,5 +122,21 @@ namespace Ombi.Notifications.Agents }; await Send(notification, settings); } + + private async Task Run(NotificationOptions model, SlackNotificationSettings settings, NotificationType type) + { + var parsed = await LoadTemplate(NotificationAgent.Slack, type, model); + if (parsed.Disabled) + { + Logger.LogInformation($"Template {type} is disabled for {NotificationAgent.Slack}"); + return; + } + var notification = new NotificationMessage + { + Message = parsed.Message, + }; + notification.Other.Add("image", parsed.Image); + await Send(notification, settings); + } } } diff --git a/src/Ombi.Notifications/Agents/TelegramNotification.cs b/src/Ombi.Notifications/Agents/TelegramNotification.cs index 8e4c4e898..3acfc7331 100644 --- a/src/Ombi.Notifications/Agents/TelegramNotification.cs +++ b/src/Ombi.Notifications/Agents/TelegramNotification.cs @@ -41,134 +41,42 @@ namespace Ombi.Notifications.Agents protected override async Task NewRequest(NotificationOptions model, TelegramSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Telegram, NotificationType.NewRequest, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.NewRequest} is disabled for {NotificationAgent.Telegram}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - - await Send(notification, settings); + await Run(model, settings, NotificationType.NewRequest); } protected override async Task NewIssue(NotificationOptions model, TelegramSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Telegram, NotificationType.Issue, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.Issue} is disabled for {NotificationAgent.Telegram}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.Issue); } protected override async Task IssueComment(NotificationOptions model, TelegramSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Telegram, NotificationType.IssueComment, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Telegram}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.IssueComment); } protected override async Task IssueResolved(NotificationOptions model, TelegramSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Telegram, NotificationType.IssueResolved, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.IssueResolved} is disabled for {NotificationAgent.Telegram}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.IssueResolved); } protected override async Task AddedToRequestQueue(NotificationOptions model, TelegramSettings settings) { - 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 - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.ItemAddedToFaultQueue); } protected override async Task RequestDeclined(NotificationOptions model, TelegramSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Telegram, NotificationType.RequestDeclined, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestDeclined} is disabled for {NotificationAgent.Telegram}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestDeclined); } protected override async Task RequestApproved(NotificationOptions model, TelegramSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Telegram, NotificationType.RequestApproved, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestApproved} is disabled for {NotificationAgent.Telegram}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message ?? string.Empty, - }; - - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestApproved); } protected override async Task AvailableRequest(NotificationOptions model, TelegramSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Telegram, NotificationType.RequestAvailable, model); - if (parsed.Disabled) - { - Logger.LogInformation($"Template {NotificationType.RequestAvailable} is disabled for {NotificationAgent.Telegram}"); - return; - } - var notification = new NotificationMessage - { - Message = parsed.Message, - }; - await Send(notification, settings); + await Run(model, settings, NotificationType.RequestAvailable); } protected override async Task Send(NotificationMessage model, TelegramSettings settings) @@ -192,5 +100,20 @@ namespace Ombi.Notifications.Agents }; await Send(notification, settings); } + + private async Task Run(NotificationOptions model, TelegramSettings settings, NotificationType type) + { + var parsed = await LoadTemplate(NotificationAgent.Telegram, type, model); + if (parsed.Disabled) + { + Logger.LogInformation($"Template {type} is disabled for {NotificationAgent.Telegram}"); + return; + } + var notification = new NotificationMessage + { + Message = parsed.Message, + }; + await Send(notification, settings); + } } } diff --git a/src/Ombi.Store/Context/OmbiContext.cs b/src/Ombi.Store/Context/OmbiContext.cs index 3d8c05daa..bb84f1272 100644 --- a/src/Ombi.Store/Context/OmbiContext.cs +++ b/src/Ombi.Store/Context/OmbiContext.cs @@ -209,7 +209,15 @@ namespace Ombi.Store.Context }; break; case NotificationType.ItemAddedToFaultQueue: - continue; + notificationToAdd = new NotificationTemplates + { + NotificationType = notificationType, + Message = "Hello! The user '{UserName}' has requested {Title} but it could not be added. This has been added into the requests queue and will keep retrying", + Subject = "Item Added To Retry Queue", + Agent = agent, + Enabled = true, + }; + break; case NotificationType.WelcomeEmail: notificationToAdd = new NotificationTemplates {