Skip to content

Commit

Permalink
Avoid fetching several covers for playlists
Browse files Browse the repository at this point in the history
User can't see them until needed, and causes a ton of requests that can
stall Navidrome servers.

Fixes GH-174
  • Loading branch information
lukstei authored and NattyNarwhal committed Nov 8, 2023
1 parent ba903a3 commit 4f688df
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions Submariner/SBSubsonicParsingOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ class SBSubsonicParsingOperation: SBOperation, XMLParserDelegate {
} ?? false
logger.info("Adding track (and updating) with ID: \(id, privacy: .public) to playlist \(currentPlaylist.itemId ?? "(no ID?)", privacy: .public), exists? \(exists) index? \(self.playlistIndex)")

updateTrackDependenciesForDirectoryIndex(track, attributeDict: attributeDict)
updateTrackDependenciesForDirectoryIndex(track, attributeDict: attributeDict, shouldFetchAlbumArt: false)

// limitation if the same track exists twice
track.playlistIndex = NSNumber(value: playlistIndex)
Expand All @@ -412,7 +412,7 @@ class SBSubsonicParsingOperation: SBOperation, XMLParserDelegate {
// FIXME: Should we update *existing* tracks regardless? For previous cases they were pulled anew...
logger.info("Creating new track with ID: \(id, privacy: .public) for playlist \(currentPlaylist.itemId ?? "(no ID?)", privacy: .public)")
let track = createTrack(attributes: attributeDict)
updateTrackDependenciesForDirectoryIndex(track, attributeDict: attributeDict)
updateTrackDependenciesForDirectoryIndex(track, attributeDict: attributeDict, shouldFetchAlbumArt: false)

track.playlistIndex = NSNumber(value: playlistIndex)
playlistIndex += 1
Expand Down Expand Up @@ -822,8 +822,8 @@ class SBSubsonicParsingOperation: SBOperation, XMLParserDelegate {
return album
}

// NOT USED YET - this makes sense only if you're using
private func updateTrackDependenciesForTag(_ track: SBTrack, attributeDict: [String: String]) {
// NOT USED YET - this makes sense only if you're using the tag based APIs
private func updateTrackDependenciesForTag(_ track: SBTrack, attributeDict: [String: String], shouldFetchAlbumArt: Bool = true) {
var attachedArtist: SBArtist?
// is this right for album artist? the artist object can get corrected on fetch though...
if let artistID = attributeDict["artistId"] {
Expand Down Expand Up @@ -874,7 +874,9 @@ class SBSubsonicParsingOperation: SBOperation, XMLParserDelegate {
attachedAlbum.cover = attachedCover!
}

clientController.getCover(id: coverID)
if shouldFetchAlbumArt {
clientController.getCover(id: coverID)
}
}

if let attachedAlbum = attachedAlbum {
Expand All @@ -884,7 +886,7 @@ class SBSubsonicParsingOperation: SBOperation, XMLParserDelegate {
}

// not as good as former, but we have to use it until we switch to using tag based metadata instead of hierarchy index
private func updateTrackDependenciesForDirectoryIndex(_ track: SBTrack, attributeDict: [String: String]) {
private func updateTrackDependenciesForDirectoryIndex(_ track: SBTrack, attributeDict: [String: String], shouldFetchAlbumArt: Bool = true) {
var attachedAlbum: SBAlbum?
var attachedCover: SBCover?
var attachedArtist: SBArtist?
Expand Down Expand Up @@ -925,7 +927,9 @@ class SBSubsonicParsingOperation: SBOperation, XMLParserDelegate {
attachedAlbum.cover = attachedCover!
}

clientController.getCover(id: coverID)
if shouldFetchAlbumArt {
clientController.getCover(id: coverID)
}
}

if let attachedAlbum = attachedAlbum, let artistName = attributeDict["artist"] {
Expand Down

0 comments on commit 4f688df

Please sign in to comment.