1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Core/Notifications/NotificationService.cs

216 lines
7.6 KiB
C#
Raw Normal View History

2013-05-20 01:17:32 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common.Composition;
using NzbDrone.Common.Messaging;
2013-05-29 04:49:17 +02:00
using NzbDrone.Common.Serializer;
2013-05-20 01:17:32 +02:00
using NzbDrone.Core.Download;
using NzbDrone.Core.MediaFiles.Events;
2013-06-05 05:13:57 +02:00
using NzbDrone.Core.Tv;
2013-05-29 08:24:45 +02:00
using Omu.ValueInjecter;
2013-05-20 01:17:32 +02:00
namespace NzbDrone.Core.Notifications
{
public interface INotificationService
{
List<Notification> All();
Notification Get(int id);
List<Notification> Schema();
2013-05-29 04:49:17 +02:00
Notification Create(Notification notification);
Notification Update(Notification notification);
void Delete(int id);
2013-05-20 01:17:32 +02:00
}
public class NotificationService
: INotificationService,
IHandle<EpisodeGrabbedEvent>,
IHandle<EpisodeDownloadedEvent>,
IHandle<SeriesRenamedEvent>
{
private readonly INotificationRepository _notificationRepository;
private readonly IContainer _container;
private readonly List<INotification> _notifications;
private readonly Logger _logger;
public NotificationService(INotificationRepository notificationRepository,
IEnumerable<INotification> notifications,
IContainer container,
Logger logger)
{
_notificationRepository = notificationRepository;
_container = container;
_notifications = notifications.ToList();
_logger = logger;
}
public List<Notification> All()
{
return _notificationRepository.All().Select(ToNotification).ToList();
}
public Notification Get(int id)
{
return ToNotification(_notificationRepository.Get(id));
}
public List<Notification> Schema()
{
var notifications = new List<Notification>();
int i = 1;
foreach (var notification in _notifications)
{
var type = notification.GetType();
var newNotification = new Notification();
newNotification.Instance = (INotification)_container.Resolve(type);
newNotification.Id = i;
newNotification.ImplementationName = notification.ImplementationName;
newNotification.Link = notification.Link;
var instanceType = newNotification.Instance.GetType();
var baseGenArgs = instanceType.BaseType.GetGenericArguments();
newNotification.Settings = (INotifcationSettings)Activator.CreateInstance(baseGenArgs[0]);
newNotification.Implementation = type.Name;
notifications.Add(newNotification);
2013-05-27 08:01:27 +02:00
i++;
}
2013-05-27 08:01:27 +02:00
return notifications.OrderBy(n => n.Name).ToList();
}
2013-05-29 04:49:17 +02:00
public Notification Create(Notification notification)
{
2013-05-29 08:24:45 +02:00
var definition = new NotificationDefinition();
definition.InjectFrom(notification);
definition.Settings = notification.Settings.ToJson();
2013-05-29 04:49:17 +02:00
definition = _notificationRepository.Insert(definition);
notification.Id = definition.Id;
return notification;
}
public Notification Update(Notification notification)
{
var definition = _notificationRepository.Get(notification.Id);
2013-05-29 08:24:45 +02:00
definition.InjectFrom(notification);
definition.Settings = notification.Settings.ToJson();
2013-05-29 04:49:17 +02:00
_notificationRepository.Update(definition);
return notification;
}
public void Delete(int id)
{
_notificationRepository.Delete(id);
}
2013-05-20 01:17:32 +02:00
private Notification ToNotification(NotificationDefinition definition)
{
var notification = new Notification();
notification.Id = definition.Id;
notification.OnGrab = definition.OnGrab;
notification.OnDownload = definition.OnDownload;
notification.Instance = GetInstance(definition);
notification.Name = definition.Name;
notification.Implementation = definition.Implementation;
notification.ImplementationName = notification.Instance.ImplementationName;
2013-05-20 04:43:22 +02:00
notification.Settings = ((dynamic)notification.Instance).ImportSettingsFromJson(definition.Settings);
2013-05-20 01:17:32 +02:00
return notification;
}
private INotification GetInstance(NotificationDefinition indexerDefinition)
{
var type = _notifications.Single(c => c.GetType().Name.Equals(indexerDefinition.Implementation, StringComparison.InvariantCultureIgnoreCase)).GetType();
var instance = (INotification)_container.Resolve(type);
instance.InstanceDefinition = indexerDefinition;
return instance;
}
2013-08-01 09:11:42 +02:00
private string GetMessage(Series series, List<Episode> episodes, QualityModel quality)
2013-06-05 04:15:39 +02:00
{
2013-07-26 17:22:02 +02:00
if (series.SeriesType == SeriesTypes.Daily)
{
2013-08-01 09:11:42 +02:00
var episode = episodes.First();
return String.Format("{0} - {1} - {2} [{3}]",
series.Title,
episode.AirDate,
episode.Title,
quality);
2013-07-26 17:22:02 +02:00
}
2013-08-01 09:11:42 +02:00
var episodeNumbers = String.Concat(episodes.Select(e => e.EpisodeNumber)
.Select(i => String.Format("x{0:00}", i)));
var episodeTitles = String.Join(" + ", episodes.Select(e => e.Title));
return String.Format("{0} - {1}{2} - {3} {4}",
series.Title,
episodes.First().SeasonNumber,
episodeNumbers,
episodeTitles,
quality);
2013-06-05 04:15:39 +02:00
}
2013-05-20 01:17:32 +02:00
public void Handle(EpisodeGrabbedEvent message)
{
2013-08-01 09:11:42 +02:00
var messageBody = GetMessage(message.Episode.Series, message.Episode.Episodes, message.Episode.ParsedEpisodeInfo.Quality);
2013-07-26 17:22:02 +02:00
foreach (var notification in All().Where(n => n.OnGrab))
{
try
{
notification.Instance.OnGrab(messageBody);
}
catch (Exception ex)
{
2013-07-31 18:43:38 +02:00
_logger.ErrorException("Unable to send OnGrab notification to: " + notification.Name, ex);
}
}
2013-05-20 01:17:32 +02:00
}
public void Handle(EpisodeDownloadedEvent message)
{
2013-08-01 09:11:42 +02:00
var messageBody = GetMessage(message.Episode.Series, message.Episode.Episodes, message.Episode.ParsedEpisodeInfo.Quality);
2013-07-26 17:22:02 +02:00
foreach (var notification in All().Where(n => n.OnDownload))
{
try
{
2013-08-01 09:11:42 +02:00
notification.Instance.OnDownload(messageBody, message.Episode.Series);
}
catch (Exception ex)
{
_logger.WarnException("Unable to send OnDownload notification to: " + notification.Name, ex);
}
}
2013-05-20 01:17:32 +02:00
}
public void Handle(SeriesRenamedEvent message)
{
foreach (var notification in All().Where(n => n.OnDownload))
{
try
{
notification.Instance.AfterRename(message.Series);
}
catch (Exception ex)
{
_logger.WarnException("Unable to send AfterRename notification to: " + notification.Name, ex);
}
}
2013-05-20 01:17:32 +02:00
}
}
}