mirror of
https://github.com/SoPat712/allstarr.git
synced 2026-02-09 23:55:10 -05:00
Fix playlist track count to show actual available tracks
- Changed ChildCount to reflect tracks actually in Jellyfin (local + external matched) - Previously was incorrectly adding missing tracks to the count - Now clients see the correct number of playable tracks - Uses spotify:matched:ordered cache to count external matched tracks
This commit is contained in:
@@ -2628,18 +2628,28 @@ public class JellyfinController : ControllerBase
|
||||
if (playlistConfig != null)
|
||||
{
|
||||
var playlistName = playlistConfig.Name;
|
||||
var missingTracksKey = $"spotify:missing:{playlistName}";
|
||||
var missingTracks = await _cache.GetAsync<List<allstarr.Models.Spotify.MissingTrack>>(missingTracksKey);
|
||||
|
||||
_logger.LogInformation("Cache lookup for {Key}: {Count} tracks",
|
||||
missingTracksKey, missingTracks?.Count ?? 0);
|
||||
// Get matched external tracks (tracks that were successfully downloaded/matched)
|
||||
var matchedTracksKey = $"spotify:matched:ordered:{playlistName}";
|
||||
var matchedTracks = await _cache.GetAsync<List<MatchedTrack>>(matchedTracksKey);
|
||||
|
||||
// Fallback to file cache
|
||||
if (missingTracks == null || missingTracks.Count == 0)
|
||||
_logger.LogInformation("Cache lookup for {Key}: {Count} matched tracks",
|
||||
matchedTracksKey, matchedTracks?.Count ?? 0);
|
||||
|
||||
// Fallback to legacy cache format
|
||||
if (matchedTracks == null || matchedTracks.Count == 0)
|
||||
{
|
||||
_logger.LogInformation("Trying file cache for {Name}", playlistName);
|
||||
missingTracks = await LoadMissingTracksFromFile(playlistName);
|
||||
_logger.LogInformation("File cache result: {Count} tracks", missingTracks?.Count ?? 0);
|
||||
var legacyKey = $"spotify:matched:{playlistName}";
|
||||
var legacySongs = await _cache.GetAsync<List<Song>>(legacyKey);
|
||||
if (legacySongs != null && legacySongs.Count > 0)
|
||||
{
|
||||
matchedTracks = legacySongs.Select((s, i) => new MatchedTrack
|
||||
{
|
||||
Position = i,
|
||||
MatchedSong = s
|
||||
}).ToList();
|
||||
_logger.LogInformation("Loaded {Count} tracks from legacy cache", matchedTracks.Count);
|
||||
}
|
||||
}
|
||||
|
||||
// Get local tracks count from Jellyfin
|
||||
@@ -2655,7 +2665,7 @@ public class JellyfinController : ControllerBase
|
||||
localTracksResponse.RootElement.TryGetProperty("Items", out var localItems))
|
||||
{
|
||||
localTracksCount = localItems.GetArrayLength();
|
||||
_logger.LogInformation("Found {Count} local tracks in Jellyfin playlist {Name}",
|
||||
_logger.LogInformation("Found {Count} total items in Jellyfin playlist {Name}",
|
||||
localTracksCount, playlistName);
|
||||
}
|
||||
}
|
||||
@@ -2664,26 +2674,28 @@ public class JellyfinController : ControllerBase
|
||||
_logger.LogWarning(ex, "Failed to get local tracks count for {Name}", playlistName);
|
||||
}
|
||||
|
||||
if (missingTracks != null && missingTracks.Count > 0)
|
||||
// Count external matched tracks (not local)
|
||||
var externalMatchedCount = 0;
|
||||
if (matchedTracks != null)
|
||||
{
|
||||
// Update ChildCount to show total tracks (local + external)
|
||||
var totalCount = localTracksCount + missingTracks.Count;
|
||||
itemDict["ChildCount"] = totalCount;
|
||||
modified = true;
|
||||
_logger.LogInformation("✓ Updated ChildCount for Spotify playlist {Name} to {Total} ({Local} local + {External} external)",
|
||||
playlistName, totalCount, localTracksCount, missingTracks.Count);
|
||||
externalMatchedCount = matchedTracks.Count(t => t.MatchedSong != null && !t.MatchedSong.IsLocal);
|
||||
}
|
||||
else if (localTracksCount > 0)
|
||||
|
||||
// Total available tracks = what's actually in Jellyfin (local + external matched)
|
||||
// This is what clients should see as the track count
|
||||
var totalAvailableCount = localTracksCount;
|
||||
|
||||
if (totalAvailableCount > 0)
|
||||
{
|
||||
// No external tracks, but we have local tracks
|
||||
itemDict["ChildCount"] = localTracksCount;
|
||||
// Update ChildCount to show actual available tracks
|
||||
itemDict["ChildCount"] = totalAvailableCount;
|
||||
modified = true;
|
||||
_logger.LogInformation("✓ Updated ChildCount for Spotify playlist {Name} to {Count} (local only, no external tracks)",
|
||||
playlistName, localTracksCount);
|
||||
_logger.LogInformation("✓ Updated ChildCount for Spotify playlist {Name} to {Total} (actual tracks in Jellyfin)",
|
||||
playlistName, totalAvailableCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("No tracks found for {Name} (neither local nor external)", playlistName);
|
||||
_logger.LogWarning("No tracks found in Jellyfin for {Name}", playlistName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user