pull/1941/head
tidusjar 7 years ago
parent c370909619
commit e235475413

@ -2,12 +2,13 @@
{ {
public enum NotificationAgent public enum NotificationAgent
{ {
Email, Email = 0,
Discord, Discord = 1,
Pushbullet, Pushbullet = 2,
Pushover, Pushover = 3,
Telegram, Telegram = 4,
Slack, Slack = 5,
Mattermost, Mattermost = 6,
Mobile = 7,
} }
} }

@ -1,9 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Ombi.Api.Discord;
using Ombi.Api.Discord.Models;
using Ombi.Api.Notifications; using Ombi.Api.Notifications;
using Ombi.Core.Settings; using Ombi.Core.Settings;
using Ombi.Helpers; using Ombi.Helpers;
@ -19,29 +20,34 @@ namespace Ombi.Notifications.Agents
{ {
public class MobileNotification : BaseNotification<MobileNotificationSettings> public class MobileNotification : BaseNotification<MobileNotificationSettings>
{ {
public MobileNotification(IOneSignalApi api, ISettingsService<MobileNotificationSettings> sn, ILogger<MobileNotification> log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, ISettingsService<CustomizationSettings> s) : base(sn, r, m, t, s) public MobileNotification(IOneSignalApi api, ISettingsService<MobileNotificationSettings> sn, ILogger<MobileNotification> log, INotificationTemplatesRepository r,
IMovieRequestRepository m, ITvRequestRepository t, ISettingsService<CustomizationSettings> s, IRepository<NotificationUserId> notification,
UserManager<OmbiUser> um) : base(sn, r, m, t, s)
{ {
Api = api; _api = api;
Logger = log; _logger = log;
_notifications = notification;
_userManager = um;
} }
public override string NotificationName => "DiscordNotification"; public override string NotificationName => "MobileNotification";
private IOneSignalApi Api { get; } private readonly IOneSignalApi _api;
private ILogger<MobileNotification> Logger { get; } private readonly ILogger<MobileNotification> _logger;
private readonly IRepository<NotificationUserId> _notifications;
private readonly UserManager<OmbiUser> _userManager;
protected override bool ValidateConfiguration(MobileNotificationSettings settings) protected override bool ValidateConfiguration(MobileNotificationSettings settings)
{ {
return false; return false;
} }
protected override async Task NewRequest(NotificationOptions model, MobileNotificationSettings settings) 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) if (parsed.Disabled)
{ {
Logger.LogInformation($"Template {NotificationType.NewRequest} is disabled for {NotificationAgent.Discord}"); _logger.LogInformation($"Template {NotificationType.NewRequest} is disabled for {NotificationAgent.Mobile}");
return; return;
} }
var notification = new NotificationMessage var notification = new NotificationMessage
@ -49,16 +55,19 @@ namespace Ombi.Notifications.Agents
Message = parsed.Message, Message = parsed.Message,
}; };
notification.Other.Add("image", parsed.Image); // Get admin devices
await Send(notification, settings); 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) 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) if (parsed.Disabled)
{ {
Logger.LogInformation($"Template {NotificationType.Issue} is disabled for {NotificationAgent.Discord}"); _logger.LogInformation($"Template {NotificationType.Issue} is disabled for {NotificationAgent.Mobile}");
return; return;
} }
var notification = new NotificationMessage var notification = new NotificationMessage
@ -71,10 +80,10 @@ namespace Ombi.Notifications.Agents
protected override async Task IssueResolved(NotificationOptions model, MobileNotificationSettings settings) 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) if (parsed.Disabled)
{ {
Logger.LogInformation($"Template {NotificationType.IssueResolved} is disabled for {NotificationAgent.Discord}"); _logger.LogInformation($"Template {NotificationType.IssueResolved} is disabled for {NotificationAgent.Mobile}");
return; return;
} }
var notification = new NotificationMessage var notification = new NotificationMessage
@ -113,10 +122,10 @@ namespace Ombi.Notifications.Agents
protected override async Task RequestDeclined(NotificationOptions model, MobileNotificationSettings settings) 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) if (parsed.Disabled)
{ {
Logger.LogInformation($"Template {NotificationType.RequestDeclined} is disabled for {NotificationAgent.Discord}"); _logger.LogInformation($"Template {NotificationType.RequestDeclined} is disabled for {NotificationAgent.Mobile}");
return; return;
} }
var notification = new NotificationMessage var notification = new NotificationMessage
@ -129,10 +138,10 @@ namespace Ombi.Notifications.Agents
protected override async Task RequestApproved(NotificationOptions model, MobileNotificationSettings settings) 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) if (parsed.Disabled)
{ {
Logger.LogInformation($"Template {NotificationType.RequestApproved} is disabled for {NotificationAgent.Discord}"); _logger.LogInformation($"Template {NotificationType.RequestApproved} is disabled for {NotificationAgent.Mobile}");
return; return;
} }
var notification = new NotificationMessage var notification = new NotificationMessage
@ -146,10 +155,10 @@ namespace Ombi.Notifications.Agents
protected override async Task AvailableRequest(NotificationOptions model, MobileNotificationSettings settings) 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) if (parsed.Disabled)
{ {
Logger.LogInformation($"Template {NotificationType.RequestAvailable} is disabled for {NotificationAgent.Discord}"); _logger.LogInformation($"Template {NotificationType.RequestAvailable} is disabled for {NotificationAgent.Mobile}");
return; return;
} }
var notification = new NotificationMessage var notification = new NotificationMessage
@ -159,38 +168,15 @@ namespace Ombi.Notifications.Agents
notification.Other.Add("image", parsed.Image); notification.Other.Add("image", parsed.Image);
await Send(notification, settings); await Send(notification, settings);
} }
protected override Task Send(NotificationMessage model, MobileNotificationSettings settings)
protected override async Task Send(NotificationMessage model, MobileNotificationSettings settings)
{ {
try throw new NotImplementedException();
{ }
var discordBody = new DiscordWebhookBody
{
content = model.Message,
//username = settings.Username,
};
string image;
if (model.Other.TryGetValue("image", out image))
{
discordBody.embeds = new List<DiscordEmbeds>
{
new DiscordEmbeds
{
image = new DiscordImage
{
url = image
}
}
};
}
//await Api.SendMessage(discordBody, settings.WebHookId, settings.Token); protected async Task Send(List<string> playerIds, NotificationMessage model, MobileNotificationSettings settings)
} {
catch (Exception e) var response = await _api.PushNotification(playerIds, model.Message);
{ _logger.LogDebug("Sent message to {0} recipients with message id {1}", response.recipients, response.id);
Logger.LogError(LoggingEvents.DiscordNotification, e, "Failed to send Discord Notification");
}
} }
protected override async Task Test(NotificationOptions model, MobileNotificationSettings settings) protected override async Task Test(NotificationOptions model, MobileNotificationSettings settings)

@ -99,7 +99,7 @@ namespace Ombi.Store.Context
}); });
SaveChanges(); SaveChanges();
} }
var notification = ApplicationConfigurations.FirstOrDefault(x => x.Type != ConfigurationTypes.Notification); var notification = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.Notification);
if (notification == null) if (notification == null)
{ {
ApplicationConfigurations.Add(new ApplicationConfiguration ApplicationConfigurations.Add(new ApplicationConfiguration

Loading…
Cancel
Save