Remoddeled the notificaiton settings to make it easier to add more. This is some techinical changes that no one except me will ever notice :(

pull/1251/head^2
Jamie.Rees 8 years ago
parent 0280f8149e
commit dc63693bf6

@ -35,7 +35,7 @@ using Ombi.Services.Interfaces;
namespace Ombi.Services.Notification namespace Ombi.Services.Notification
{ {
public abstract class BaseNotification<T,U> : INotification where T : Settings, new() where U : new() public abstract class BaseNotification<T> : INotification where T : Settings, new()
{ {
protected BaseNotification(ISettingsService<T> settings) protected BaseNotification(ISettingsService<T> settings)
{ {
@ -63,35 +63,42 @@ namespace Ombi.Services.Notification
return; return;
} }
try
{
switch (model.NotificationType) switch (model.NotificationType)
{ {
case NotificationType.NewRequest: case NotificationType.NewRequest:
await EmailNewRequest(model, notificationSettings); await NewRequest(model, notificationSettings);
break; break;
case NotificationType.Issue: case NotificationType.Issue:
await EmailIssue(model, notificationSettings); await Issue(model, notificationSettings);
break; break;
case NotificationType.RequestAvailable: case NotificationType.RequestAvailable:
await EmailAvailableRequest(model, notificationSettings); await AvailableRequest(model, notificationSettings);
break; break;
case NotificationType.RequestApproved: case NotificationType.RequestApproved:
await EmailRequestApproved(model, notificationSettings); await RequestApproved(model, notificationSettings);
break; break;
case NotificationType.AdminNote: case NotificationType.AdminNote:
throw new NotImplementedException(); throw new NotImplementedException();
case NotificationType.Test: case NotificationType.Test:
await EmailTest(model, notificationSettings); await Test(model, notificationSettings);
break; break;
case NotificationType.RequestDeclined: case NotificationType.RequestDeclined:
await EmailRequestDeclined(model, notificationSettings); await RequestDeclined(model, notificationSettings);
break; break;
case NotificationType.ItemAddedToFaultQueue: case NotificationType.ItemAddedToFaultQueue:
await EmailAddedToRequestQueue(model, notificationSettings); await AddedToRequestQueue(model, notificationSettings);
break; break;
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
}
catch (NotImplementedException)
{
// Do nothing, it's not implimented meaning it might not be ready or even used
}
} }
@ -103,14 +110,14 @@ namespace Ombi.Services.Notification
protected abstract bool ValidateConfiguration(T settings); protected abstract bool ValidateConfiguration(T settings);
protected abstract Task EmailNewRequest(NotificationModel model, T settings); protected abstract Task NewRequest(NotificationModel model, T settings);
protected abstract Task EmailIssue(NotificationModel model, T settings); protected abstract Task Issue(NotificationModel model, T settings);
protected abstract Task EmailAddedToRequestQueue(NotificationModel model, T settings); protected abstract Task AddedToRequestQueue(NotificationModel model, T settings);
protected abstract Task EmailRequestDeclined(NotificationModel model, T settings); protected abstract Task RequestDeclined(NotificationModel model, T settings);
protected abstract Task EmailRequestApproved(NotificationModel model, T settings); protected abstract Task RequestApproved(NotificationModel model, T settings);
protected abstract Task EmailAvailableRequest(NotificationModel model, T settings); protected abstract Task AvailableRequest(NotificationModel model, T settings);
protected abstract Task Send(U message, T settings); protected abstract Task Send(NotificationMessage model, T settings);
protected abstract Task EmailTest(NotificationModel model, T settings); protected abstract Task Test(NotificationModel model, T settings);
} }
} }

@ -29,137 +29,116 @@ using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using NLog; using NLog;
using Ombi.Api.Interfaces; using Ombi.Api.Interfaces;
using Ombi.Api.Models.Notifications;
using Ombi.Core; using Ombi.Core;
using Ombi.Core.Models;
using Ombi.Core.SettingModels; using Ombi.Core.SettingModels;
using Ombi.Services.Interfaces;
namespace Ombi.Services.Notification namespace Ombi.Services.Notification
{ {
public class DiscordNotification : INotification public class DiscordNotification : BaseNotification<DiscordNotificationSettings>
{ {
public DiscordNotification(IDiscordApi api, ISettingsService<DiscordNotificationSettings> sn) public DiscordNotification(IDiscordApi api, ISettingsService<DiscordNotificationSettings> sn) : base(sn)
{ {
Api = api; Api = api;
Settings = sn;
} }
public string NotificationName => "DiscordNotification"; public override string NotificationName => "DiscordNotification";
private IDiscordApi Api { get; } private IDiscordApi Api { get; }
private ISettingsService<DiscordNotificationSettings> Settings { get; } private static readonly Logger Log = LogManager.GetCurrentClassLogger();
private static Logger Log = LogManager.GetCurrentClassLogger();
protected override bool ValidateConfiguration(DiscordNotificationSettings settings)
public async Task NotifyAsync(NotificationModel model)
{ {
var settings = Settings.GetSettings(); if (!settings.Enabled)
{
await NotifyAsync(model, settings); return false;
} }
if (string.IsNullOrEmpty(settings.WebhookUrl))
public async Task NotifyAsync(NotificationModel model, Settings settings)
{ {
if (settings == null) await NotifyAsync(model); return false;
}
var pushSettings = (DiscordNotificationSettings)settings; try
if (!ValidateConfiguration(pushSettings))
{ {
Log.Error("Settings for Slack was not correct, we cannot push a notification"); var a = settings.Token;
return; var b = settings.WebookId;
} }
catch (IndexOutOfRangeException)
switch (model.NotificationType) {
{ return false;
case NotificationType.NewRequest:
await PushNewRequestAsync(model, pushSettings);
break;
case NotificationType.Issue:
await PushIssueAsync(model, pushSettings);
break;
case NotificationType.RequestAvailable:
break;
case NotificationType.RequestApproved:
break;
case NotificationType.AdminNote:
break;
case NotificationType.Test:
await PushTest(pushSettings);
break;
case NotificationType.RequestDeclined:
await PushRequestDeclinedAsync(model, pushSettings);
break;
case NotificationType.ItemAddedToFaultQueue:
await PushFaultQueue(model, pushSettings);
break;
default:
throw new ArgumentOutOfRangeException();
} }
return true;
} }
private async Task PushNewRequestAsync(NotificationModel model, DiscordNotificationSettings settings) protected override async Task NewRequest(NotificationModel model, DiscordNotificationSettings settings)
{ {
var message = $"{model.Title} has been requested by user: {model.User}"; var message = $"{model.Title} has been requested by user: {model.User}";
await Push(settings, message);
}
private async Task PushRequestDeclinedAsync(NotificationModel model, DiscordNotificationSettings settings) var notification = new NotificationMessage
{ {
var message = $"Hello! Your request for {model.Title} has been declined, Sorry!"; Message = message,
await Push(settings, message); };
await Send(notification, settings);
} }
private async Task PushIssueAsync(NotificationModel model, DiscordNotificationSettings settings) protected override async Task Issue(NotificationModel model, DiscordNotificationSettings settings)
{ {
var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}"; var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}";
await Push(settings, message); var notification = new NotificationMessage
}
private async Task PushTest(DiscordNotificationSettings settings)
{ {
var message = $"This is a test from Ombi, if you can see this then we have successfully pushed a notification!"; Message = message,
await Push(settings, message); };
await Send(notification, settings);
} }
private async Task PushFaultQueue(NotificationModel model, DiscordNotificationSettings settings) protected override async Task AddedToRequestQueue(NotificationModel model, DiscordNotificationSettings settings)
{ {
var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying";
await Push(settings, message); var notification = new NotificationMessage
{
Message = message,
};
await Send(notification, settings);
} }
private async Task Push(DiscordNotificationSettings config, string message) protected override async Task RequestDeclined(NotificationModel model, DiscordNotificationSettings settings)
{ {
try var message = $"Hello! Your request for {model.Title} has been declined, Sorry!";
var notification = new NotificationMessage
{ {
await Api.SendMessageAsync(message, config.WebookId, config.Token, config.Username); Message = message,
};
await Send(notification, settings);
} }
catch (Exception e)
protected override Task RequestApproved(NotificationModel model, DiscordNotificationSettings settings)
{ {
Log.Error(e); throw new NotImplementedException();
}
} }
private bool ValidateConfiguration(DiscordNotificationSettings settings) protected override Task AvailableRequest(NotificationModel model, DiscordNotificationSettings settings)
{
if (!settings.Enabled)
{ {
return false; throw new NotImplementedException();
} }
if (string.IsNullOrEmpty(settings.WebhookUrl))
protected override async Task Send(NotificationMessage model, DiscordNotificationSettings settings)
{ {
return false;
}
try try
{ {
var a = settings.Token; await Api.SendMessageAsync(model.Message, settings.WebookId, settings.Token, settings.Username);
var b = settings.WebookId;
} }
catch (IndexOutOfRangeException) catch (Exception e)
{ {
return false; Log.Error(e);
} }
return true; }
protected override async Task Test(NotificationModel model, DiscordNotificationSettings settings)
{
var message = $"This is a test from Ombi, if you can see this then we have successfully pushed a notification!";
var notification = new NotificationMessage
{
Message = message,
};
await Send(notification, settings);
} }
} }
} }

