diff --git a/src/Ombi.Notifications/Agents/DiscordNotification.cs b/src/Ombi.Notifications/Agents/DiscordNotification.cs index 0cc0c9b89..ae344116f 100644 --- a/src/Ombi.Notifications/Agents/DiscordNotification.cs +++ b/src/Ombi.Notifications/Agents/DiscordNotification.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Logging; using Ombi.Api.Discord; using Ombi.Api.Discord.Models; @@ -21,8 +22,8 @@ namespace Ombi.Notifications.Agents public DiscordNotification(IDiscordApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s, IRepository sub, IMusicRequestRepository music, - IRepository userPref) - : base(sn, r, m, t, s, log, sub, music, userPref) + IRepository userPref, UserManager um) + : base(sn, r, m, t, s, log, sub, music, userPref, um) { Api = api; Logger = log; diff --git a/src/Ombi.Notifications/Agents/EmailNotification.cs b/src/Ombi.Notifications/Agents/EmailNotification.cs index f41406d0e..cb96fcdb6 100644 --- a/src/Ombi.Notifications/Agents/EmailNotification.cs +++ b/src/Ombi.Notifications/Agents/EmailNotification.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using MailKit.Net.Smtp; @@ -22,7 +23,7 @@ namespace Ombi.Notifications.Agents { public EmailNotification(ISettingsService settings, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, IEmailProvider prov, ISettingsService c, ILogger log, UserManager um, IRepository sub, IMusicRequestRepository music, - IRepository userPref) : base(settings, r, m, t, c, log, sub, music, userPref) + IRepository userPref) : base(settings, r, m, t, c, log, sub, music, userPref, um) { EmailProvider = prov; Logger = log; @@ -114,7 +115,15 @@ namespace Ombi.Notifications.Agents var plaintext = await LoadPlainTextMessage(NotificationType.NewRequest, model, settings); message.Other.Add("PlainTextBody", plaintext); - await Send(message, settings); + foreach (var recipient in (await GetPrivilegedUsers()).DistinctBy(x => x.Email)) + { + if (recipient.Email.IsNullOrEmpty()) + { + continue; + } + message.To = recipient.Email; + await Send(message, settings); + } } protected override async Task NewIssue(NotificationOptions model, EmailNotificationSettings settings) diff --git a/src/Ombi.Notifications/Agents/GotifyNotification.cs b/src/Ombi.Notifications/Agents/GotifyNotification.cs index 00b62c343..e948c4d89 100644 --- a/src/Ombi.Notifications/Agents/GotifyNotification.cs +++ b/src/Ombi.Notifications/Agents/GotifyNotification.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Logging; using Ombi.Api.Gotify; using Ombi.Core.Settings; @@ -17,7 +18,7 @@ namespace Ombi.Notifications.Agents { public GotifyNotification(IGotifyApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s, IRepository sub, IMusicRequestRepository music, - IRepository userPref) : base(sn, r, m, t, s, log, sub, music, userPref) + IRepository userPref, UserManager um) : base(sn, r, m, t, s, log, sub, music, userPref, um) { Api = api; Logger = log; diff --git a/src/Ombi.Notifications/Agents/LegacyMobileNotification.cs b/src/Ombi.Notifications/Agents/LegacyMobileNotification.cs index c2d71e0d0..9df7c6a94 100644 --- a/src/Ombi.Notifications/Agents/LegacyMobileNotification.cs +++ b/src/Ombi.Notifications/Agents/LegacyMobileNotification.cs @@ -23,7 +23,7 @@ namespace Ombi.Notifications.Agents public LegacyMobileNotification(IOneSignalApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s, IRepository notification, UserManager um, IRepository sub, IMusicRequestRepository music, IRepository issueRepository, - IRepository userPref) : base(sn, r, m, t, s, log, sub, music, userPref) + IRepository userPref) : base(sn, r, m, t, s, log, sub, music, userPref, um) { _api = api; _logger = log; @@ -59,7 +59,7 @@ namespace Ombi.Notifications.Agents }; // Get admin devices - var playerIds = await GetAdmins(NotificationType.NewRequest); + var playerIds = await GetPrivilegedUsersPlayerIds(); await Send(playerIds, notification, settings, model, true); } @@ -77,7 +77,7 @@ namespace Ombi.Notifications.Agents }; // Get admin devices - var playerIds = await GetAdmins(NotificationType.Issue); + var playerIds = await GetAdmins(); await Send(playerIds, notification, settings, model); } @@ -106,7 +106,7 @@ namespace Ombi.Notifications.Agents else { // Send to admin - var playerIds = await GetAdmins(NotificationType.IssueComment); + var playerIds = await GetAdmins(); await Send(playerIds, notification, settings, model); } } @@ -147,7 +147,7 @@ namespace Ombi.Notifications.Agents }; // Get admin devices - var playerIds = await GetAdmins(NotificationType.Test); + var playerIds = await GetAdmins(); await Send(playerIds, notification, settings, model); } @@ -241,15 +241,25 @@ namespace Ombi.Notifications.Agents await Send(playerIds, notification, settings, model); } - private async Task> GetAdmins(NotificationType type) + private async Task> GetAdmins() { - var adminUsers = (await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin)).Select(x => x.Id).ToList(); + return await GetNotificationRecipients(await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin)); + } + private async Task> GetPrivilegedUsersPlayerIds() + { + return await GetNotificationRecipients(await GetPrivilegedUsers()); + } + + private async Task> GetNotificationRecipients(IEnumerable users) + { + + var adminUsers = users.Select(x => x.Id).ToList(); var notificationUsers = _notifications.GetAll().Include(x => x.User).Where(x => adminUsers.Contains(x.UserId)); var playerIds = await notificationUsers.Select(x => x.PlayerId).ToListAsync(); if (!playerIds.Any()) { _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 agent {NotificationAgent.Mobile}"); return null; } return playerIds; diff --git a/src/Ombi.Notifications/Agents/MattermostNotification.cs b/src/Ombi.Notifications/Agents/MattermostNotification.cs index 39e87e334..4b5b570c7 100644 --- a/src/Ombi.Notifications/Agents/MattermostNotification.cs +++ b/src/Ombi.Notifications/Agents/MattermostNotification.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Logging; using Ombi.Api.Mattermost; using Ombi.Api.Mattermost.Models; @@ -19,7 +20,7 @@ namespace Ombi.Notifications.Agents { public MattermostNotification(IMattermostApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s, IRepository sub, IMusicRequestRepository music, - IRepository userPref) : base(sn, r, m, t, s, log, sub, music, userPref) + IRepository userPref, UserManager um) : base(sn, r, m, t, s, log, sub, music, userPref, um) { Api = api; Logger = log; diff --git a/src/Ombi.Notifications/Agents/MobileNotification.cs b/src/Ombi.Notifications/Agents/MobileNotification.cs index b7ada4838..0d21f39a5 100644 --- a/src/Ombi.Notifications/Agents/MobileNotification.cs +++ b/src/Ombi.Notifications/Agents/MobileNotification.cs @@ -23,7 +23,7 @@ namespace Ombi.Notifications.Agents public MobileNotification(ICloudMobileNotification api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s, IRepository notification, UserManager um, IRepository sub, IMusicRequestRepository music, IRepository issueRepository, - IRepository userPref) : base(sn, r, m, t, s, log, sub, music, userPref) + IRepository userPref) : base(sn, r, m, t, s, log, sub, music, userPref, um) { _api = api; _logger = log; @@ -62,7 +62,7 @@ namespace Ombi.Notifications.Agents }; // Get admin devices - var playerIds = await GetAdmins(NotificationType.NewRequest); + var playerIds = await GetPrivilegedUsersPlayerIds(); await Send(playerIds, notification, settings, model, true); } @@ -82,7 +82,7 @@ namespace Ombi.Notifications.Agents }; // Get admin devices - var playerIds = await GetAdmins(NotificationType.Issue); + var playerIds = await GetAdmins(); await Send(playerIds, notification, settings, model); } @@ -112,7 +112,7 @@ namespace Ombi.Notifications.Agents else { // Send to admin - var playerIds = await GetAdmins(NotificationType.IssueComment); + var playerIds = await GetAdmins(); await Send(playerIds, notification, settings, model); } } @@ -157,7 +157,7 @@ namespace Ombi.Notifications.Agents }; // Get admin devices - var playerIds = await GetAdmins(NotificationType.Test); + var playerIds = await GetAdmins(); await Send(playerIds, notification, settings, model); } @@ -279,15 +279,25 @@ namespace Ombi.Notifications.Agents await Send(playerIds, notification, settings, model); } - private async Task> GetAdmins(NotificationType type) + private async Task> GetAdmins() { - var adminUsers = (await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin)).Select(x => x.Id).ToList(); + return await GetNotificationRecipients(await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin)); + } + private async Task> GetPrivilegedUsersPlayerIds() + { + return await GetNotificationRecipients(await GetPrivilegedUsers()); + } + + private async Task> GetNotificationRecipients(IEnumerable users) + { + + var adminUsers = users.Select(x => x.Id).ToList(); var notificationUsers = _notifications.GetAll().Include(x => x.User).Where(x => adminUsers.Contains(x.UserId)); var playerIds = await notificationUsers.Select(x => x.Token).ToListAsync(); if (!playerIds.Any()) { _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 agent {NotificationAgent.Mobile}"); return null; } return playerIds; @@ -377,7 +387,7 @@ namespace Ombi.Notifications.Agents }; // Get admin devices - var playerIds = await GetAdmins(NotificationType.PartiallyAvailable); + var playerIds = await GetAdmins(); await Send(playerIds, notification, settings, model, true); } } diff --git a/src/Ombi.Notifications/Agents/PushbulletNotification.cs b/src/Ombi.Notifications/Agents/PushbulletNotification.cs index fd4be12a2..3823afe13 100644 --- a/src/Ombi.Notifications/Agents/PushbulletNotification.cs +++ b/src/Ombi.Notifications/Agents/PushbulletNotification.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Logging; using Ombi.Api.Pushbullet; using Ombi.Core.Settings; @@ -17,7 +18,7 @@ namespace Ombi.Notifications.Agents { public PushbulletNotification(IPushbulletApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s, IRepository sub, IMusicRequestRepository music, - IRepository userPref) : base(sn, r, m, t, s, log, sub, music, userPref) + IRepository userPref, UserManager um) : base(sn, r, m, t, s, log, sub, music, userPref, um) { Api = api; Logger = log; diff --git a/src/Ombi.Notifications/Agents/PushoverNotification.cs b/src/Ombi.Notifications/Agents/PushoverNotification.cs index 21352b8d6..3edcd62e2 100644 --- a/src/Ombi.Notifications/Agents/PushoverNotification.cs +++ b/src/Ombi.Notifications/Agents/PushoverNotification.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Logging; using Ombi.Api.Pushbullet; using Ombi.Api.Pushover; @@ -18,7 +19,7 @@ namespace Ombi.Notifications.Agents { public PushoverNotification(IPushoverApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s, IRepository sub, IMusicRequestRepository music, - IRepository userPref) : base(sn, r, m, t, s, log, sub, music, userPref) + IRepository userPref, UserManager um) : base(sn, r, m, t, s, log, sub, music, userPref, um) { Api = api; Logger = log; diff --git a/src/Ombi.Notifications/Agents/SlackNotification.cs b/src/Ombi.Notifications/Agents/SlackNotification.cs index 8dbf14a7d..7ea5b628a 100644 --- a/src/Ombi.Notifications/Agents/SlackNotification.cs +++ b/src/Ombi.Notifications/Agents/SlackNotification.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Logging; using Ombi.Api.Slack; using Ombi.Api.Slack.Models; @@ -18,7 +19,7 @@ namespace Ombi.Notifications.Agents { public SlackNotification(ISlackApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s, IRepository sub, IMusicRequestRepository music, - IRepository userPref) : base(sn, r, m, t, s, log, sub, music, userPref) + IRepository userPref, UserManager um) : base(sn, r, m, t, s, log, sub, music, userPref, um) { Api = api; Logger = log; diff --git a/src/Ombi.Notifications/Agents/TelegramNotification.cs b/src/Ombi.Notifications/Agents/TelegramNotification.cs index 6561a2494..7d5d9ea54 100644 --- a/src/Ombi.Notifications/Agents/TelegramNotification.cs +++ b/src/Ombi.Notifications/Agents/TelegramNotification.cs @@ -10,6 +10,7 @@ using Ombi.Store.Entities; using Ombi.Store.Repository; using Ombi.Store.Repository.Requests; using Ombi.Api.Telegram; +using Microsoft.AspNetCore.Identity; namespace Ombi.Notifications.Agents { @@ -19,7 +20,7 @@ namespace Ombi.Notifications.Agents INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s , IRepository sub, IMusicRequestRepository music, - IRepository userPref) : base(sn, r, m, t,s,log, sub, music, userPref) + IRepository userPref, UserManager um) : base(sn, r, m, t,s,log, sub, music, userPref, um) { Api = api; Logger = log; diff --git a/src/Ombi.Notifications/Agents/WebhookNotification.cs b/src/Ombi.Notifications/Agents/WebhookNotification.cs index c226d048a..6ed62605e 100644 --- a/src/Ombi.Notifications/Agents/WebhookNotification.cs +++ b/src/Ombi.Notifications/Agents/WebhookNotification.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Logging; using Ombi.Api.Webhook; using Ombi.Core.Settings; @@ -18,7 +19,7 @@ namespace Ombi.Notifications.Agents { public WebhookNotification(IWebhookApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s, IRepository sub, IMusicRequestRepository music, - IRepository userPref) : base(sn, r, m, t, s, log, sub, music, userPref) + IRepository userPref, UserManager um) : base(sn, r, m, t, s, log, sub, music, userPref, um) { Api = api; Logger = log; diff --git a/src/Ombi.Notifications/Agents/WhatsAppNotification.cs b/src/Ombi.Notifications/Agents/WhatsAppNotification.cs index cf97f498f..7956af18a 100644 --- a/src/Ombi.Notifications/Agents/WhatsAppNotification.cs +++ b/src/Ombi.Notifications/Agents/WhatsAppNotification.cs @@ -10,6 +10,7 @@ using Ombi.Store.Entities; using Ombi.Store.Repository; using Ombi.Store.Repository.Requests; using Ombi.Api.Twilio; +using Microsoft.AspNetCore.Identity; namespace Ombi.Notifications.Agents { @@ -19,7 +20,7 @@ namespace Ombi.Notifications.Agents INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s , IRepository sub, IMusicRequestRepository music, - IRepository userPref) : base(sn, r, m, t,s,log, sub, music, userPref) + IRepository userPref, UserManager um) : base(sn, r, m, t,s,log, sub, music, userPref, um) { Api = api; Logger = log; diff --git a/src/Ombi.Notifications/BaseNotification.cs b/src/Ombi.Notifications/BaseNotification.cs index ac9de5736..9397767bf 100644 --- a/src/Ombi.Notifications/BaseNotification.cs +++ b/src/Ombi.Notifications/BaseNotification.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Ombi.Core.Settings; @@ -19,7 +21,7 @@ namespace Ombi.Notifications { protected BaseNotification(ISettingsService settings, INotificationTemplatesRepository templateRepo, IMovieRequestRepository movie, ITvRequestRepository tv, ISettingsService customization, ILogger> log, IRepository sub, IMusicRequestRepository album, - IRepository notificationUserPreferences) + IRepository notificationUserPreferences, UserManager um) { Settings = settings; TemplateRepository = templateRepo; @@ -30,6 +32,7 @@ namespace Ombi.Notifications _log = log; AlbumRepository = album; UserNotificationPreferences = notificationUserPreferences; + _userManager = um; Settings.ClearCache(); } @@ -43,6 +46,7 @@ namespace Ombi.Notifications protected IRepository UserNotificationPreferences { get; set; } private ISettingsService CustomizationSettings { get; } private readonly ILogger> _log; + private readonly UserManager _userManager; protected ChildRequests TvRequest { get; set; } @@ -210,6 +214,13 @@ namespace Ombi.Notifications .FirstOrDefault(x => x.Agent == agent && x.UserId == userId); } + protected async Task> GetPrivilegedUsers() + { + IEnumerable recipients = await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin); + recipients = recipients.Concat(await _userManager.GetUsersInRoleAsync(OmbiRoles.PowerUser)); + return recipients; + } + private NotificationMessageContent Parse(NotificationOptions model, NotificationTemplates template, NotificationAgent agent) { var resolver = new NotificationMessageResolver(); diff --git a/src/Ombi/ClientApp/src/app/settings/notifications/emailnotification.component.html b/src/Ombi/ClientApp/src/app/settings/notifications/emailnotification.component.html index 33aa3a06c..5c24d21f2 100644 --- a/src/Ombi/ClientApp/src/app/settings/notifications/emailnotification.component.html +++ b/src/Ombi/ClientApp/src/app/settings/notifications/emailnotification.component.html @@ -61,7 +61,7 @@
Admin Email - + Admin Email is required