diff --git a/src/Ombi.Helpers/NotificationAgent.cs b/src/Ombi.Helpers/NotificationAgent.cs index 515277f9e..8990eeba9 100644 --- a/src/Ombi.Helpers/NotificationAgent.cs +++ b/src/Ombi.Helpers/NotificationAgent.cs @@ -2,12 +2,13 @@ { public enum NotificationAgent { - Email, - Discord, - Pushbullet, - Pushover, - Telegram, - Slack, - Mattermost, + Email = 0, + Discord = 1, + Pushbullet = 2, + Pushover = 3, + Telegram = 4, + Slack = 5, + Mattermost = 6, + Mobile = 7, } } \ No newline at end of file diff --git a/src/Ombi.Notifications/Agents/MobileNotification.cs b/src/Ombi.Notifications/Agents/MobileNotification.cs index 130e1c668..5074226c8 100644 --- a/src/Ombi.Notifications/Agents/MobileNotification.cs +++ b/src/Ombi.Notifications/Agents/MobileNotification.cs @@ -1,9 +1,10 @@ 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.Api.Discord; -using Ombi.Api.Discord.Models; using Ombi.Api.Notifications; using Ombi.Core.Settings; using Ombi.Helpers; @@ -19,29 +20,34 @@ namespace Ombi.Notifications.Agents { public class MobileNotification : BaseNotification { - public MobileNotification(IOneSignalApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s) : base(sn, r, m, t, s) + public MobileNotification(IOneSignalApi api, ISettingsService sn, ILogger log, INotificationTemplatesRepository r, + IMovieRequestRepository m, ITvRequestRepository t, ISettingsService s, IRepository notification, + UserManager um) : base(sn, r, m, t, s) { - Api = api; - Logger = log; + _api = api; + _logger = log; + _notifications = notification; + _userManager = um; } - public override string NotificationName => "DiscordNotification"; + public override string NotificationName => "MobileNotification"; - private IOneSignalApi Api { get; } - private ILogger Logger { get; } + private readonly IOneSignalApi _api; + private readonly ILogger _logger; + private readonly IRepository _notifications; + private readonly UserManager _userManager; protected override bool ValidateConfiguration(MobileNotificationSettings settings) { - return false; } protected override async Task NewRequest(NotificationOptions model, MobileNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.NewRequest, model); + var parsed = await LoadTemplate(NotificationAgent.Mobile, NotificationType.NewRequest, model); if (parsed.Disabled) { - Logger.LogInformation($"Template {NotificationType.NewRequest} is disabled for {NotificationAgent.Discord}"); + _logger.LogInformation($"Template {NotificationType.NewRequest} is disabled for {NotificationAgent.Mobile}"); return; } var notification = new NotificationMessage @@ -49,16 +55,19 @@ namespace Ombi.Notifications.Agents Message = parsed.Message, }; - notification.Other.Add("image", parsed.Image); - await Send(notification, settings); + // Get admin devices + var adminUsers = (await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin)).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(); + await Send(playerIds, notification, settings); } protected override async Task NewIssue(NotificationOptions model, MobileNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.Issue, model); + var parsed = await LoadTemplate(NotificationAgent.Mobile, NotificationType.Issue, model); if (parsed.Disabled) { - Logger.LogInformation($"Template {NotificationType.Issue} is disabled for {NotificationAgent.Discord}"); + _logger.LogInformation($"Template {NotificationType.Issue} is disabled for {NotificationAgent.Mobile}"); return; } var notification = new NotificationMessage @@ -71,10 +80,10 @@ namespace Ombi.Notifications.Agents protected override async Task IssueResolved(NotificationOptions model, MobileNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.IssueResolved, model); + var parsed = await LoadTemplate(NotificationAgent.Mobile, NotificationType.IssueResolved, model); if (parsed.Disabled) { - Logger.LogInformation($"Template {NotificationType.IssueResolved} is disabled for {NotificationAgent.Discord}"); + _logger.LogInformation($"Template {NotificationType.IssueResolved} is disabled for {NotificationAgent.Mobile}"); return; } var notification = new NotificationMessage @@ -113,10 +122,10 @@ namespace Ombi.Notifications.Agents protected override async Task RequestDeclined(NotificationOptions model, MobileNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.RequestDeclined, model); + var parsed = await LoadTemplate(NotificationAgent.Mobile, NotificationType.RequestDeclined, model); if (parsed.Disabled) { - Logger.LogInformation($"Template {NotificationType.RequestDeclined} is disabled for {NotificationAgent.Discord}"); + _logger.LogInformation($"Template {NotificationType.RequestDeclined} is disabled for {NotificationAgent.Mobile}"); return; } var notification = new NotificationMessage @@ -129,10 +138,10 @@ namespace Ombi.Notifications.Agents protected override async Task RequestApproved(NotificationOptions model, MobileNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.RequestApproved, model); + var parsed = await LoadTemplate(NotificationAgent.Mobile, NotificationType.RequestApproved, model); if (parsed.Disabled) { - Logger.LogInformation($"Template {NotificationType.RequestApproved} is disabled for {NotificationAgent.Discord}"); + _logger.LogInformation($"Template {NotificationType.RequestApproved} is disabled for {NotificationAgent.Mobile}"); return; } var notification = new NotificationMessage @@ -146,10 +155,10 @@ namespace Ombi.Notifications.Agents protected override async Task AvailableRequest(NotificationOptions model, MobileNotificationSettings settings) { - var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.RequestAvailable, model); + var parsed = await LoadTemplate(NotificationAgent.Mobile, NotificationType.RequestAvailable, model); if (parsed.Disabled) { - Logger.LogInformation($"Template {NotificationType.RequestAvailable} is disabled for {NotificationAgent.Discord}"); + _logger.LogInformation($"Template {NotificationType.RequestAvailable} is disabled for {NotificationAgent.Mobile}"); return; } var notification = new NotificationMessage @@ -159,38 +168,15 @@ namespace Ombi.Notifications.Agents notification.Other.Add("image", parsed.Image); await Send(notification, settings); } - - protected override async Task Send(NotificationMessage model, MobileNotificationSettings settings) + protected override Task Send(NotificationMessage model, MobileNotificationSettings settings) { - try - { - var discordBody = new DiscordWebhookBody - { - content = model.Message, - //username = settings.Username, - }; - - string image; - if (model.Other.TryGetValue("image", out image)) - { - discordBody.embeds = new List - { - new DiscordEmbeds - { - image = new DiscordImage - { - url = image - } - } - }; - } + throw new NotImplementedException(); + } - //await Api.SendMessage(discordBody, settings.WebHookId, settings.Token); - } - catch (Exception e) - { - Logger.LogError(LoggingEvents.DiscordNotification, e, "Failed to send Discord Notification"); - } + protected async Task Send(List playerIds, NotificationMessage model, MobileNotificationSettings settings) + { + var response = await _api.PushNotification(playerIds, model.Message); + _logger.LogDebug("Sent message to {0} recipients with message id {1}", response.recipients, response.id); } protected override async Task Test(NotificationOptions model, MobileNotificationSettings settings) diff --git a/src/Ombi.Store/Context/OmbiContext.cs b/src/Ombi.Store/Context/OmbiContext.cs index d18d5865f..967347a6c 100644 --- a/src/Ombi.Store/Context/OmbiContext.cs +++ b/src/Ombi.Store/Context/OmbiContext.cs @@ -99,7 +99,7 @@ namespace Ombi.Store.Context }); SaveChanges(); } - var notification = ApplicationConfigurations.FirstOrDefault(x => x.Type != ConfigurationTypes.Notification); + var notification = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.Notification); if (notification == null) { ApplicationConfigurations.Add(new ApplicationConfiguration