Refactored the Notification service to how it should have really been done in the first place.

This commit is contained in:
tidusjar 2016-03-24 17:14:45 +00:00
commit 3fe1f13bd1
17 changed files with 220 additions and 166 deletions

View file

@ -27,11 +27,13 @@
using System;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
using NLog;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Services.Interfaces;
namespace PlexRequests.Services.Notification
{
@ -47,31 +49,35 @@ namespace PlexRequests.Services.Notification
private EmailNotificationSettings Settings => GetConfiguration();
public string NotificationName => "EmailMessageNotification";
public bool Notify(NotificationModel model)
public async Task NotifyAsync(NotificationModel model)
{
var configuration = GetConfiguration();
if (!ValidateConfiguration(configuration))
{
return false;
return;
}
switch (model.NotificationType)
{
case NotificationType.NewRequest:
return EmailNewRequest(model);
await EmailNewRequest(model);
break;
case NotificationType.Issue:
return EmailIssue(model);
await EmailIssue(model);
break;
case NotificationType.RequestAvailable:
break;
throw new NotImplementedException();
case NotificationType.RequestApproved:
break;
throw new NotImplementedException();
case NotificationType.AdminNote:
break;
throw new NotImplementedException();
default:
throw new ArgumentOutOfRangeException();
}
return false;
}
private EmailNotificationSettings GetConfiguration()
@ -94,7 +100,7 @@ namespace PlexRequests.Services.Notification
return true;
}
private bool EmailNewRequest(NotificationModel model)
private async Task EmailNewRequest(NotificationModel model)
{
var message = new MailMessage
{
@ -111,22 +117,20 @@ namespace PlexRequests.Services.Notification
{
smtp.Credentials = new NetworkCredential(Settings.EmailUsername, Settings.EmailPassword);
smtp.EnableSsl = Settings.Ssl;
smtp.Send(message);
return true;
await smtp.SendMailAsync(message).ConfigureAwait(false);
}
}
catch (SmtpException smtp)
{
Log.Fatal(smtp);
Log.Error(smtp);
}
catch (Exception e)
{
Log.Fatal(e);
Log.Error(e);
}
return false;
}
private bool EmailIssue(NotificationModel model)
private async Task EmailIssue(NotificationModel model)
{
var message = new MailMessage
{
@ -143,19 +147,17 @@ namespace PlexRequests.Services.Notification
{
smtp.Credentials = new NetworkCredential(Settings.EmailUsername, Settings.EmailPassword);
smtp.EnableSsl = Settings.Ssl;
smtp.Send(message);
return true;
await smtp.SendMailAsync(message).ConfigureAwait(false);
}
}
catch (SmtpException smtp)
{
Log.Fatal(smtp);
Log.Error(smtp);
}
catch (Exception e)
{
Log.Fatal(e);
Log.Error(e);
}
return false;
}
}
}

View file

@ -1,45 +0,0 @@
#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="model">The model.</param>
/// <returns></returns>
bool Notify(NotificationModel model);
}
}

View file

@ -24,64 +24,49 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System.Collections.Generic;
using System.Threading;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
using NLog;
using PlexRequests.Helpers;
using PlexRequests.Services.Interfaces;
namespace PlexRequests.Services.Notification
{
public static class NotificationService
public class NotificationService : INotificationService
{
private static Logger Log = LogManager.GetCurrentClassLogger();
public static Dictionary<string, INotification> Observers { get; }
public ConcurrentDictionary<string, INotification> Observers { get; } = new ConcurrentDictionary<string, INotification>();
static NotificationService()
public async Task Publish(NotificationModel model)
{
Observers = new Dictionary<string, INotification>();
var notificationTasks = Observers.Values.Select(notification => NotifyAsync(notification, model));
await Task.WhenAll(notificationTasks).ConfigureAwait(false);
}
public static void Publish(NotificationModel model)
public void Subscribe(INotification notification)
{
Log.Trace("Notifying all observers: ");
Log.Trace(Observers.DumpJson());
foreach (var observer in Observers)
{
var notification = observer.Value;
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
notification.Notify(model);
}).Start();
}
Observers.TryAdd(notification.NotificationName, notification);
}
public static void Subscribe(INotification notification)
public void UnSubscribe(INotification notification)
{
INotification notificationValue;
if (Observers.TryGetValue(notification.NotificationName, out notificationValue))
{
return;
}
Observers[notification.NotificationName] = notification;
Observers.TryRemove(notification.NotificationName, out notification);
}
public static void UnSubscribe(INotification notification)
private static async Task NotifyAsync(INotification notification, NotificationModel model)
{
Log.Trace("Unsubscribing Observer {0}", notification.NotificationName);
INotification notificationValue;
if (!Observers.TryGetValue(notification.NotificationName, out notificationValue))
try
{
Log.Trace("Observer {0} doesn't exist to Unsubscribe", notification.NotificationName);
// Observer doesn't exists
return;
await notification.NotifyAsync(model).ConfigureAwait(false);
}
catch (Exception ex)
{
Log.Error(ex, $"Notification '{notification.NotificationName}' failed with exception");
}
Observers.Remove(notification.NotificationName);
}
}
}

View file

