Skip to content

Commit

Permalink
fix: skip visited videos regardless of depth
Browse files Browse the repository at this point in the history
  • Loading branch information
revam committed Nov 13, 2024
1 parent dc96e7d commit b420e89
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Shokofin/MergeVersions/MergeVersionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,15 @@ public async Task<bool> SplitAndMergeVideos<TVideo>(
// Split up any existing merged videos.
double currentCount = 0d;
double totalCount = videos.Count;
var visitedVideos = new HashSet<Guid>();
foreach (var video in videos) {
// Handle cancellation and update progress.
cancellationToken?.ThrowIfCancellationRequested();
var percent = currentCount++ / totalCount * 50d;
progress?.Report(percent);

// Remove all alternate sources linked to the video.
await RemoveAlternateSources(video);
await RemoveAlternateSources(video, visitedVideos);
}

// This will likely tax the CPU a bit… maybe, but we need to make sure the videos we're about to merge are up to date.
Expand Down Expand Up @@ -276,14 +277,15 @@ public async Task SplitVideos<TVideo>(IReadOnlyList<TVideo> videos, IProgress<do
// Split up any existing merged videos.
double currentCount = 0d;
double totalMovies = videos.Count;
var visitedVideos = new HashSet<Guid>();
foreach (var video in videos) {
// Handle cancellation and update progress.
cancellationToken?.ThrowIfCancellationRequested();
var percent = currentCount++ / totalMovies * 100d;
progress?.Report(percent);

// Remove all alternate sources linked to the video.
await RemoveAlternateSources(video);
await RemoveAlternateSources(video, visitedVideos);
}

progress?.Report(100);
Expand Down Expand Up @@ -356,17 +358,25 @@ await primaryVersion.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, Cancel
/// videos.
/// </summary>
/// <param name="baseItem">The primary video to clean up.</param>
private async Task RemoveAlternateSources<TVideo>(TVideo? video, int depth = 0) where TVideo : Video
private async Task RemoveAlternateSources<TVideo>(TVideo? video, HashSet<Guid> visited) where TVideo : Video
{
if (video is null)
return;

var depth = visited.Count;
if (visited.Contains(video.Id)) {
_logger.LogTrace("Skipping already visited video. (Video={VideoId},Depth={Depth})", video.Id, depth);
return;
}

visited.Add(video.Id);

// Remove all links for the primary video if this is not the primary video.
if (video.PrimaryVersionId is not null && depth is 0) {
if (video.PrimaryVersionId is not null) {
var primaryVideo = _libraryManager.GetItemById(video.PrimaryVersionId) as TVideo;
if (primaryVideo is not null && primaryVideo.Id != video.Id) {
if (primaryVideo is not null) {
_logger.LogTrace("Found primary video to clean up first. (Video={VideoId},Depth={Depth})", primaryVideo.Id, depth);
await RemoveAlternateSources(primaryVideo, depth + 1);
await RemoveAlternateSources(primaryVideo, visited);
}
}

Expand All @@ -382,7 +392,7 @@ private async Task RemoveAlternateSources<TVideo>(TVideo? video, int depth = 0)
var linkedAlternateVersions = video.GetLinkedAlternateVersions().ToList();
_logger.LogTrace("Removing {Count} linked alternate sources for video. (Video={VideoId},Depth={Depth})", linkedAlternateVersions.Count, video.Id, depth);
foreach (var linkedVideo in linkedAlternateVersions) {
await RemoveAlternateSources(linkedVideo, depth + 1);
await RemoveAlternateSources(linkedVideo, visited);
}

// Remove the link for every local linked video.
Expand All @@ -392,7 +402,7 @@ private async Task RemoveAlternateSources<TVideo>(TVideo? video, int depth = 0)
.ToList();
_logger.LogTrace("Removing {Count} local alternate sources for video. (Video={VideoId},Depth={Depth})", localAlternateVersions.Count, video.Id, depth);
foreach (var linkedVideo in localAlternateVersions) {
await RemoveAlternateSources(linkedVideo, depth + 1);
await RemoveAlternateSources(linkedVideo, visited);
}

// Remove the link for the primary video.
Expand Down

0 comments on commit b420e89

Please sign in to comment.