From 19d87f6b8f073b6f0c9287b10ed7b46d410a4104 Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Sun, 15 Aug 2021 14:52:16 +0200 Subject: [PATCH] Only fetch the full track if necessary Previously the full track data would be fetched every time although the `Track` object could already be based on a `FullTrack` object. This should reduce the query count somewhat. --- src/mpris.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/mpris.rs b/src/mpris.rs index 09b8ade..36e46ee 100644 --- a/src/mpris.rs +++ b/src/mpris.rs @@ -41,10 +41,17 @@ fn get_metadata(playable: Option, spotify: Spotify) -> Metadata { // Fetch full track details in case this playable is based on a SimplifiedTrack // This is necessary because SimplifiedTrack objects don't contain a cover_url let playable_full = playable.and_then(|p| match p { - Playable::Track(track) => spotify - .track(&track.id.unwrap_or_default()) - .as_ref() - .map(|t| Playable::Track(t.into())), + Playable::Track(track) => { + if track.cover_url.is_some() { + // We already have `cover_url`, no need to fetch the full track + Some(Playable::Track(track)) + } else { + spotify + .track(&track.id.unwrap_or_default()) + .as_ref() + .map(|t| Playable::Track(t.into())) + } + } Playable::Episode(episode) => Some(Playable::Episode(episode)), }); let playable = playable_full.as_ref();