@ -25,12 +25,14 @@
// ************************************************************************/
#endregion
using System;
using System.Threading.Tasks;
using NLog;
using PlexRequests.Api.Interfaces;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Services.Interfaces;
namespace PlexRequests.Services.Notification
{
@ -41,27 +43,27 @@ namespace PlexRequests.Services.Notification
PushbulletApi = pushbulletApi;
SettingsService = settings;
}
private IPushbulletApi PushbulletApi { get; }
private IPushbulletApi PushbulletApi { get; }
private ISettingsService<PushbulletNotificationSettings> SettingsService { get; }
private PushbulletNotificationSettings Settings => GetSettings();
private static Logger Log = LogManager.GetCurrentClassLogger();
public string NotificationName => "PushbulletNotification";
public bool Notify(NotificationModel model)
public async Task NotifyAsync(NotificationModel model)
{
if (!ValidateConfiguration())
{
return false;
return;
}
switch (model.NotificationType)
{
case NotificationType.NewRequest:
return PushNewRequest(model);
await PushNewRequestAsync(model);
break;
case NotificationType.Issue:
return PushIssue(model);
await PushIssueAsync(model);
break;
case NotificationType.RequestAvailable:
break;
case NotificationType.RequestApproved:
@ -71,8 +73,6 @@ namespace PlexRequests.Services.Notification
default:
throw new ArgumentOutOfRangeException();
}
return false;
}
private bool ValidateConfiguration()
@ -93,42 +93,40 @@ namespace PlexRequests.Services.Notification
return SettingsService.GetSettings();
}
private bool PushNewRequest(NotificationModel model)
private async Task PushNewRequestAsync(NotificationModel model)
{
var message = $"{model.Title} has been requested by user: {model.User}";
var pushTitle = $"Plex Requests: {model.Title} has been requested!";
try
{
var result = PushbulletApi.Push(Settings.AccessToken, pushTitle, message, Settings.DeviceIdentifier);
if (result != null)
var result = await PushbulletApi.PushAsync(Settings.AccessToken, pushTitle, message, Settings.DeviceIdentifier);
if (result == null)
{
return true;
Log.Error("Pushbullet api returned a null value, the notification did not get pushed");
}
}
catch (Exception e)
{
Log.Fatal(e);
Log.Error(e);
}
return false;
}
private bool PushIssue(NotificationModel model)
private async Task PushIssueAsync(NotificationModel model)
{
var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}";
var pushTitle = $"Plex Requests: A new issue has been reported for {model.Title}";
try
{
var result = PushbulletApi.Push(Settings.AccessToken, pushTitle, message, Settings.DeviceIdentifier);
var result = await PushbulletApi.PushAsync(Settings.AccessToken, pushTitle, message, Settings.DeviceIdentifier);
if (result != null)
{
return true;
Log.Error("Pushbullet api returned a null value, the notification did not get pushed");
}
}
catch (Exception e)
{
Log.Fatal(e);
Log.Error(e);
}
return false;
}
}
}

View file

@ -25,12 +25,14 @@
// ************************************************************************/
#endregion
using System;
using System.Threading.Tasks;
using NLog;
using PlexRequests.Api.Interfaces;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Services.Interfaces;
namespace PlexRequests.Services.Notification
{
@ -47,21 +49,21 @@ namespace PlexRequests.Services.Notification
private static Logger Log = LogManager.GetCurrentClassLogger();
public string NotificationName => "PushoverNotification";
public bool Notify(NotificationModel model)
public async Task NotifyAsync(NotificationModel model)
{
if (!ValidateConfiguration())
{
return false;
return;
}
switch (model.NotificationType)
{
case NotificationType.NewRequest:
return PushNewRequest(model);
await PushNewRequestAsync(model);
break;
case NotificationType.Issue:
return PushIssue(model);
await PushIssueAsync(model);
break;
case NotificationType.RequestAvailable:
break;
case NotificationType.RequestApproved:
@ -71,8 +73,6 @@ namespace PlexRequests.Services.Notification
default:
throw new ArgumentOutOfRangeException();
}
return false;
}
private bool ValidateConfiguration()
@ -93,40 +93,38 @@ namespace PlexRequests.Services.Notification
return SettingsService.GetSettings();
}
private bool PushNewRequest(NotificationModel model)
private async Task PushNewRequestAsync(NotificationModel model)
{
var message = $"Plex Requests: {model.Title} has been requested by user: {model.User}";
try
{
var result = PushoverApi.Push(Settings.AccessToken, message, Settings.UserToken);
if (result?.status == 1)
var result = await PushoverApi.PushAsync(Settings.AccessToken, message, Settings.UserToken);
if (result?.status != 1)
{
return true;
Log.Error("Pushover api returned a status that was not 1, the notification did not get pushed");
}
}
catch (Exception e)
{
Log.Fatal(e);
Log.Error(e);
}
return false;
}
private bool PushIssue(NotificationModel model)
private async Task PushIssueAsync(NotificationModel model)
{
var message = $"Plex Requests: A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}";
try
{
var result = PushoverApi.Push(Settings.AccessToken, message, Settings.UserToken);
if (result != null)
var result = await PushoverApi.PushAsync(Settings.AccessToken, message, Settings.UserToken);
if (result?.status != 1)
{
return true;
Log.Error("Pushover api returned a status that was not 1, the notification did not get pushed");
}
}
catch (Exception e)
{
Log.Fatal(e);
Log.Error(e);
}
return false;
}
}
}