mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-14 18:47:15 -07:00
Added first implimentation of the Notification Service #8
Added tests to cover the notification service
This commit is contained in:
parent
aa5304b8dd
commit
b9a886d5dc
13 changed files with 410 additions and 10 deletions
108
PlexRequests.Services/Notification/EmailMessageNotification.cs
Normal file
108
PlexRequests.Services/Notification/EmailMessageNotification.cs
Normal file
|
@ -0,0 +1,108 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: EmailMessageNotification.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;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
|
||||
using NLog;
|
||||
|
||||
using PlexRequests.Core;
|
||||
using PlexRequests.Core.SettingModels;
|
||||
|
||||
namespace PlexRequests.Services.Notification
|
||||
{
|
||||
public class EmailMessageNotification : INotification
|
||||
{
|
||||
public EmailMessageNotification(ISettingsService<EmailNotificationSettings> settings)
|
||||
{
|
||||
EmailNotificationSettings = settings;
|
||||
}
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
private ISettingsService<EmailNotificationSettings> EmailNotificationSettings { get; }
|
||||
public string NotificationName => "EmailMessageNotification";
|
||||
public bool Notify(string title, string requester)
|
||||
{
|
||||
var configuration = GetConfiguration();
|
||||
if (!ValidateConfiguration(configuration))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var message = new MailMessage
|
||||
{
|
||||
IsBodyHtml = true,
|
||||
To = { new MailAddress(configuration.RecipientEmail) },
|
||||
Body = $"User {requester} has requested {title}!",
|
||||
From = new MailAddress(configuration.EmailUsername),
|
||||
Subject = $"New Request for {title}!"
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
using (var smtp = new SmtpClient(configuration.EmailHost, configuration.EmailPort))
|
||||
{
|
||||
smtp.Credentials = new NetworkCredential(configuration.EmailUsername, configuration.EmailPassword);
|
||||
smtp.EnableSsl = configuration.Ssl;
|
||||
smtp.Send(message);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (SmtpException smtp)
|
||||
{
|
||||
Log.Fatal(smtp);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Fatal(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private EmailNotificationSettings GetConfiguration()
|
||||
{
|
||||
var settings = EmailNotificationSettings.GetSettings();
|
||||
return settings;
|
||||
}
|
||||
|
||||
private bool ValidateConfiguration(EmailNotificationSettings settings)
|
||||
{
|
||||
if (!settings.Enabled)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (string.IsNullOrEmpty(settings.EmailHost) || string.IsNullOrEmpty(settings.EmailUsername)
|
||||
|| string.IsNullOrEmpty(settings.EmailPassword) || string.IsNullOrEmpty(settings.RecipientEmail)
|
||||
|| string.IsNullOrEmpty(settings.EmailPort.ToString()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
46
PlexRequests.Services/Notification/INotification.cs
Normal file
46
PlexRequests.Services/Notification/INotification.cs
Normal file
|
@ -0,0 +1,46 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: INotification.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
|
||||
namespace PlexRequests.Services.Notification
|
||||
{
|
||||
public interface INotification
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the name of the notification.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The name of the notification.
|
||||
/// </value>
|
||||
string NotificationName { get; }
|
||||
/// <summary>
|
||||
/// Notifies the specified title.
|
||||
/// </summary>
|
||||
/// <param name="title">The title.</param>
|
||||
/// <param name="requester">The requester.</param>
|
||||
/// <returns></returns>
|
||||
bool Notify(string title, string requester);
|
||||
}
|
||||
}
|
78
PlexRequests.Services/Notification/NotificationService.cs
Normal file
78
PlexRequests.Services/Notification/NotificationService.cs
Normal file
|
@ -0,0 +1,78 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: NotificationService.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;
|
||||
using System.Threading;
|
||||
|
||||
namespace PlexRequests.Services.Notification
|
||||
{
|
||||
public static class NotificationService
|
||||
{
|
||||
public static Dictionary<string, INotification> Observers { get; }
|
||||
|
||||
static NotificationService()
|
||||
{
|
||||
Observers = new Dictionary<string, INotification>();
|
||||
}
|
||||
|
||||
public static void Publish(string title, string requester)
|
||||
{
|
||||
foreach (var observer in Observers)
|
||||
{
|
||||
var notification = observer.Value;
|
||||
|
||||
new Thread(() =>
|
||||
{
|
||||
Thread.CurrentThread.IsBackground = true;
|
||||
notification.Notify(title, requester);
|
||||
}).Start();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Subscribe(INotification notification)
|
||||
{
|
||||
INotification notificationValue;
|
||||
if (Observers.TryGetValue(notification.NotificationName, out notificationValue))
|
||||
{
|
||||
// Observer already exists
|
||||
return;
|
||||
}
|
||||
|
||||
Observers[notification.NotificationName] = notification;
|
||||
}
|
||||
|
||||
public static void UnSubscribe(INotification notification)
|
||||
{
|
||||
INotification notificationValue;
|
||||
if (!Observers.TryGetValue(notification.NotificationName, out notificationValue))
|
||||
{
|
||||
// Observer doesn't exists
|
||||
return;
|
||||
}
|
||||
Observers.Remove(notification.NotificationName);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -77,6 +77,9 @@
|
|||
<Compile Include="Interfaces\IAvailabilityChecker.cs" />
|
||||
<Compile Include="Interfaces\IConfigurationReader.cs" />
|
||||
<Compile Include="Interfaces\IIntervals.cs" />
|
||||
<Compile Include="Notification\INotification.cs" />
|
||||
<Compile Include="Notification\EmailMessageNotification.cs" />
|
||||
<Compile Include="Notification\NotificationService.cs" />
|
||||
<Compile Include="PlexAvailabilityChecker.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="UpdateInterval.cs" />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue