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

@ -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,
}
}

@ -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<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;
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<MobileNotification> Logger { get; }
private readonly IOneSignalApi _api;
private readonly ILogger<MobileNotification> _logger;
private readonly IRepository<NotificationUserId> _notifications;
private readonly UserManager<OmbiUser> _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)
{
try
{
var discordBody = new DiscordWebhookBody
{
content = model.Message,
//username = settings.Username,
};
string image;
if (model.Other.TryGetValue("image", out image))
protected override Task Send(NotificationMessage model, MobileNotificationSettings settings)
{
discordBody.embeds = new List<DiscordEmbeds>
{
new DiscordEmbeds
{
image = new DiscordImage
{
url = image
}
}
};
throw new NotImplementedException();
}
//await Api.SendMessage(discordBody, settings.WebHookId, settings.Token);
}
catch (Exception e)
protected async Task Send(List<string> playerIds, NotificationMessage model, MobileNotificationSettings settings)
{
Logger.LogError(LoggingEvents.DiscordNotification, e, "Failed to send Discord Notification");
}
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)

@ -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

Loading…
Cancel
Save