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:
2026-02-03 19:53:22 -05:00
parent af03a53af5
commit c7785b6488

View File

@@ -2628,18 +2628,28 @@ public class JellyfinController : ControllerBase
if (playlistConfig != null) if (playlistConfig != null)
{ {
var playlistName = playlistConfig.Name; 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", // Get matched external tracks (tracks that were successfully downloaded/matched)
missingTracksKey, missingTracks?.Count ?? 0); var matchedTracksKey = $"spotify:matched:ordered:{playlistName}";
var matchedTracks = await _cache.GetAsync<List<MatchedTrack>>(matchedTracksKey);
// Fallback to file cache _logger.LogInformation("Cache lookup for {Key}: {Count} matched tracks",
if (missingTracks == null || missingTracks.Count == 0) matchedTracksKey, matchedTracks?.Count ?? 0);
// Fallback to legacy cache format
if (matchedTracks == null || matchedTracks.Count == 0)
{ {
_logger.LogInformation("Trying file cache for {Name}", playlistName); var legacyKey = $"spotify:matched:{playlistName}";
missingTracks = await LoadMissingTracksFromFile(playlistName); var legacySongs = await _cache.GetAsync<List<Song>>(legacyKey);
_logger.LogInformation("File cache result: {Count} tracks", missingTracks?.Count ?? 0); 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 // Get local tracks count from Jellyfin
@@ -2655,7 +2665,7 @@ public class JellyfinController : ControllerBase
localTracksResponse.RootElement.TryGetProperty("Items", out var localItems)) localTracksResponse.RootElement.TryGetProperty("Items", out var localItems))
{ {
localTracksCount = localItems.GetArrayLength(); 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); localTracksCount, playlistName);
} }
} }
@@ -2664,26 +2674,28 @@ public class JellyfinController : ControllerBase
_logger.LogWarning(ex, "Failed to get local tracks count for {Name}", playlistName); _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) externalMatchedCount = matchedTracks.Count(t => t.MatchedSong != null && !t.MatchedSong.IsLocal);
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);
} }
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 // Update ChildCount to show actual available tracks
itemDict["ChildCount"] = localTracksCount; itemDict["ChildCount"] = totalAvailableCount;
modified = true; modified = true;
_logger.LogInformation("✓ Updated ChildCount for Spotify playlist {Name} to {Count} (local only, no external tracks)", _logger.LogInformation("✓ Updated ChildCount for Spotify playlist {Name} to {Total} (actual tracks in Jellyfin)",
playlistName, localTracksCount); playlistName, totalAvailableCount);
} }
else else
{ {
_logger.LogWarning("No tracks found for {Name} (neither local nor external)", playlistName); _logger.LogWarning("No tracks found in Jellyfin for {Name}", playlistName);
} }
} }
} }