2013-05-20 02:17:32 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-05-21 09:16:19 +03:00
|
|
|
using Newtonsoft.Json;
|
2013-05-20 02:17:32 +03:00
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Common.Composition;
|
|
|
|
using NzbDrone.Common.Messaging;
|
2013-05-29 05:49:17 +03:00
|
|
|
using NzbDrone.Common.Serializer;
|
2013-05-20 02:17:32 +03:00
|
|
|
using NzbDrone.Core.Download;
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-06-05 05:15:39 +03:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-06-05 06:13:57 +03:00
|
|
|
using NzbDrone.Core.Tv;
|
2013-05-29 09:24:45 +03:00
|
|
|
using Omu.ValueInjecter;
|
2013-05-20 02:17:32 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Notifications
|
|
|
|
{
|
|
|
|
public interface INotificationService
|
|
|
|
{
|
|
|
|
List<Notification> All();
|
2013-05-21 09:16:19 +03:00
|
|
|
Notification Get(int id);
|
2013-05-25 04:51:25 +03:00
|
|
|
List<Notification> Schema();
|
2013-05-29 05:49:17 +03:00
|
|
|
Notification Create(Notification notification);
|
|
|
|
Notification Update(Notification notification);
|
|
|
|
void Delete(int id);
|
2013-05-20 02:17:32 +03: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();
|
|
|
|
}
|
|
|
|
|
2013-05-21 09:16:19 +03:00
|
|
|
public Notification Get(int id)
|
|
|
|
{
|
|
|
|
return ToNotification(_notificationRepository.Get(id));
|
|
|
|
}
|
|
|
|
|
2013-05-25 04:51:25 +03:00
|
|
|
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;
|
2013-06-13 09:41:26 +03:00
|
|
|
newNotification.ImplementationName = notification.ImplementationName;
|
2013-05-25 04:51:25 +03:00
|
|
|
|
|
|
|
var instanceType = newNotification.Instance.GetType();
|
|
|
|
var baseGenArgs = instanceType.BaseType.GetGenericArguments();
|
2013-05-30 00:29:51 +03:00
|
|
|
newNotification.Settings = (INotifcationSettings)Activator.CreateInstance(baseGenArgs[0]);
|
2013-05-25 04:51:25 +03:00
|
|
|
newNotification.Implementation = type.Name;
|
|
|
|
|
|
|
|
notifications.Add(newNotification);
|
2013-05-27 09:01:27 +03:00
|
|
|
i++;
|
2013-05-25 04:51:25 +03:00
|
|
|
}
|
|
|
|
|
2013-05-27 09:01:27 +03:00
|
|
|
return notifications.OrderBy(n => n.Name).ToList();
|
2013-05-25 04:51:25 +03:00
|
|
|
}
|
|
|
|
|
2013-05-29 05:49:17 +03:00
|
|
|
public Notification Create(Notification notification)
|
|
|
|
{
|
2013-05-29 09:24:45 +03:00
|
|
|
var definition = new NotificationDefinition();
|
|
|
|
definition.InjectFrom(notification);
|
2013-05-30 00:29:51 +03:00
|
|
|
definition.Settings = notification.Settings.ToJson();
|
2013-05-29 05:49:17 +03: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 09:24:45 +03:00
|
|
|
definition.InjectFrom(notification);
|
2013-05-30 00:29:51 +03:00
|
|
|
definition.Settings = notification.Settings.ToJson();
|
2013-05-29 05:49:17 +03:00
|
|
|
|
|
|
|
_notificationRepository.Update(definition);
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Delete(int id)
|
|
|
|
{
|
|
|
|
_notificationRepository.Delete(id);
|
|
|
|
}
|
|
|
|
|
2013-05-20 02:17:32 +03: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;
|
2013-05-21 09:16:19 +03:00
|
|
|
notification.Implementation = definition.Implementation;
|
2013-06-13 09:41:26 +03:00
|
|
|
notification.ImplementationName = notification.Instance.ImplementationName;
|
2013-05-20 05:43:22 +03:00
|
|
|
notification.Settings = ((dynamic)notification.Instance).ImportSettingsFromJson(definition.Settings);
|
2013-05-20 02:17:32 +03: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-06-05 06:13:57 +03:00
|
|
|
private string GetMessage(ParsedEpisodeInfo parsedEpisodeInfo, Series series)
|
2013-06-05 05:15:39 +03:00
|
|
|
{
|
|
|
|
return String.Format("{0} - {1}{2}",
|
2013-06-05 06:13:57 +03:00
|
|
|
series.Title,
|
2013-06-05 05:15:39 +03:00
|
|
|
parsedEpisodeInfo.SeasonNumber,
|
|
|
|
String.Concat(parsedEpisodeInfo.EpisodeNumbers.Select(i => String.Format("x{0:00}", i))));
|
|
|
|
}
|
|
|
|
|
2013-05-20 02:17:32 +03:00
|
|
|
public void Handle(EpisodeGrabbedEvent message)
|
|
|
|
{
|
|
|
|
All().Where(n => n.OnGrab)
|
|
|
|
.ToList()
|
|
|
|
.ForEach(notification =>
|
|
|
|
notification.Instance
|
2013-06-05 06:13:57 +03:00
|
|
|
.OnGrab(GetMessage(message.Episode.ParsedEpisodeInfo, message.Episode.Series))
|
2013-05-20 02:17:32 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(EpisodeDownloadedEvent message)
|
|
|
|
{
|
|
|
|
All().Where(n => n.OnDownload)
|
|
|
|
.ToList()
|
|
|
|
.ForEach(notification =>
|
|
|
|
notification.Instance
|
2013-06-05 06:13:57 +03:00
|
|
|
.OnDownload(GetMessage(message.ParsedEpisodeInfo, message.Series), message.Series)
|
2013-05-20 02:17:32 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(SeriesRenamedEvent message)
|
|
|
|
{
|
|
|
|
All().Where(n => n.OnDownload)
|
|
|
|
.ToList()
|
|
|
|
.ForEach(notification =>
|
|
|
|
notification.Instance
|
2013-06-05 05:15:39 +03:00
|
|
|
.OnDownload(message.Series.Title, message.Series)
|
2013-05-20 02:17:32 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|