From 480a107fa699af2ea4364fe41657d710a2e7f493 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Wed, 7 Feb 2018 23:30:23 +0000 Subject: [PATCH] Fixed #1914 Also added new notification variables and a new notification when someone adds a comment on an issue. --- src/Ombi.Helpers/NotificationType.cs | 1 + .../Agents/DiscordNotification.cs | 16 ++++ .../Agents/EmailNotification.cs | 44 +++++++++- .../Agents/MattermostNotification.cs | 16 ++++ .../Agents/MobileNotification.cs | 46 +++++++++- .../Agents/PushbulletNotification.cs | 15 ++++ .../Agents/PushoverNotification.cs | 15 ++++ .../Agents/SlackNotification.cs | 16 ++++ .../Agents/TelegramNotification.cs | 15 ++++ .../Interfaces/BaseNotification.cs | 4 + .../Models/NotificationOptions.cs | 4 + .../NotificationMessageCurlys.cs | 85 ++++++++++++++----- src/Ombi.Store/Context/OmbiContext.cs | 11 +++ .../Repository/PlexContentRepository.cs | 6 +- src/Ombi/Controllers/IssuesController.cs | 70 ++++++++++++--- 15 files changed, 322 insertions(+), 42 deletions(-) diff --git a/src/Ombi.Helpers/NotificationType.cs b/src/Ombi.Helpers/NotificationType.cs index 9c97137fb..d6466096c 100644 --- a/src/Ombi.Helpers/NotificationType.cs +++ b/src/Ombi.Helpers/NotificationType.cs @@ -12,5 +12,6 @@ ItemAddedToFaultQueue = 7, WelcomeEmail = 8, IssueResolved = 9, + IssueComment = 10, } } diff --git a/src/Ombi.Notifications/Agents/DiscordNotification.cs b/src/Ombi.Notifications/Agents/DiscordNotification.cs index 918c49940..0a5d5dabc 100644 --- a/src/Ombi.Notifications/Agents/DiscordNotification.cs +++ b/src/Ombi.Notifications/Agents/DiscordNotification.cs @@ -87,6 +87,22 @@ namespace Ombi.Notifications.Agents await Send(notification, settings); } + 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); + } + protected override async Task IssueResolved(NotificationOptions model, DiscordNotificationSettings settings) { var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.IssueResolved, model); diff --git a/src/Ombi.Notifications/Agents/EmailNotification.cs b/src/Ombi.Notifications/Agents/EmailNotification.cs index 9ca2f3470..6559f17ce 100644 --- a/src/Ombi.Notifications/Agents/EmailNotification.cs +++ b/src/Ombi.Notifications/Agents/EmailNotification.cs @@ -1,6 +1,8 @@ using System; +using System.Linq; using System.Threading.Tasks; using MailKit.Net.Smtp; +using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Logging; using MimeKit; using Ombi.Core.Settings; @@ -19,14 +21,16 @@ namespace Ombi.Notifications.Agents public class EmailNotification : BaseNotification, IEmailNotification { public EmailNotification(ISettingsService settings, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, IEmailProvider prov, ISettingsService c, - ILogger log) : base(settings, r, m, t, c,log) + ILogger log, UserManager um) : base(settings, r, m, t, c) { EmailProvider = prov; Logger = log; + _userManager = um; } private IEmailProvider EmailProvider { get; } private ILogger Logger { get; } public override string NotificationName => nameof(EmailNotification); + private readonly UserManager _userManager; protected override bool ValidateConfiguration(EmailNotificationSettings settings) { @@ -65,9 +69,24 @@ namespace Ombi.Notifications.Agents { Message = html, Subject = parsed.Subject, - To = model.Recipient.HasValue() ? model.Recipient : settings.AdminEmail, }; + if (model.Substitutes.TryGetValue("AdminComment", out var isAdminString)) + { + var isAdmin = bool.Parse(isAdminString); + if (isAdmin) + { + var user = _userManager.Users.FirstOrDefault(x => x.Id == model.UserId); + // Send to user + message.To = user.Email; + } + else + { + // Send to admin + message.To = settings.AdminEmail; + } + } + return message; } @@ -109,6 +128,27 @@ namespace Ombi.Notifications.Agents await Send(message, settings); } + protected override async Task IssueComment(NotificationOptions model, EmailNotificationSettings settings) + { + var message = await LoadTemplate(NotificationType.IssueComment, model, settings); + if (message == null) + { + return; + } + + var plaintext = await LoadPlainTextMessage(NotificationType.IssueComment, model, settings); + message.Other.Add("PlainTextBody", plaintext); + + if (model.Substitutes.TryGetValue("AdminComment", out var isAdminString)) + { + var isAdmin = bool.Parse(isAdminString); + message.To = isAdmin ? model.Recipient : settings.AdminEmail; + } + + + await Send(message, settings); + } + protected override async Task IssueResolved(NotificationOptions model, EmailNotificationSettings settings) { var message = await LoadTemplate(NotificationType.IssueResolved, model, settings); diff --git a/src/Ombi.Notifications/Agents/MattermostNotification.cs b/src/Ombi.Notifications/Agents/MattermostNotification.cs index 5b55c95d5..efc1df1a6 100644 --- a/src/Ombi.Notifications/Agents/MattermostNotification.cs +++ b/src/Ombi.Notifications/Agents/MattermostNotification.cs @@ -79,6 +79,22 @@ namespace Ombi.Notifications.Agents await Send(notification, settings); } + 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, + }; + notification.Other.Add("image", parsed.Image); + await Send(notification, settings); + } + protected override async Task IssueResolved(NotificationOptions model, MattermostNotificationSettings settings) { var parsed = await LoadTemplate(NotificationAgent.Mattermost, NotificationType.IssueResolved, model); diff --git a/src/Ombi.Notifications/Agents/MobileNotification.cs b/src/Ombi.Notifications/Agents/MobileNotification.cs index fb84fe57f..05c02dfef 100644 --- a/src/Ombi.Notifications/Agents/MobileNotification.cs +++ b/src/Ombi.Notifications/Agents/MobileNotification.cs @@ -78,6 +78,36 @@ namespace Ombi.Notifications.Agents await Send(playerIds, notification, settings); } + protected override async Task IssueComment(NotificationOptions model, MobileNotificationSettings settings) + { + var parsed = await LoadTemplate(NotificationAgent.Mobile, NotificationType.IssueComment, model); + if (parsed.Disabled) + { + _logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Mobile}"); + return; + } + var notification = new NotificationMessage + { + Message = parsed.Message, + }; + if (model.Substitutes.TryGetValue("AdminComment", out var isAdminString)) + { + var isAdmin = bool.Parse(isAdminString); + if (isAdmin) + { + // Send to user + var playerIds = GetUsers(model, NotificationType.IssueComment); + await Send(playerIds, notification, settings); + } + else + { + // Send to admin + var playerIds = await GetAdmins(NotificationType.IssueComment); + await Send(playerIds, notification, settings); + } + } + } + protected override async Task IssueResolved(NotificationOptions model, MobileNotificationSettings settings) { var parsed = await LoadTemplate(NotificationAgent.Mobile, NotificationType.IssueResolved, model); @@ -216,9 +246,19 @@ namespace Ombi.Notifications.Agents } private List GetUsers(NotificationOptions model, NotificationType type) { - var notificationIds = model.RequestType == RequestType.Movie - ? MovieRequest.RequestedUser.NotificationUserIds - : TvRequest.RequestedUser.NotificationUserIds; + var notificationIds = new List(); + if (MovieRequest != null || TvRequest != null) + { + notificationIds = model.RequestType == RequestType.Movie + ? MovieRequest?.RequestedUser?.NotificationUserIds + : TvRequest?.RequestedUser?.NotificationUserIds; + } + if (model.UserId.HasValue() && !notificationIds.Any()) + { + var user= _userManager.Users.Include(x => x.NotificationUserIds).FirstOrDefault(x => x.Id == model.UserId); + notificationIds = user.NotificationUserIds; + } + if (!notificationIds.Any()) { _logger.LogInformation( diff --git a/src/Ombi.Notifications/Agents/PushbulletNotification.cs b/src/Ombi.Notifications/Agents/PushbulletNotification.cs index 11e69563a..21a384db6 100644 --- a/src/Ombi.Notifications/Agents/PushbulletNotification.cs +++ b/src/Ombi.Notifications/Agents/PushbulletNotification.cs @@ -73,6 +73,21 @@ namespace Ombi.Notifications.Agents await Send(notification, settings); } + 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); + } + protected override async Task IssueResolved(NotificationOptions model, PushbulletSettings settings) { var parsed = await LoadTemplate(NotificationAgent.Pushbullet, NotificationType.IssueResolved, model); diff --git a/src/Ombi.Notifications/Agents/PushoverNotification.cs b/src/Ombi.Notifications/Agents/PushoverNotification.cs index ae1a860a9..505adc44e 100644 --- a/src/Ombi.Notifications/Agents/PushoverNotification.cs +++ b/src/Ombi.Notifications/Agents/PushoverNotification.cs @@ -74,6 +74,21 @@ namespace Ombi.Notifications.Agents await Send(notification, settings); } + 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); + } + protected override async Task IssueResolved(NotificationOptions model, PushoverSettings settings) { var parsed = await LoadTemplate(NotificationAgent.Pushover, NotificationType.IssueResolved, model); diff --git a/src/Ombi.Notifications/Agents/SlackNotification.cs b/src/Ombi.Notifications/Agents/SlackNotification.cs index 1cb41e1a4..d723c065a 100644 --- a/src/Ombi.Notifications/Agents/SlackNotification.cs +++ b/src/Ombi.Notifications/Agents/SlackNotification.cs @@ -85,6 +85,22 @@ namespace Ombi.Notifications.Agents await Send(notification, settings); } + 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); + } + protected override async Task IssueResolved(NotificationOptions model, SlackNotificationSettings settings) { var parsed = await LoadTemplate(NotificationAgent.Slack, NotificationType.IssueResolved, model); diff --git a/src/Ombi.Notifications/Agents/TelegramNotification.cs b/src/Ombi.Notifications/Agents/TelegramNotification.cs index 1ccc0f0eb..921fd3ad2 100644 --- a/src/Ombi.Notifications/Agents/TelegramNotification.cs +++ b/src/Ombi.Notifications/Agents/TelegramNotification.cs @@ -69,6 +69,21 @@ namespace Ombi.Notifications.Agents await Send(notification, settings); } + 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); + } + protected override async Task IssueResolved(NotificationOptions model, TelegramSettings settings) { var parsed = await LoadTemplate(NotificationAgent.Telegram, NotificationType.IssueResolved, model); diff --git a/src/Ombi.Notifications/Interfaces/BaseNotification.cs b/src/Ombi.Notifications/Interfaces/BaseNotification.cs index be07e36c5..f21d3fb12 100644 --- a/src/Ombi.Notifications/Interfaces/BaseNotification.cs +++ b/src/Ombi.Notifications/Interfaces/BaseNotification.cs @@ -99,6 +99,9 @@ namespace Ombi.Notifications.Interfaces case NotificationType.IssueResolved: await IssueResolved(model, notificationSettings); break; + case NotificationType.IssueComment: + await IssueComment(model, notificationSettings); + break; default: throw new ArgumentOutOfRangeException(); } @@ -180,6 +183,7 @@ namespace Ombi.Notifications.Interfaces protected abstract bool ValidateConfiguration(T settings); protected abstract Task NewRequest(NotificationOptions model, T settings); protected abstract Task NewIssue(NotificationOptions model, T settings); + protected abstract Task IssueComment(NotificationOptions model, T settings); protected abstract Task IssueResolved(NotificationOptions model, T settings); protected abstract Task AddedToRequestQueue(NotificationOptions model, T settings); protected abstract Task RequestDeclined(NotificationOptions model, T settings); diff --git a/src/Ombi.Notifications/Models/NotificationOptions.cs b/src/Ombi.Notifications/Models/NotificationOptions.cs index 80d619083..bc04218f7 100644 --- a/src/Ombi.Notifications/Models/NotificationOptions.cs +++ b/src/Ombi.Notifications/Models/NotificationOptions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Ombi.Helpers; using Ombi.Store; using Ombi.Store.Entities; @@ -13,6 +14,9 @@ namespace Ombi.Notifications.Models public RequestType RequestType { get; set; } public string Recipient { get; set; } public string AdditionalInformation { get; set; } + public string UserId { get; set; } + + public Dictionary Substitutes { get; set; } = new Dictionary(); } } \ No newline at end of file diff --git a/src/Ombi.Notifications/NotificationMessageCurlys.cs b/src/Ombi.Notifications/NotificationMessageCurlys.cs index 94a345261..3b9eee236 100644 --- a/src/Ombi.Notifications/NotificationMessageCurlys.cs +++ b/src/Ombi.Notifications/NotificationMessageCurlys.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using Microsoft.Extensions.Logging; using Ombi.Helpers; using Ombi.Notifications.Models; using Ombi.Settings.Settings.Models; @@ -11,38 +10,58 @@ namespace Ombi.Notifications { public class NotificationMessageCurlys { + public void Setup(NotificationOptions opts, FullBaseRequest req, CustomizationSettings s) { + LoadIssues(opts); + string title; + if (req == null) + { + opts.Substitutes.TryGetValue("Title", out title); + } + else + { + title = req?.Title; + } ApplicationUrl = (s?.ApplicationUrl.HasValue() ?? false) ? s.ApplicationUrl : string.Empty; ApplicationName = string.IsNullOrEmpty(s?.ApplicationName) ? "Ombi" : s?.ApplicationName; - 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.RequestType == RequestType.Movie ? - $"https://image.tmdb.org/t/p/w300{req.PosterPath}" : req.PosterPath; + RequestedUser = string.IsNullOrEmpty(req?.RequestedUser?.Alias) + ? req?.RequestedUser?.UserName + : req?.RequestedUser?.Alias; + Title = title; + RequestedDate = req?.RequestedDate.ToString("D"); + Type = req?.RequestType.ToString(); + Overview = req?.Overview; + Year = req?.ReleaseDate.Year.ToString(); + PosterImage = req?.RequestType == RequestType.Movie ? + string.Format("https://image.tmdb.org/t/p/w300{0}", req?.PosterPath) : req?.PosterPath; AdditionalInformation = opts?.AdditionalInformation ?? string.Empty; } public void Setup(NotificationOptions opts, ChildRequests req, CustomizationSettings s) { - - ApplicationUrl = (s?.ApplicationUrl.HasValue() ?? false) ? s.ApplicationUrl : string.Empty; + LoadIssues(opts); + string title; + if (req == null) + { + opts.Substitutes.TryGetValue("Title", out title); + } + else + { + title = req?.ParentRequest.Title; + } + ApplicationUrl = (s?.ApplicationUrl.HasValue() ?? false) ? s.ApplicationUrl : string.Empty; ApplicationName = string.IsNullOrEmpty(s?.ApplicationName) ? "Ombi" : s?.ApplicationName; - 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.RequestType == RequestType.Movie ? - $"https://image.tmdb.org/t/p/w300{req.ParentRequest.PosterPath}" : req.ParentRequest.PosterPath; + RequestedUser = string.IsNullOrEmpty(req?.RequestedUser.Alias) + ? req?.RequestedUser.UserName + : req?.RequestedUser.Alias; + Title = title; + RequestedDate = req?.RequestedDate.ToString("D"); + Type = req?.RequestType.ToString(); + Overview = req?.ParentRequest.Overview; + Year = req?.ParentRequest.ReleaseDate.Year.ToString(); + PosterImage = req?.RequestType == RequestType.Movie ? + $"https://image.tmdb.org/t/p/w300{req?.ParentRequest.PosterPath}" : req?.ParentRequest.PosterPath; AdditionalInformation = opts.AdditionalInformation; // DO Episode and Season Lists } @@ -54,6 +73,16 @@ namespace Ombi.Notifications RequestedUser = user.UserName; } + private void LoadIssues(NotificationOptions opts) + { + var val = string.Empty; + IssueDescription = opts.Substitutes.TryGetValue("IssueDescription", out val) ? val : string.Empty; + IssueCategory = opts.Substitutes.TryGetValue("IssueCategory", out val) ? val : string.Empty; + IssueStatus = opts.Substitutes.TryGetValue("IssueStatus", out val) ? val : string.Empty; + IssueSubject = opts.Substitutes.TryGetValue("IssueSubject", out val) ? val : string.Empty; + NewIssueComment = opts.Substitutes.TryGetValue("NewIssueComment", out val) ? val : string.Empty; + } + // User Defined public string RequestedUser { get; set; } public string Title { get; set; } @@ -67,6 +96,11 @@ namespace Ombi.Notifications public string PosterImage { get; set; } public string ApplicationName { get; set; } public string ApplicationUrl { get; set; } + public string IssueDescription { get; set; } + public string IssueCategory { get; set; } + public string IssueStatus { get; set; } + public string IssueSubject { get; set; } + public string NewIssueComment { get; set; } // System Defined private string LongDate => DateTime.Now.ToString("D"); @@ -92,6 +126,11 @@ namespace Ombi.Notifications {nameof(PosterImage),PosterImage}, {nameof(ApplicationName),ApplicationName}, {nameof(ApplicationUrl),ApplicationUrl}, + {nameof(IssueDescription),IssueDescription}, + {nameof(IssueCategory),IssueCategory}, + {nameof(IssueStatus),IssueStatus}, + {nameof(IssueSubject),IssueSubject}, + {nameof(NewIssueComment),NewIssueComment}, }; } } \ No newline at end of file diff --git a/src/Ombi.Store/Context/OmbiContext.cs b/src/Ombi.Store/Context/OmbiContext.cs index 967347a6c..8470a1743 100644 --- a/src/Ombi.Store/Context/OmbiContext.cs +++ b/src/Ombi.Store/Context/OmbiContext.cs @@ -205,6 +205,17 @@ namespace Ombi.Store.Context Enabled = true, }; break; + + case NotificationType.IssueComment: + notificationToAdd = new NotificationTemplates + { + NotificationType = notificationType, + Message = "Hello, There is a new comment on your issue {IssueSubject}, The comment is: {NewIssueComment}", + Subject = "{ApplicationName}: New comment on issue {IssueSubject}!", + Agent = agent, + Enabled = true, + }; + break; case NotificationType.AdminNote: continue; default: diff --git a/src/Ombi.Store/Repository/PlexContentRepository.cs b/src/Ombi.Store/Repository/PlexContentRepository.cs index 6181395f8..35bf56057 100644 --- a/src/Ombi.Store/Repository/PlexContentRepository.cs +++ b/src/Ombi.Store/Repository/PlexContentRepository.cs @@ -66,7 +66,11 @@ namespace Ombi.Store.Repository var item = await Db.PlexServerContent.FirstOrDefaultAsync(x => x.ImdbId == providerId); if (item == null) { - item = await Db.PlexServerContent.FirstOrDefaultAsync(x => x.TheMovieDbId == providerId) ?? await Db.PlexServerContent.FirstOrDefaultAsync(x => x.TvDbId == providerId); + item = await Db.PlexServerContent.FirstOrDefaultAsync(x => x.TheMovieDbId == providerId); + if (item == null) + { + item = await Db.PlexServerContent.FirstOrDefaultAsync(x => x.TvDbId == providerId); + } } return item; } diff --git a/src/Ombi/Controllers/IssuesController.cs b/src/Ombi/Controllers/IssuesController.cs index 7828be6e4..0d0f38d74 100644 --- a/src/Ombi/Controllers/IssuesController.cs +++ b/src/Ombi/Controllers/IssuesController.cs @@ -136,14 +136,17 @@ namespace Ombi.Controllers var notificationModel = new NotificationOptions { - RequestId = 0, + RequestId = i.RequestId ?? 0, DateTime = DateTime.Now, NotificationType = NotificationType.Issue, RequestType = i.RequestType, Recipient = string.Empty, - AdditionalInformation = $"{i.Subject} | {i.Description}" + AdditionalInformation = $"{i.Subject} | {i.Description}", + UserId = i.UserReportedId }; + AddIssueNotificationSubstitutes(notificationModel, i); + BackgroundJob.Enqueue(() => _notification.Publish(notificationModel)); return i.Id; @@ -190,21 +193,46 @@ namespace Ombi.Controllers [HttpPost("comments")] public async Task AddComment([FromBody] NewIssueCommentViewModel comment) { - var userId = await _userManager.Users.Where(x => User.Identity.Name == x.UserName).Select(x => x.Id) + var user = await _userManager.Users.Where(x => User.Identity.Name == x.UserName) .FirstOrDefaultAsync(); + var issue = await _issues.Find(comment.IssueId ?? 0); + if (issue == null) + { + return null; + } var newComment = new IssueComments { Comment = comment.Comment, Date = DateTime.UtcNow, - UserId = userId, + UserId = user.Id, IssuesId = comment.IssueId, }; + + var notificationModel = new NotificationOptions + { + RequestId = issue.RequestId ?? 0, + DateTime = DateTime.Now, + NotificationType = NotificationType.IssueComment, + RequestType = issue.RequestType, + Recipient = user.Email, + UserId = user.Id + }; + + var isAdmin = await _userManager.IsInRoleAsync(user, OmbiRoles.Admin); + AddIssueNotificationSubstitutes(notificationModel, issue); + notificationModel.Substitutes.Add("NewIssueComment", comment.Comment); + notificationModel.Substitutes.Add("AdminComment", isAdmin.ToString()); + + BackgroundJob.Enqueue(() => _notification.Publish(notificationModel)); + return await _issueComments.Add(newComment); } [HttpPost("status")] public async Task UpdateStatus([FromBody] IssueStateViewModel model) { + var user = await _userManager.Users.Where(x => User.Identity.Name == x.UserName) + .FirstOrDefaultAsync(); var issue = await _issues.Find(model.IssueId); if (issue == null) { @@ -214,20 +242,36 @@ namespace Ombi.Controllers issue.Status = model.Status; await _issues.SaveChangesAsync(); - var notificationModel = new NotificationOptions + if (issue.Status == IssueStatus.Resolved) { - RequestId = 0, - DateTime = DateTime.Now, - NotificationType = NotificationType.Issue, - RequestType = issue.RequestType, - Recipient = !string.IsNullOrEmpty(issue.UserReported?.Email) ? issue.UserReported.Email : string.Empty, - AdditionalInformation = $"{issue.Subject} | {issue.Description}" - }; + var notificationModel = new NotificationOptions + { + RequestId = 0, + DateTime = DateTime.Now, + NotificationType = NotificationType.IssueResolved, + RequestType = issue.RequestType, + Recipient = !string.IsNullOrEmpty(issue.UserReported?.Email) + ? issue.UserReported.Email + : string.Empty, + AdditionalInformation = $"{issue.Subject} | {issue.Description}", + UserId = user.Id + }; + AddIssueNotificationSubstitutes(notificationModel, issue); - BackgroundJob.Enqueue(() => _notification.Publish(notificationModel)); + BackgroundJob.Enqueue(() => _notification.Publish(notificationModel)); + } return true; } + + private static void AddIssueNotificationSubstitutes(NotificationOptions notificationModel, Issues issue) + { + notificationModel.Substitutes.Add("Title", issue.Title); + notificationModel.Substitutes.Add("IssueDescription", issue.Description); + notificationModel.Substitutes.Add("IssueCategory", issue.IssueCategory?.Value); + notificationModel.Substitutes.Add("IssueStatus", issue.Status.ToString()); + notificationModel.Substitutes.Add("IssueSubject", issue.Subject); + } } } \ No newline at end of file