From 1dcccf26839a2f2dfd6b350fbbdd5695fc7123c6 Mon Sep 17 00:00:00 2001 From: ta264 Date: Thu, 1 Aug 2019 22:22:28 +0100 Subject: [PATCH] Fixed: Frontend updates when selected album release changed --- .../MediaFiles/Commands/RescanArtistCommand.cs | 7 +++++-- src/NzbDrone.Core/MediaFiles/DiskScanService.cs | 4 ++-- .../TrackImport/ImportApprovedTracks.cs | 15 ++++++++++++--- src/NzbDrone.Core/Music/AlbumEditedService.cs | 13 +++++++++++-- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/NzbDrone.Core/MediaFiles/Commands/RescanArtistCommand.cs b/src/NzbDrone.Core/MediaFiles/Commands/RescanArtistCommand.cs index 5e17c0cd7..b3d1dc05a 100644 --- a/src/NzbDrone.Core/MediaFiles/Commands/RescanArtistCommand.cs +++ b/src/NzbDrone.Core/MediaFiles/Commands/RescanArtistCommand.cs @@ -5,16 +5,19 @@ namespace NzbDrone.Core.MediaFiles.Commands public class RescanArtistCommand : Command { public int? ArtistId { get; set; } + public FilterFilesType Filter { get; set; } public override bool SendUpdatesToClient => true; - public RescanArtistCommand() + public RescanArtistCommand(FilterFilesType filter = FilterFilesType.Known) { + Filter = filter; } - public RescanArtistCommand(int artistId) + public RescanArtistCommand(int artistId, FilterFilesType filter = FilterFilesType.Known) { ArtistId = artistId; + Filter = filter; } } } diff --git a/src/NzbDrone.Core/MediaFiles/DiskScanService.cs b/src/NzbDrone.Core/MediaFiles/DiskScanService.cs index 51dbec4b8..c8ae43358 100644 --- a/src/NzbDrone.Core/MediaFiles/DiskScanService.cs +++ b/src/NzbDrone.Core/MediaFiles/DiskScanService.cs @@ -275,7 +275,7 @@ namespace NzbDrone.Core.MediaFiles if (message.ArtistId.HasValue) { var artist = _artistService.GetArtist(message.ArtistId.Value); - Scan(artist); + Scan(artist, message.Filter); } else @@ -284,7 +284,7 @@ namespace NzbDrone.Core.MediaFiles foreach (var artist in allArtists) { - Scan(artist); + Scan(artist, message.Filter); } } } diff --git a/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs b/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs index 74a666019..0c3b7afbb 100644 --- a/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs +++ b/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs @@ -102,7 +102,7 @@ namespace NzbDrone.Core.MediaFiles.TrackImport // set the correct release to be monitored before importing the new files _logger.Debug("Updating release to {0} [{1} tracks]", newRelease, newRelease.TrackCount); - _releaseService.SetMonitored(newRelease); + album.AlbumReleases = _releaseService.SetMonitored(newRelease); // Publish album edited event. // Deliberatly don't put in the old album since we don't want to trigger an ArtistScan. @@ -111,6 +111,7 @@ namespace NzbDrone.Core.MediaFiles.TrackImport var filesToAdd = new List(qualifiedImports.Count); var albumReleasesDict = new Dictionary>(albumDecisions.Count); + var trackImportedEvents = new List(qualifiedImports.Count); foreach (var importDecision in qualifiedImports.OrderBy(e => e.Item.Tracks.Select(track => track.AbsoluteTrackNumber).MinOrDefault()) .ThenByDescending(e => e.Item.Size)) @@ -209,7 +210,9 @@ namespace NzbDrone.Core.MediaFiles.TrackImport allImportedTrackFiles.Add(trackFile); allOldTrackFiles.AddRange(oldFiles); - _eventAggregator.PublishEvent(new TrackImportedEvent(localTrack, trackFile, oldFiles, !localTrack.ExistingFile, downloadClientItem)); + // create all the import events here, but we can't publish until the trackfiles have been + // inserted and ids created + trackImportedEvents.Add(new TrackImportedEvent(localTrack, trackFile, oldFiles, !localTrack.ExistingFile, downloadClientItem)); } catch (RootFolderNotFoundException e) { @@ -244,7 +247,13 @@ namespace NzbDrone.Core.MediaFiles.TrackImport filesToAdd.ForEach(f => f.Tracks.Value.ForEach(t => t.TrackFileId = f.Id)); _trackService.SetFileIds(filesToAdd.SelectMany(x => x.Tracks.Value).ToList()); _logger.Debug($"TrackFileIds updated, total {watch.ElapsedMilliseconds}ms"); - + + // now that trackfiles have been inserted and ids generated, publish the import events + foreach (var trackImportedEvent in trackImportedEvents) + { + _eventAggregator.PublishEvent(trackImportedEvent); + } + var albumImports = importResults.Where(e => e.ImportDecision.Item.Album != null) .GroupBy(e => e.ImportDecision.Item.Album.Id).ToList(); diff --git a/src/NzbDrone.Core/Music/AlbumEditedService.cs b/src/NzbDrone.Core/Music/AlbumEditedService.cs index de4d63f28..26cacfc4e 100644 --- a/src/NzbDrone.Core/Music/AlbumEditedService.cs +++ b/src/NzbDrone.Core/Music/AlbumEditedService.cs @@ -4,16 +4,20 @@ using NzbDrone.Core.MediaFiles.Commands; using NzbDrone.Core.Music.Events; using System.Linq; using System.Collections.Generic; +using NzbDrone.Core.MediaFiles; namespace NzbDrone.Core.Music { public class AlbumEditedService : IHandle { private readonly IManageCommandQueue _commandQueueManager; + private readonly ITrackService _trackService; - public AlbumEditedService(IManageCommandQueue commandQueueManager) + public AlbumEditedService(IManageCommandQueue commandQueueManager, + ITrackService trackService) { _commandQueueManager = commandQueueManager; + _trackService = trackService; } public void Handle(AlbumEditedEvent message) @@ -25,7 +29,12 @@ namespace NzbDrone.Core.Music if (!new_monitored.SetEquals(old_monitored) || (message.OldAlbum.AnyReleaseOk == false && message.Album.AnyReleaseOk == true)) { - _commandQueueManager.Push(new RescanArtistCommand(message.Album.ArtistId)); + // Unlink any old track files + var tracks = _trackService.GetTracksByAlbum(message.Album.Id); + tracks.ForEach(x => x.TrackFileId = 0); + _trackService.SetFileIds(tracks); + + _commandQueueManager.Push(new RescanArtistCommand(message.Album.ArtistId, FilterFilesType.Matched)); } } }