@ -37,7 +37,7 @@ using SmtpClient = MailKit.Net.Smtp.SmtpClient;
namespace Ombi.Services.Notification namespace Ombi.Services.Notification
{ {
public class EmailMessageNotification : BaseNotification<EmailNotificationSettings, MimeMessage> public class EmailMessageNotification : BaseNotification<EmailNotificationSettings>
{ {
public EmailMessageNotification(ISettingsService<EmailNotificationSettings> settings) : base(settings) public EmailMessageNotification(ISettingsService<EmailNotificationSettings> settings) : base(settings)
{ {
@ -63,135 +63,150 @@ namespace Ombi.Services.Notification
return true; return true;
} }
protected override async Task EmailNewRequest(NotificationModel model, EmailNotificationSettings settings) protected override async Task NewRequest(NotificationModel model, EmailNotificationSettings settings)
{ {
var email = new EmailBasicTemplate(); var email = new EmailBasicTemplate();
var html = email.LoadTemplate( var html = email.LoadTemplate(
$"Ombi: New {model.RequestType.GetString()?.ToLower()} request for {model.Title}!", $"Ombi: New {model.RequestType.GetString()?.ToLower()} request for {model.Title}!",
$"Hello! The user '{model.User}' has requested the {model.RequestType.GetString()?.ToLower()} '{model.Title}'! Please log in to approve this request. Request Date: {model.DateTime.ToString("f")}", $"Hello! The user '{model.User}' has requested the {model.RequestType.GetString()?.ToLower()} '{model.Title}'! Please log in to approve this request. Request Date: {model.DateTime.ToString("f")}",
model.ImgSrc); model.ImgSrc);
var body = new BodyBuilder { HtmlBody = html, TextBody = $"Hello! The user '{model.User}' has requested the {model.RequestType.GetString()?.ToLower()} '{model.Title}'! Please log in to approve this request. Request Date: {model.DateTime.ToString("f")}" };
var message = new MimeMessage
var message = new NotificationMessage
{ {
Body = body.ToMessageBody(), Message = html,
Subject = $"Ombi: New {model.RequestType.GetString()?.ToLower()} request for {model.Title}!" Subject = $"Ombi: New {model.RequestType.GetString()?.ToLower()} request for {model.Title}!",
To = model.UserEmail,
}; };
message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender));
message.To.Add(new MailboxAddress(settings.RecipientEmail, settings.RecipientEmail));
message.Other.Add("PlainTextBody", $"Hello! The user '{model.User}' has requested the {model.RequestType.GetString()?.ToLower()} '{model.Title}'! Please log in to approve this request. Request Date: {model.DateTime.ToString("f")}");
await Send(message, settings); await Send(message, settings);
} }
protected override async Task EmailIssue(NotificationModel model, EmailNotificationSettings settings) protected override async Task Issue(NotificationModel model, EmailNotificationSettings settings)
{ {
var email = new EmailBasicTemplate(); var email = new EmailBasicTemplate();
var html = email.LoadTemplate( var html = email.LoadTemplate(
$"Ombi: New issue for {model.Title}!", $"Ombi: New issue for {model.Title}!",
$"Hello! The user '{model.User}' has reported a new issue {model.Body} for the title {model.Title}!", $"Hello! The user '{model.User}' has reported a new issue {model.Body} for the title {model.Title}!",
model.ImgSrc); model.ImgSrc);
var body = new BodyBuilder { HtmlBody = html, TextBody = $"Hello! The user '{model.User}' has reported a new issue {model.Body} for the title {model.Title}!"};
var message = new MimeMessage var message = new NotificationMessage
{ {
Body = body.ToMessageBody(), Message = html,
Subject = $"Ombi: New issue for {model.Title}!" Subject = $"Ombi: New issue for {model.Title}!",
To = model.UserEmail,
}; };
message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender));
message.To.Add(new MailboxAddress(settings.RecipientEmail, settings.RecipientEmail)); message.Other.Add("PlainTextBody", $"Hello! The user '{model.User}' has reported a new issue {model.Body} for the title {model.Title}!");
await Send(message, settings); await Send(message, settings);
} }
protected override async Task EmailAddedToRequestQueue(NotificationModel model, EmailNotificationSettings settings) protected override async Task AddedToRequestQueue(NotificationModel model, EmailNotificationSettings settings)
{ {
var email = new EmailBasicTemplate(); var email = new EmailBasicTemplate();
var html = email.LoadTemplate( var html = email.LoadTemplate(
"Ombi: A request could not be added.", "Ombi: A request could not be added.",
$"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying", $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying",
model.ImgSrc); model.ImgSrc);
var body = new BodyBuilder { HtmlBody = html, TextBody = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying" };
var message = new MimeMessage var message = new NotificationMessage
{ {
Body = body.ToMessageBody(), Message = html,
Subject = $"Ombi: A request could not be added" Subject = $"Ombi: A request could not be added",
To = model.UserEmail,
}; };
message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender));
message.To.Add(new MailboxAddress(settings.RecipientEmail, settings.RecipientEmail)); message.Other.Add("PlainTextBody", $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying");
await Send(message, settings); await Send(message, settings);
} }
protected override async Task EmailRequestDeclined(NotificationModel model, EmailNotificationSettings settings) protected override async Task RequestDeclined(NotificationModel model, EmailNotificationSettings settings)
{ {
var email = new EmailBasicTemplate(); var email = new EmailBasicTemplate();
var html = email.LoadTemplate( var html = email.LoadTemplate(
"Ombi: Your request has been declined", "Ombi: Your request has been declined",
$"Hello! Your request for {model.Title} has been declined, Sorry!", $"Hello! Your request for {model.Title} has been declined, Sorry!",
model.ImgSrc); model.ImgSrc);
var body = new BodyBuilder { HtmlBody = html, TextBody = $"Hello! Your request for {model.Title} has been declined, Sorry!", };
var message = new MimeMessage var message = new NotificationMessage
{ {
Body = body.ToMessageBody(), Message = html,
Subject = $"Ombi: Your request has been declined" Subject = $"Ombi: Your request has been declined",
To = model.UserEmail,
}; };
message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender));
message.To.Add(new MailboxAddress(model.UserEmail, model.UserEmail)); message.Other.Add("PlainTextBody", $"Hello! Your request for {model.Title} has been declined, Sorry!");
await Send(message, settings); await Send(message, settings);
} }
protected override async Task EmailRequestApproved(NotificationModel model, EmailNotificationSettings settings) protected override async Task RequestApproved(NotificationModel model, EmailNotificationSettings settings)
{ {
var email = new EmailBasicTemplate(); var email = new EmailBasicTemplate();
var html = email.LoadTemplate( var html = email.LoadTemplate(
"Ombi: Your request has been approved!", "Ombi: Your request has been approved!",
$"Hello! Your request for {model.Title} has been approved!", $"Hello! Your request for {model.Title} has been approved!",
model.ImgSrc); model.ImgSrc);
var body = new BodyBuilder { HtmlBody = html, TextBody = $"Hello! Your request for {model.Title} has been approved!", };
var message = new MimeMessage var message = new NotificationMessage
{ {
Body = body.ToMessageBody(), Message = html,
Subject = $"Ombi: Your request has been approved!" Subject = $"Ombi: Your request has been approved!",
To = model.UserEmail,
}; };
message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender));
message.To.Add(new MailboxAddress(model.UserEmail, model.UserEmail));
message.Other.Add("PlainTextBody", $"Hello! Your request for {model.Title} has been approved!");
await Send(message, settings); await Send(message, settings);
} }
protected override async Task EmailAvailableRequest(NotificationModel model, EmailNotificationSettings settings) protected override async Task AvailableRequest(NotificationModel model, EmailNotificationSettings settings)
{ {
var email = new EmailBasicTemplate(); var email = new EmailBasicTemplate();
var html = email.LoadTemplate( var html = email.LoadTemplate(
$"Ombi: {model.Title} is now available!", $"Ombi: {model.Title} is now available!",
$"Hello! You requested {model.Title} on Ombi! This is now available on Plex! :)", $"Hello! You requested {model.Title} on Ombi! This is now available on Plex! :)",
model.ImgSrc); model.ImgSrc);
var body = new BodyBuilder { HtmlBody = html, TextBody = $"Hello! You requested {model.Title} on Ombi! This is now available on Plex! :)" };
var message = new MimeMessage
var message = new NotificationMessage
{ {
Body = body.ToMessageBody(), Message = html,
Subject = $"Ombi: {model.Title} is now available!" Subject = $"Ombi: {model.Title} is now available!",
To = model.UserEmail,
}; };
message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender));
message.To.Add(new MailboxAddress(model.UserEmail, model.UserEmail)); message.Other.Add("PlainTextBody", $"Hello! You requested {model.Title} on Ombi! This is now available on Plex! :)");
await Send(message, settings); await Send(message, settings);
} }
protected override async Task Send(MimeMessage message, EmailNotificationSettings settings) protected override async Task Send(NotificationMessage model, EmailNotificationSettings settings)
{ {
try try
{ {
var body = new BodyBuilder
{
HtmlBody = model.Message,
TextBody = model.Other["PlainTextBody"]
};
var message = new MimeMessage
{
Body = body.ToMessageBody(),
Subject = model.Subject
};
message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender));
message.To.Add(new MailboxAddress(model.To, model.To));
using (var client = new SmtpClient()) using (var client = new SmtpClient())
{ {
client.Connect(settings.EmailHost, settings.EmailPort); // Let MailKit figure out the correct SecureSocketOptions. client.Connect(settings.EmailHost, settings.EmailPort); // Let MailKit figure out the correct SecureSocketOptions.
@ -215,20 +230,21 @@ namespace Ombi.Services.Notification
} }
} }
protected override async Task EmailTest(NotificationModel model, EmailNotificationSettings settings) protected override async Task Test(NotificationModel model, EmailNotificationSettings settings)
{ {
var email = new EmailBasicTemplate(); var email = new EmailBasicTemplate();
var html = email.LoadTemplate( var html = email.LoadTemplate(
"Test Message", "Test Message",
"This is just a test! Success!", "This is just a test! Success!",
model.ImgSrc); model.ImgSrc);
var body = new BodyBuilder { HtmlBody = html, }; var message = new NotificationMessage
var message = new MimeMessage
{ {
Body = body.ToMessageBody() Message = html,
Subject = $"Ombi: Test",
To = model.UserEmail,
}; };
message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender));
message.To.Add(new MailboxAddress(settings.RecipientEmail, settings.RecipientEmail)); message.Other.Add("PlainTextBody", "This is just a test! Success!");
await Send(message, settings); await Send(message, settings);
} }

