2013-10-23 09:36:31 +03:00
|
|
|
using System;
|
2014-04-01 23:07:41 +03:00
|
|
|
using System.Linq;
|
2014-12-02 08:26:25 +02:00
|
|
|
using NzbDrone.Common.Extensions;
|
2014-02-02 01:07:30 +03:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-10-23 09:36:31 +03:00
|
|
|
using NzbDrone.Core.Download;
|
2014-02-02 01:07:30 +03:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
2013-10-23 09:36:31 +03:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2014-02-23 03:17:19 +03:00
|
|
|
using NzbDrone.Core.Tv.Events;
|
2013-10-23 09:36:31 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Blacklisting
|
|
|
|
{
|
|
|
|
public interface IBlacklistService
|
|
|
|
{
|
2014-12-19 02:26:42 +02:00
|
|
|
bool Blacklisted(int seriesId, string sourceTitle, DateTime publishedDate);
|
2014-02-02 01:07:30 +03:00
|
|
|
PagingSpec<Blacklist> Paged(PagingSpec<Blacklist> pagingSpec);
|
|
|
|
void Delete(int id);
|
2013-10-23 09:36:31 +03:00
|
|
|
}
|
|
|
|
|
2014-04-01 23:07:41 +03:00
|
|
|
public class BlacklistService : IBlacklistService,
|
|
|
|
IExecute<ClearBlacklistCommand>,
|
|
|
|
IHandle<DownloadFailedEvent>,
|
2014-12-19 02:26:42 +02:00
|
|
|
IHandleAsync<SeriesDeletedEvent>
|
2013-10-23 09:36:31 +03:00
|
|
|
{
|
|
|
|
private readonly IBlacklistRepository _blacklistRepository;
|
|
|
|
|
2014-12-19 02:26:42 +02:00
|
|
|
public BlacklistService(IBlacklistRepository blacklistRepository)
|
2013-10-23 09:36:31 +03:00
|
|
|
{
|
|
|
|
_blacklistRepository = blacklistRepository;
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:07:41 +03:00
|
|
|
public bool Blacklisted(int seriesId, string sourceTitle, DateTime publishedDate)
|
2013-10-23 09:36:31 +03:00
|
|
|
{
|
2014-04-01 23:07:41 +03:00
|
|
|
var blacklisted = _blacklistRepository.Blacklisted(seriesId, sourceTitle);
|
|
|
|
|
|
|
|
return blacklisted.Any(item => HasSamePublishedDate(item, publishedDate));
|
2013-10-23 09:36:31 +03:00
|
|
|
}
|
|
|
|
|
2014-02-02 01:07:30 +03:00
|
|
|
public PagingSpec<Blacklist> Paged(PagingSpec<Blacklist> pagingSpec)
|
|
|
|
{
|
|
|
|
return _blacklistRepository.GetPaged(pagingSpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Delete(int id)
|
|
|
|
{
|
|
|
|
_blacklistRepository.Delete(id);
|
|
|
|
}
|
|
|
|
|
2014-12-19 02:26:42 +02:00
|
|
|
private static bool HasSamePublishedDate(Blacklist item, DateTime publishedDate)
|
2014-04-01 23:07:41 +03:00
|
|
|
{
|
|
|
|
if (!item.PublishedDate.HasValue) return true;
|
|
|
|
|
|
|
|
return item.PublishedDate.Value.AddDays(-2) <= publishedDate &&
|
|
|
|
item.PublishedDate.Value.AddDays(2) >= publishedDate;
|
|
|
|
}
|
|
|
|
|
2014-02-23 03:17:19 +03:00
|
|
|
public void Execute(ClearBlacklistCommand message)
|
|
|
|
{
|
|
|
|
_blacklistRepository.Purge();
|
|
|
|
}
|
|
|
|
|
2013-10-23 09:36:31 +03:00
|
|
|
public void Handle(DownloadFailedEvent message)
|
|
|
|
{
|
|
|
|
var blacklist = new Blacklist
|
|
|
|
{
|
2013-10-24 08:13:04 +03:00
|
|
|
SeriesId = message.SeriesId,
|
|
|
|
EpisodeIds = message.EpisodeIds,
|
2013-10-23 09:36:31 +03:00
|
|
|
SourceTitle = message.SourceTitle,
|
|
|
|
Quality = message.Quality,
|
2014-04-01 23:07:41 +03:00
|
|
|
Date = DateTime.UtcNow,
|
2014-12-19 02:26:42 +02:00
|
|
|
PublishedDate = DateTime.Parse(message.Data.GetValueOrDefault("publishedDate"))
|
2013-10-23 09:36:31 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_blacklistRepository.Insert(blacklist);
|
|
|
|
}
|
2014-02-02 01:07:30 +03:00
|
|
|
|
2014-12-19 02:26:42 +02:00
|
|
|
public void HandleAsync(SeriesDeletedEvent message)
|
2014-02-02 01:07:30 +03:00
|
|
|
{
|
2014-02-23 03:17:19 +03:00
|
|
|
var blacklisted = _blacklistRepository.BlacklistedBySeries(message.Series.Id);
|
|
|
|
|
|
|
|
_blacklistRepository.DeleteMany(blacklisted);
|
2014-02-02 01:07:30 +03:00
|
|
|
}
|
2013-10-23 09:36:31 +03:00
|
|
|
}
|
|
|
|
}
|