2017-09-24 19:44:25 +00:00
|
|
|
using System;
|
2017-07-08 02:06:21 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2018-04-08 06:25:34 +00:00
|
|
|
using System.Linq.Expressions;
|
|
|
|
using System.Net.Sockets;
|
2017-07-08 02:06:21 +00:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Extensions;
|
2018-04-08 06:25:34 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
2017-07-08 02:06:21 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Music
|
|
|
|
{
|
|
|
|
public interface IAlbumMonitoredService
|
|
|
|
{
|
2018-01-28 06:27:33 +00:00
|
|
|
void SetAlbumMonitoredStatus(Artist artist, MonitoringOptions monitoringOptions);
|
2017-07-08 02:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class AlbumMonitoredService : IAlbumMonitoredService
|
|
|
|
{
|
|
|
|
private readonly IArtistService _artistService;
|
|
|
|
private readonly IAlbumService _albumService;
|
|
|
|
private readonly ITrackService _trackService;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public AlbumMonitoredService(IArtistService artistService, IAlbumService albumService, ITrackService trackService, Logger logger)
|
|
|
|
{
|
|
|
|
_artistService = artistService;
|
|
|
|
_albumService = albumService;
|
|
|
|
_trackService = trackService;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetAlbumMonitoredStatus(Artist artist, MonitoringOptions monitoringOptions)
|
|
|
|
{
|
|
|
|
if (monitoringOptions != null)
|
|
|
|
{
|
|
|
|
_logger.Debug("[{0}] Setting album monitored status.", artist.Name);
|
|
|
|
|
|
|
|
var albums = _albumService.GetAlbumsByArtist(artist.Id);
|
|
|
|
|
2018-04-08 06:25:34 +00:00
|
|
|
var albumsWithFiles = _albumService.GetArtistAlbumsWithFiles(artist);
|
|
|
|
|
|
|
|
var albumsWithoutFiles = albums.Where(c => !albumsWithFiles.Select(e => e.Id).Contains(c.Id) && c.ReleaseDate <= DateTime.UtcNow).ToList();
|
|
|
|
|
2018-02-06 23:08:36 +00:00
|
|
|
var monitoredAlbums = monitoringOptions.AlbumsToMonitor;
|
2018-01-28 06:27:33 +00:00
|
|
|
|
2018-04-08 06:25:34 +00:00
|
|
|
// If specific albums are passed use those instead of the monitoring options.
|
2018-02-06 23:08:36 +00:00
|
|
|
if (monitoredAlbums.Any())
|
2018-01-28 06:27:33 +00:00
|
|
|
{
|
|
|
|
ToggleAlbumsMonitoredState(
|
2018-02-06 23:08:36 +00:00
|
|
|
albums.Where(s => monitoredAlbums.Any(t => t == s.ForeignAlbumId)), true);
|
2018-01-28 06:27:33 +00:00
|
|
|
ToggleAlbumsMonitoredState(
|
2018-02-06 23:08:36 +00:00
|
|
|
albums.Where(s => monitoredAlbums.Any(t => t != s.ForeignAlbumId)), false);
|
2018-01-28 06:27:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-23 22:39:11 +00:00
|
|
|
switch (monitoringOptions.Monitor)
|
2018-04-08 06:25:34 +00:00
|
|
|
{
|
2019-02-23 22:39:11 +00:00
|
|
|
case MonitorTypes.All:
|
2018-04-08 06:25:34 +00:00
|
|
|
ToggleAlbumsMonitoredState(albums, true);
|
|
|
|
break;
|
2019-02-23 22:39:11 +00:00
|
|
|
case MonitorTypes.Future:
|
2018-04-08 06:25:34 +00:00
|
|
|
_logger.Debug("Unmonitoring Albums with Files");
|
|
|
|
ToggleAlbumsMonitoredState(albums.Where(e => albumsWithFiles.Select(c => c.Id).Contains(e.Id)), false);
|
|
|
|
_logger.Debug("Unmonitoring Albums without Files");
|
|
|
|
ToggleAlbumsMonitoredState(albums.Where(e => albumsWithoutFiles.Select(c => c.Id).Contains(e.Id)), false);
|
|
|
|
break;
|
2019-02-23 22:39:11 +00:00
|
|
|
case MonitorTypes.None:
|
2018-04-08 06:25:34 +00:00
|
|
|
ToggleAlbumsMonitoredState(albums, false);
|
|
|
|
break;
|
2019-02-23 22:39:11 +00:00
|
|
|
case MonitorTypes.Missing:
|
2018-04-08 06:25:34 +00:00
|
|
|
_logger.Debug("Unmonitoring Albums with Files");
|
|
|
|
ToggleAlbumsMonitoredState(albums.Where(e => albumsWithFiles.Select(c => c.Id).Contains(e.Id)), false);
|
|
|
|
_logger.Debug("Monitoring Albums without Files");
|
|
|
|
ToggleAlbumsMonitoredState(albums.Where(e => albumsWithoutFiles.Select(c => c.Id).Contains(e.Id)), true);
|
|
|
|
break;
|
2019-02-23 22:39:11 +00:00
|
|
|
case MonitorTypes.Existing:
|
2018-04-08 06:25:34 +00:00
|
|
|
_logger.Debug("Monitoring Albums with Files");
|
|
|
|
ToggleAlbumsMonitoredState(albums.Where(e => albumsWithFiles.Select(c => c.Id).Contains(e.Id)), true);
|
|
|
|
_logger.Debug("Unmonitoring Albums without Files");
|
|
|
|
ToggleAlbumsMonitoredState(albums.Where(e => albumsWithoutFiles.Select(c => c.Id).Contains(e.Id)), false);
|
|
|
|
break;
|
2019-02-23 22:39:11 +00:00
|
|
|
case MonitorTypes.Latest:
|
2018-04-08 06:25:34 +00:00
|
|
|
ToggleAlbumsMonitoredState(albums, false);
|
|
|
|
ToggleAlbumsMonitoredState(albums.OrderByDescending(e=>e.ReleaseDate).Take(1),true);
|
|
|
|
break;
|
2019-02-23 22:39:11 +00:00
|
|
|
case MonitorTypes.First:
|
2018-04-08 06:25:34 +00:00
|
|
|
ToggleAlbumsMonitoredState(albums, false);
|
|
|
|
ToggleAlbumsMonitoredState(albums.OrderBy(e => e.ReleaseDate).Take(1), true);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
}
|
2018-01-28 06:27:33 +00:00
|
|
|
}
|
2017-07-08 02:06:21 +00:00
|
|
|
|
2019-07-09 20:13:52 +00:00
|
|
|
_albumService.UpdateMany(albums);
|
2017-07-08 02:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_artistService.UpdateArtist(artist);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ToggleAlbumsMonitoredState(IEnumerable<Album> albums, bool monitored)
|
|
|
|
{
|
|
|
|
foreach (var album in albums)
|
|
|
|
{
|
|
|
|
album.Monitored = monitored;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|