@ -0,0 +1,40 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2017 Jamie Rees
// File: NotificationMessage.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System.Collections.Generic;
namespace Ombi.Services.Notification
{
public class NotificationMessage
{
public string Subject { get; set; }
public string Message { get; set; }
public string To { get; set; }
public Dictionary<string,string> Other { get; set; } = new Dictionary<string, string>();
}
}

@ -30,67 +30,24 @@ using System.Threading.Tasks;
using NLog; using NLog;
using Ombi.Api.Interfaces; using Ombi.Api.Interfaces;
using Ombi.Core; using Ombi.Core;
using Ombi.Core.Models;
using Ombi.Core.SettingModels; using Ombi.Core.SettingModels;
using Ombi.Services.Interfaces;
using Ombi.Store; using Ombi.Store;
namespace Ombi.Services.Notification namespace Ombi.Services.Notification
{ {
public class PushbulletNotification : INotification public class PushbulletNotification : BaseNotification<PushbulletNotificationSettings>
{ {
public PushbulletNotification(IPushbulletApi pushbulletApi, ISettingsService<PushbulletNotificationSettings> settings) public PushbulletNotification(IPushbulletApi pushbulletApi, ISettingsService<PushbulletNotificationSettings> settings) : base(settings)
{ {
PushbulletApi = pushbulletApi; PushbulletApi = pushbulletApi;
SettingsService = settings;
} }
private IPushbulletApi PushbulletApi { get; } private IPushbulletApi PushbulletApi { get; }
private ISettingsService<PushbulletNotificationSettings> SettingsService { get; } private ISettingsService<PushbulletNotificationSettings> SettingsService { get; }
private static Logger Log = LogManager.GetCurrentClassLogger(); private static readonly Logger Log = LogManager.GetCurrentClassLogger();
public string NotificationName => "PushbulletNotification"; public override string NotificationName => "PushbulletNotification";
public async Task NotifyAsync(NotificationModel model)
{
var configuration = GetSettings();
await NotifyAsync(model, configuration);
}
public async Task NotifyAsync(NotificationModel model, Settings settings)
{
if (settings == null) await NotifyAsync(model);
var pushSettings = (PushbulletNotificationSettings)settings;
if (!ValidateConfiguration(pushSettings)) return;
switch (model.NotificationType)
{
case NotificationType.NewRequest:
await PushNewRequestAsync(model, pushSettings);
break;
case NotificationType.Issue:
await PushIssueAsync(model, pushSettings);
break;
case NotificationType.RequestAvailable:
break;
case NotificationType.RequestApproved:
break;
case NotificationType.AdminNote:
break;
case NotificationType.Test:
await PushTestAsync(pushSettings);
break;
case NotificationType.RequestDeclined:
break;
case NotificationType.ItemAddedToFaultQueue:
await PushFaultQueue(model, pushSettings);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private bool ValidateConfiguration(PushbulletNotificationSettings settings) protected override bool ValidateConfiguration(PushbulletNotificationSettings settings)
{ {
if (!settings.Enabled) if (!settings.Enabled)
{ {
@ -103,44 +60,64 @@ namespace Ombi.Services.Notification
return true; return true;
} }
private PushbulletNotificationSettings GetSettings() protected override async Task NewRequest(NotificationModel model, PushbulletNotificationSettings settings)
{
return SettingsService.GetSettings();
}
private async Task PushNewRequestAsync(NotificationModel model, PushbulletNotificationSettings settings)
{ {
var message = $"The {model.RequestType.GetString()?.ToLower()} '{model.Title}' has been requested by user: {model.User}"; var message = $"The {model.RequestType.GetString()?.ToLower()} '{model.Title}' has been requested by user: {model.User}";
var pushTitle = $"Ombi: The {model.RequestType.GetString()?.ToLower()} {model.Title} has been requested!"; var pushTitle = $"Ombi: The {model.RequestType.GetString()?.ToLower()} {model.Title} has been requested!";
await Push(settings, message, pushTitle); var notification = new NotificationMessage
{
Message = message,
Subject = pushTitle
};
await Send(notification, settings);
} }
private async Task PushIssueAsync(NotificationModel model, PushbulletNotificationSettings settings)
protected override async Task Issue(NotificationModel model, PushbulletNotificationSettings settings)
{ {
var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}"; var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}";
var pushTitle = $"Ombi: A new issue has been reported for {model.Title}"; var pushTitle = $"Ombi: A new issue has been reported for {model.Title}";
await Push(settings, message, pushTitle); var notification = new NotificationMessage
{
Message = message,
Subject = pushTitle
};
await Send(notification, settings);
} }
private async Task PushTestAsync(PushbulletNotificationSettings settings) protected override async Task AddedToRequestQueue(NotificationModel model, PushbulletNotificationSettings settings)
{ {
var message = "This is just a test! Success!";
var pushTitle = "Ombi: Test Message!"; var message = $"Hello!The user '{model.User}' has requested { model.Title} but it could not be added. This has been added into the requests queue and will keep retrying";
await Push(settings, message, pushTitle); var pushTitle = $"Ombi: A request could not be added.";
var notification = new NotificationMessage
{
Message = message,
Subject = pushTitle
};
await Send(notification, settings);
}
protected override Task RequestDeclined(NotificationModel model, PushbulletNotificationSettings settings)
{
throw new NotImplementedException();
} }
private async Task PushFaultQueue(NotificationModel model, PushbulletNotificationSettings settings) protected override Task RequestApproved(NotificationModel model, PushbulletNotificationSettings settings)
{ {
var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; throw new NotImplementedException();
var pushTitle = $"Ombi: The {model.RequestType.GetString()?.ToLower()} {model.Title} has been requested but could not be added!";
await Push(settings, message, pushTitle);
} }
private async Task Push(PushbulletNotificationSettings settings, string message, string title) protected override Task AvailableRequest(NotificationModel model, PushbulletNotificationSettings settings)
{
throw new NotImplementedException();
}
protected override async Task Send(NotificationMessage model, PushbulletNotificationSettings settings)
{ {
try try
{ {
var result = await PushbulletApi.PushAsync(settings.AccessToken, title, message, settings.DeviceIdentifier); var result = await PushbulletApi.PushAsync(settings.AccessToken, model.Subject, model.Message, settings.DeviceIdentifier);
if (result != null) if (result != null)
{ {
Log.Error("Pushbullet api returned a null value, the notification did not get pushed"); Log.Error("Pushbullet api returned a null value, the notification did not get pushed");
@ -151,5 +128,17 @@ namespace Ombi.Services.Notification
Log.Error(e); Log.Error(e);
} }
} }
protected override async Task Test(NotificationModel model, PushbulletNotificationSettings settings)
{
var message = "This is just a test! Success!";
var pushTitle = "Ombi: Test Message!";
var notification = new NotificationMessage
{
Message = message,
Subject = pushTitle
};
await Send(notification,settings);
}
} }
} }

@ -30,67 +30,24 @@ using System.Threading.Tasks;
using NLog; using NLog;
using Ombi.Api.Interfaces; using Ombi.Api.Interfaces;
using Ombi.Core; using Ombi.Core;
using Ombi.Core.Models;
using Ombi.Core.SettingModels; using Ombi.Core.SettingModels;
using Ombi.Services.Interfaces;
using Ombi.Store; using Ombi.Store;
namespace Ombi.Services.Notification namespace Ombi.Services.Notification
{ {
public class PushoverNotification : INotification public class PushoverNotification : BaseNotification<PushoverNotificationSettings>
{ {
public PushoverNotification(IPushoverApi pushoverApi, ISettingsService<PushoverNotificationSettings> settings) public PushoverNotification(IPushoverApi pushoverApi, ISettingsService<PushoverNotificationSettings> settings) : base(settings)
{ {
PushoverApi = pushoverApi; PushoverApi = pushoverApi;
SettingsService = settings;
} }
private IPushoverApi PushoverApi { get; } private IPushoverApi PushoverApi { get; }
private ISettingsService<PushoverNotificationSettings> SettingsService { get; }
private static Logger Log = LogManager.GetCurrentClassLogger(); private static Logger Log = LogManager.GetCurrentClassLogger();
public string NotificationName => "PushoverNotification"; public override string NotificationName => "PushoverNotification";
public async Task NotifyAsync(NotificationModel model)
{
var configuration = GetSettings();
await NotifyAsync(model, configuration);
}
public async Task NotifyAsync(NotificationModel model, Settings settings)
{
if (settings == null) await NotifyAsync(model);
var pushSettings = (PushoverNotificationSettings)settings;
if (!ValidateConfiguration(pushSettings)) return;
switch (model.NotificationType)
{
case NotificationType.NewRequest:
await PushNewRequestAsync(model, pushSettings);
break;
case NotificationType.Issue:
await PushIssueAsync(model, pushSettings);
break;
case NotificationType.RequestAvailable:
break;
case NotificationType.RequestApproved:
break;
case NotificationType.AdminNote:
break;
case NotificationType.Test:
await PushTestAsync(model, pushSettings);
break;
case NotificationType.RequestDeclined:
break;
case NotificationType.ItemAddedToFaultQueue:
await PushFaultQueue(model, pushSettings);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private bool ValidateConfiguration(PushoverNotificationSettings settings) protected override bool ValidateConfiguration(PushoverNotificationSettings settings)
{ {
if (!settings.Enabled) if (!settings.Enabled)
{ {
@ -103,40 +60,57 @@ namespace Ombi.Services.Notification
return true; return true;
} }
private PushoverNotificationSettings GetSettings() protected override async Task NewRequest(NotificationModel model, PushoverNotificationSettings settings)
{
var message = $"Ombi: The {model.RequestType.GetString()?.ToLower()} '{model.Title}' has been requested by user: {model.User}";
var notification = new NotificationMessage
{ {
return SettingsService.GetSettings(); Message = message,
};
await Send(notification, settings);
} }
private async Task PushNewRequestAsync(NotificationModel model, PushoverNotificationSettings settings) protected override async Task Issue(NotificationModel model, PushoverNotificationSettings settings)
{ {
var message = $"Ombi: The {model.RequestType.GetString()?.ToLower()} '{model.Title}' has been requested by user: {model.User}"; var message = $"Ombi: A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}";
await Push(settings, message); var notification = new NotificationMessage
{
Message = message,
};
await Send(notification, settings);
} }
private async Task PushIssueAsync(NotificationModel model, PushoverNotificationSettings settings) protected override async Task AddedToRequestQueue(NotificationModel model, PushoverNotificationSettings settings)
{ {
var message = $"Ombi: A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}"; var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying";
await Push(settings, message);
var notification = new NotificationMessage
{
Message = message,
};
await Send(notification, settings);
} }
private async Task PushTestAsync(NotificationModel model, PushoverNotificationSettings settings) protected override Task RequestDeclined(NotificationModel model, PushoverNotificationSettings settings)
{ {
var message = $"Ombi: Test Message!"; throw new NotImplementedException();
await Push(settings, message);
} }
private async Task PushFaultQueue(NotificationModel model, PushoverNotificationSettings settings) protected override Task RequestApproved(NotificationModel model, PushoverNotificationSettings settings)
{ {
var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; throw new NotImplementedException();
await Push(settings, message); }
protected override Task AvailableRequest(NotificationModel model, PushoverNotificationSettings settings)
{
throw new NotImplementedException();
} }
private async Task Push(PushoverNotificationSettings settings, string message) protected override async Task Send(NotificationMessage model, PushoverNotificationSettings settings)
{ {
try try
{ {
var result = await PushoverApi.PushAsync(settings.AccessToken, message, settings.UserToken); var result = await PushoverApi.PushAsync(settings.AccessToken, model.Message, settings.UserToken);
if (result?.status != 1) if (result?.status != 1)
{ {
Log.Error("Pushover api returned a status that was not 1, the notification did not get pushed"); Log.Error("Pushover api returned a status that was not 1, the notification did not get pushed");
@ -147,5 +121,16 @@ namespace Ombi.Services.Notification
Log.Error(e); Log.Error(e);
} }
} }
protected override async Task Test(NotificationModel model, PushoverNotificationSettings settings)
{
var message = $"Ombi: Test Message!";
var notification = new NotificationMessage
{
Message = message,
};
await Send(notification, settings);
}
} }
} }

@ -31,101 +31,97 @@ using NLog;
using Ombi.Api.Interfaces; using Ombi.Api.Interfaces;
using Ombi.Api.Models.Notifications; using Ombi.Api.Models.Notifications;
using Ombi.Core; using Ombi.Core;
using Ombi.Core.Models;
using Ombi.Core.SettingModels; using Ombi.Core.SettingModels;
using Ombi.Services.Interfaces;
namespace Ombi.Services.Notification namespace Ombi.Services.Notification
{ {
public class SlackNotification : INotification public class SlackNotification : BaseNotification<SlackNotificationSettings>
{ {
public SlackNotification(ISlackApi api, ISettingsService<SlackNotificationSettings> sn) public SlackNotification(ISlackApi api, ISettingsService<SlackNotificationSettings> sn) : base(sn)
{ {
Api = api; Api = api;
Settings = sn;
} }
public string NotificationName => "SlackNotification"; public override string NotificationName => "SlackNotification";
private ISlackApi Api { get; } private ISlackApi Api { get; }
private ISettingsService<SlackNotificationSettings> Settings { get; } private static readonly Logger Log = LogManager.GetCurrentClassLogger();
private static Logger Log = LogManager.GetCurrentClassLogger();
public async Task NotifyAsync(NotificationModel model) protected override bool ValidateConfiguration(SlackNotificationSettings settings)
{ {
var settings = Settings.GetSettings(); if (!settings.Enabled)
{
await NotifyAsync(model, settings); return false;
} }
if (string.IsNullOrEmpty(settings.WebhookUrl))
public async Task NotifyAsync(NotificationModel model, Settings settings)
{ {
if (settings == null) await NotifyAsync(model); return false;
}
var pushSettings = (SlackNotificationSettings)settings; try
if (!ValidateConfiguration(pushSettings))
{ {
Log.Error("Settings for Slack was not correct, we cannot push a notification"); var a = settings.Team;
return; var b = settings.Service;
var c = settings.Token;
} }
catch (IndexOutOfRangeException)
switch (model.NotificationType)
{ {
case NotificationType.NewRequest: return false;
await PushNewRequestAsync(model, pushSettings);
break;
case NotificationType.Issue:
await PushIssueAsync(model, pushSettings);
break;
case NotificationType.RequestAvailable:
break;
case NotificationType.RequestApproved:
break;
case NotificationType.AdminNote:
break;
case NotificationType.Test:
await PushTest(pushSettings);
break;
case NotificationType.RequestDeclined:
break;
case NotificationType.ItemAddedToFaultQueue:
await PushFaultQueue(model, pushSettings);
break;
default:
throw new ArgumentOutOfRangeException();
} }
return true;
} }
private async Task PushNewRequestAsync(NotificationModel model, SlackNotificationSettings settings) protected override async Task NewRequest(NotificationModel model, SlackNotificationSettings settings)
{ {
var message = $"{model.Title} has been requested by user: {model.User}"; var message = $"{model.Title} has been requested by user: {model.User}";
await Push(settings, message); var notification = new NotificationMessage
{
Message = message,
};
await Send(notification, settings);
} }
private async Task PushIssueAsync(NotificationModel model, SlackNotificationSettings settings) protected override async Task Issue(NotificationModel model, SlackNotificationSettings settings)
{ {
var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}"; var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}";
await Push(settings, message); var notification = new NotificationMessage
{
Message = message,
};
await Send(notification, settings);
} }
private async Task PushTest(SlackNotificationSettings settings) protected override async Task AddedToRequestQueue(NotificationModel model, SlackNotificationSettings settings)
{ {
var message = $"This is a test from Ombi, if you can see this then we have successfully pushed a notification!"; var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying";
await Push(settings, message);
var notification = new NotificationMessage
{
Message = message,
};
await Send(notification, settings);
} }
private async Task PushFaultQueue(NotificationModel model, SlackNotificationSettings settings) protected override Task RequestDeclined(NotificationModel model, SlackNotificationSettings settings)
{ {
var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying"; throw new NotImplementedException();
await Push(settings, message);
} }
private async Task Push(SlackNotificationSettings config, string message) protected override Task RequestApproved(NotificationModel model, SlackNotificationSettings settings)
{
throw new NotImplementedException();
}
protected override Task AvailableRequest(NotificationModel model, SlackNotificationSettings settings)
{
throw new NotImplementedException();
}
protected override async Task Send(NotificationMessage model, SlackNotificationSettings config)
{ {
try try
{ {
var notification = new SlackNotificationBody { username = config.Username, channel = config.Channel ?? string.Empty, text = message }; var notification = new SlackNotificationBody { username = config.Username, channel = config.Channel ?? string.Empty, text = model.Message };
var result = await Api.PushAsync(config.Team, config.Token, config.Service, notification); var result = await Api.PushAsync(config.Team, config.Token, config.Service, notification);
if (!result.Equals("ok")) if (!result.Equals("ok"))
@ -140,27 +136,14 @@ namespace Ombi.Services.Notification
} }
} }
private bool ValidateConfiguration(SlackNotificationSettings settings) protected override async Task Test(NotificationModel model, SlackNotificationSettings settings)
{
if (!settings.Enabled)
{
return false;
}
if (string.IsNullOrEmpty(settings.WebhookUrl))
{
return false;
}
try
{ {
var a = settings.Team; var message = $"This is a test from Ombi, if you can see this then we have successfully pushed a notification!";
var b = settings.Service; var notification = new NotificationMessage
var c = settings.Token;
}
catch (IndexOutOfRangeException)
{ {
return false; Message = message,
} };
return true; await Send(notification, settings);
} }
} }
} }

@ -147,6 +147,7 @@
<Compile Include="Notification\BaseNotification.cs" /> <Compile Include="Notification\BaseNotification.cs" />
<Compile Include="Notification\EmailMessageNotification.cs" /> <Compile Include="Notification\EmailMessageNotification.cs" />
<Compile Include="Notification\EmbyNotificationEngine.cs" /> <Compile Include="Notification\EmbyNotificationEngine.cs" />
<Compile Include="Notification\NotificationMessage.cs" />
<Compile Include="Notification\PlexNotificationEngine.cs" /> <Compile Include="Notification\PlexNotificationEngine.cs" />
<Compile Include="Notification\NotificationModel.cs" /> <Compile Include="Notification\NotificationModel.cs" />
<Compile Include="Notification\NotificationService.cs" /> <Compile Include="Notification\NotificationService.cs" />

@ -175,14 +175,6 @@ namespace Ombi.UI.Modules
isOwner = true; isOwner = true;
userId = GetOwnerId(plexSettings.PlexAuthToken, username); userId = GetOwnerId(plexSettings.PlexAuthToken, username);
} }
UsersModel dbUser = await IsDbuser(username);
if (dbUser != null) // in the db?
{
var perms = (Permissions) dbUser.Permissions;
authenticated = true;
isOwner = perms.HasFlag(Permissions.Administrator);
userId = dbUser.UserGuid;
}
Log.Debug("Friends list result = {0}", authenticated); Log.Debug("Friends list result = {0}", authenticated);
} }
else if (!settings.UserAuthentication) // No auth, let them pass! else if (!settings.UserAuthentication) // No auth, let them pass!
@ -207,6 +199,14 @@ namespace Ombi.UI.Modules
authenticated = true; authenticated = true;
isOwner = true; isOwner = true;
} }
Log.Debug("Friends list result = {0}", authenticated);
}
else if (!settings.UserAuthentication) // No auth, let them pass!
{
authenticated = true;
}
}
UsersModel dbUser = await IsDbuser(username); UsersModel dbUser = await IsDbuser(username);
if (dbUser != null) // in the db? if (dbUser != null) // in the db?
{ {
@ -215,13 +215,7 @@ namespace Ombi.UI.Modules
isOwner = perms.HasFlag(Permissions.Administrator); isOwner = perms.HasFlag(Permissions.Administrator);
userId = dbUser.UserGuid; userId = dbUser.UserGuid;
} }
Log.Debug("Friends list result = {0}", authenticated);
}
else if (!settings.UserAuthentication) // No auth, let them pass!
{
authenticated = true;
}
}
if (settings.UsePassword || isOwner || Security.HasPermissions(username, Permissions.Administrator)) if (settings.UsePassword || isOwner || Security.HasPermissions(username, Permissions.Administrator))
{ {
Session[SessionKeys.UserLoginName] = username; Session[SessionKeys.UserLoginName] = username;

@ -78,19 +78,21 @@ namespace Ombi.UI.Modules
private async Task<Response> LoadUsers() private async Task<Response> LoadUsers()
{ {
var model = new List<UserManagementUsersViewModel>();
var plexSettings = await PlexSettings.GetSettingsAsync(); var plexSettings = await PlexSettings.GetSettingsAsync();
var embySettings = await EmbySettings.GetSettingsAsync(); var embySettings = await EmbySettings.GetSettingsAsync();
if (plexSettings.Enable) if (plexSettings.Enable)
{ {
return await LoadPlexUsers(); model.AddRange(await LoadPlexUsers());
} }
if (embySettings.Enable) if (embySettings.Enable)
{ {
return await LoadEmbyUsers(); model.AddRange(await LoadEmbyUsers());
} }
return null; model.AddRange(await LoadLocalUsers());
return Response.AsJson(model);
} }
private async Task<Response> CreateUser() private async Task<Response> CreateUser()
@ -547,19 +549,26 @@ namespace Ombi.UI.Modules
return retVal; return retVal;
} }
private async Task<Response> LoadPlexUsers() private async Task<IEnumerable<UserManagementUsersViewModel>> LoadLocalUsers()
{ {
var localUsers = await UserMapper.GetUsersAsync();
var plexDbUsers = await PlexUsersRepository.GetAllAsync();
var model = new List<UserManagementUsersViewModel>();
var userLogins = UserLoginsRepo.GetAll().ToList(); var localUsers = await UserMapper.GetUsersAsync(); var userLogins = UserLoginsRepo.GetAll().ToList();
var model = new List<UserManagementUsersViewModel>();
foreach (var user in localUsers) foreach (var user in localUsers)
{ {
var userDb = userLogins.FirstOrDefault(x => x.UserId == user.UserGuid); var userDb = userLogins.FirstOrDefault(x => x.UserId == user.UserGuid);
model.Add(MapLocalUser(user, userDb?.LastLoggedIn ?? DateTime.MinValue)); model.Add(MapLocalUser(user, userDb?.LastLoggedIn ?? DateTime.MinValue));
} }
return model;
}
private async Task<IEnumerable<UserManagementUsersViewModel>> LoadPlexUsers()
{
var plexDbUsers = await PlexUsersRepository.GetAllAsync();
var model = new List<UserManagementUsersViewModel>();
var userLogins = UserLoginsRepo.GetAll().ToList();
var plexSettings = await PlexSettings.GetSettingsAsync(); var plexSettings = await PlexSettings.GetSettingsAsync();
if (!string.IsNullOrEmpty(plexSettings.PlexAuthToken)) if (!string.IsNullOrEmpty(plexSettings.PlexAuthToken))
@ -595,23 +604,16 @@ namespace Ombi.UI.Modules
model.Add(MapPlexAdmin(account, dbUser, userDb?.LastLoggedIn ?? DateTime.MinValue)); model.Add(MapPlexAdmin(account, dbUser, userDb?.LastLoggedIn ?? DateTime.MinValue));
} }
} }
return Response.AsJson(model); return model;
} }
private async Task<Response> LoadEmbyUsers() private async Task<IEnumerable<UserManagementUsersViewModel>> LoadEmbyUsers()
{ {
var localUsers = await UserMapper.GetUsersAsync();
var embyDbUsers = await EmbyRepository.GetAllAsync(); var embyDbUsers = await EmbyRepository.GetAllAsync();
var model = new List<UserManagementUsersViewModel>(); var model = new List<UserManagementUsersViewModel>();
var userLogins = UserLoginsRepo.GetAll().ToList(); var userLogins = UserLoginsRepo.GetAll().ToList();
foreach (var user in localUsers)
{
var userDb = userLogins.FirstOrDefault(x => x.UserId == user.UserGuid);
model.Add(MapLocalUser(user, userDb?.LastLoggedIn ?? DateTime.MinValue));
}
var embySettings = await EmbySettings.GetSettingsAsync(); var embySettings = await EmbySettings.GetSettingsAsync();
if (!string.IsNullOrEmpty(embySettings.ApiKey)) if (!string.IsNullOrEmpty(embySettings.ApiKey))
{ {
@ -631,7 +633,7 @@ namespace Ombi.UI.Modules
} }
} }
} }
return Response.AsJson(model); return model;
} }
} }
} }

@ -59,7 +59,7 @@
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<PostBuildEvent>(ROBOCOPY "$(ProjectDir)bin\$(ConfigurationName)\UpdateService" "$(SolutionDir)Ombi.UI\bin\$(ConfigurationName)\UpdateService" "*")^&amp; IF %25ERRORLEVEL%25 GEQ 16 exit 1 <PostBuildEvent>(ROBOCOPY "$(ProjectDir)bin\$(ConfigurationName)\UpdateService" "$(SolutionDir)Ombi.UI\bin\$(ConfigurationName)\UpdateService" "*")^&amp; IF %25ERRORLEVEL%25 LEQ 2 exit 0
exit 0</PostBuildEvent> exit 0</PostBuildEvent>
</PropertyGroup> </PropertyGroup>
</Project> </Project>
Loading…
Cancel
Save