From d01dad9a574498708c24b4bb46e11275edf1afab Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Sat, 12 Oct 2019 16:15:53 +0200 Subject: [PATCH] use spotify user id from librespot session for web api requests fixes broken web api requests for users who logged in with their email address fixes #98 --- src/mpris.rs | 4 +++- src/spotify.rs | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/mpris.rs b/src/mpris.rs index dac5d7c..4710cda 100644 --- a/src/mpris.rs +++ b/src/mpris.rs @@ -38,7 +38,9 @@ fn get_metadata(track: Option) -> Metadata { ); hm.insert( "mpris:length".to_string(), - Variant(Box::new(track.map(|t| t.duration * 1_000).unwrap_or(0) as i64)), + Variant(Box::new( + track.map(|t| t.duration * 1_000).unwrap_or(0) as i64 + )), ); hm.insert( "mpris:artUrl".to_string(), diff --git a/src/spotify.rs b/src/spotify.rs index e6640b2..244f3cf 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -203,12 +203,12 @@ impl Spotify { normalisation: false, normalisation_pregain: 0.0, }; - let user = credentials.username.clone(); + let (user_tx, user_rx) = oneshot::channel(); let (tx, rx) = mpsc::unbounded(); { let events = events.clone(); - thread::spawn(move || Self::worker(events, rx, player_config, credentials)); + thread::spawn(move || Self::worker(events, rx, player_config, credentials, user_tx)); } let spotify = Spotify { @@ -218,7 +218,7 @@ impl Spotify { since: RwLock::new(None), token_issued: RwLock::new(None), channel: tx, - user, + user: user_rx.wait().expect("error retrieving userid from worker"), }; // acquire token for web api usage @@ -283,10 +283,14 @@ impl Spotify { commands: mpsc::UnboundedReceiver, player_config: PlayerConfig, credentials: Credentials, + user_tx: oneshot::Sender, ) { let mut core = Core::new().unwrap(); let session = Self::create_session(&mut core, credentials); + user_tx + .send(session.username()) + .expect("could not pass username back to Spotify::new"); let backend = audio_backend::find(None).unwrap(); let (player, _eventchannel) =