From d7b49310306bf5b9b48a9ff9cb3a054111a404cd Mon Sep 17 00:00:00 2001 From: tidusjar Date: Tue, 1 Oct 2019 08:48:58 +0100 Subject: [PATCH] Fix for #3183 --- .../Agents/MobileNotification.cs | 35 ++++++++++++++++--- src/Ombi/Controllers/IssuesController.cs | 1 + 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Ombi.Notifications/Agents/MobileNotification.cs b/src/Ombi.Notifications/Agents/MobileNotification.cs index 4e3e55fc4..363029190 100644 --- a/src/Ombi.Notifications/Agents/MobileNotification.cs +++ b/src/Ombi.Notifications/Agents/MobileNotification.cs @@ -12,6 +12,7 @@ using Ombi.Notifications.Models; using Ombi.Settings.Settings.Models; using Ombi.Settings.Settings.Models.Notifications; using Ombi.Store.Entities; +using Ombi.Store.Entities.Requests; using Ombi.Store.Repository; using Ombi.Store.Repository.Requests; @@ -21,13 +22,14 @@ namespace Ombi.Notifications.Agents { public MobileNotification(IOneSignalApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s, IRepository notification, - UserManager um, IRepository sub, IMusicRequestRepository music, + UserManager um, IRepository sub, IMusicRequestRepository music, IRepository issueRepository, IRepository userPref) : base(sn, r, m, t, s, log, sub, music, userPref) { _api = api; _logger = log; _notifications = notification; _userManager = um; + _issueRepository = issueRepository; } public override string NotificationName => "MobileNotification"; @@ -36,6 +38,7 @@ namespace Ombi.Notifications.Agents private readonly ILogger _logger; private readonly IRepository _notifications; private readonly UserManager _userManager; + private readonly IRepository _issueRepository; protected override bool ValidateConfiguration(MobileNotificationSettings settings) { @@ -95,8 +98,9 @@ namespace Ombi.Notifications.Agents var isAdmin = bool.Parse(isAdminString); if (isAdmin) { + model.Substitutes.TryGetValue("IssueId", out var issueId); // Send to user - var playerIds = GetUsers(model, NotificationType.IssueComment); + var playerIds = await GetUsersForIssue(model, int.Parse(issueId), NotificationType.IssueComment); await Send(playerIds, notification, settings, model); } else @@ -250,6 +254,7 @@ namespace Ombi.Notifications.Agents } return playerIds; } + private List GetUsers(NotificationOptions model, NotificationType type) { var notificationIds = new List(); @@ -261,14 +266,36 @@ namespace Ombi.Notifications.Agents } if (model.UserId.HasValue() && (!notificationIds?.Any() ?? true)) { - var user= _userManager.Users.Include(x => x.NotificationUserIds).FirstOrDefault(x => x.Id == model.UserId); + var user = _userManager.Users.Include(x => x.NotificationUserIds).FirstOrDefault(x => x.Id == model.UserId); notificationIds = user.NotificationUserIds; } if (!notificationIds?.Any() ?? true) { _logger.LogInformation( - $"there are no admins to send a notification for {type}, for agent {NotificationAgent.Mobile}"); + $"there are no users to send a notification for {type}, for agent {NotificationAgent.Mobile}"); + return null; + } + var playerIds = notificationIds.Select(x => x.PlayerId).ToList(); + return playerIds; + } + + private async Task> GetUsersForIssue(NotificationOptions model, int issueId, NotificationType type) + { + var notificationIds = new List(); + + var issue = await _issueRepository.GetAll() + .FirstOrDefaultAsync(x => x.Id == issueId); + + // Get the user that raised the issue to send the notification to + var userRaised = await _userManager.Users.Include(x => x.NotificationUserIds).FirstOrDefaultAsync(x => x.Id == issue.UserReportedId); + + notificationIds = userRaised.NotificationUserIds; + + if (!notificationIds?.Any() ?? true) + { + _logger.LogInformation( + $"there are no users to send a notification for {type}, for agent {NotificationAgent.Mobile}"); return null; } var playerIds = notificationIds.Select(x => x.PlayerId).ToList(); diff --git a/src/Ombi/Controllers/IssuesController.cs b/src/Ombi/Controllers/IssuesController.cs index 9e28ccdc3..3c8e9c719 100644 --- a/src/Ombi/Controllers/IssuesController.cs +++ b/src/Ombi/Controllers/IssuesController.cs @@ -226,6 +226,7 @@ namespace Ombi.Controllers var isAdmin = await _userManager.IsInRoleAsync(user, OmbiRoles.Admin) || user.IsSystemUser; AddIssueNotificationSubstitutes(notificationModel, issue, issue.UserReported.UserAlias); notificationModel.Substitutes.Add("NewIssueComment", comment.Comment); + notificationModel.Substitutes.Add("IssueId", comment.IssueId.ToString()); notificationModel.Substitutes.Add("AdminComment", isAdmin.ToString()); if (isAdmin)