Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ chrono = "0.4"
log = "0.4"
librespot-audio = { version = "0.8", default-features = false }
librespot-core = { version = "0.8", default-features = false, features = ["rustls-tls-native-roots"] }
librespot-protocol = { version = "0.8", default-features = false }
librespot-playback = { version = "0.8", default-features = false, features = ["rodio-backend", "rustls-tls-native-roots"] }
tauri = { version = "2.11.3", features = [] }
tauri-plugin-dialog = "2"
Expand Down
71 changes: 42 additions & 29 deletions apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl MenuChecks {
struct ConnectionState {
connected: bool,
needs_reauth: bool,
playback_authorized: bool,
missing_scopes: Vec<String>,
}

Expand All @@ -136,6 +137,12 @@ impl ConnectionState {
Self {
connected: tokens.is_some(),
needs_reauth: !missing_scopes.is_empty(),
playback_authorized: tokens
.as_ref()
.and_then(|tokens| tokens.playback_credentials.as_ref())
.is_some_and(|credentials| {
!credentials.username.is_empty() && !credentials.auth_data.is_empty()
}),
missing_scopes,
}
}
Expand Down Expand Up @@ -669,25 +676,12 @@ async fn set_settings(app: tauri::AppHandle, mut settings: Settings) -> Result<(
settings.spotify_sync_completed = current.spotify_sync_completed;
settings.last_full_sync = current.last_full_sync;
settings.validate().map_err(|error| error.to_string())?;
// Compare against the ACTIVE backend, not the persisted setting: a failed
// activation (e.g. under-scoped token at startup) leaves the setting on
// "local" while playback fell back to Connect, and re-selecting the radio
// must retry the switch.
let wants_local = settings.playback_backend == "local";
if wants_local != state.playback.is_local_active().await {
let switch = if wants_local {
switch_to_local(&state, settings.volume).await
} else {
state.playback.switch_to_connect().await;
Ok(())
};
if let Err(error) = switch {
app.emit("operation-error", error)
.map_err(|error| error.to_string())?;
app.emit("settings-changed", current)
.map_err(|error| error.to_string())?;
return Ok(());
}
state.playback.set_local_requested(wants_local);
// Local activation is intentionally lazy: playback owns authorization
// prompts, and unrelated preference saves must remain offline-safe.
if !wants_local && state.playback.is_local_active().await {
state.playback.switch_to_connect().await;
}
state
.settings_store
Expand All @@ -710,18 +704,11 @@ async fn set_settings(app: tauri::AppHandle, mut settings: Settings) -> Result<(
}

async fn switch_to_local(state: &AppState, volume: u8) -> Result<(), String> {
let stored = state
state
.token_store
.load()
.map_err(|error| error.to_string())?
.ok_or_else(|| "Connect to Spotify before enabling built-in playback.".to_string())?;
if !stored
.scopes
.split_whitespace()
.any(|scope| scope == "streaming")
{
return Err("Reconnect to Spotify to grant playback permission (Account → Disconnect, then Connect).".into());
}
let client = provider_from(state)?;
if !client
.me()
Expand Down Expand Up @@ -758,7 +745,7 @@ fn stored_connection_state(token_store: &SharedTokenStore) -> Result<ConnectionS
.map_err(|error| error.to_string())
}

fn emit_connection_state(app: &tauri::AppHandle) -> Result<(), String> {
pub(crate) fn emit_connection_state(app: &tauri::AppHandle) -> Result<(), String> {
let state = app.state::<AppState>();
let connection = stored_connection_state(&state.token_store)?;
state
Expand Down Expand Up @@ -2060,6 +2047,7 @@ pub fn run() {
set_settings,
spotify_commands::connection_state,
spotify_commands::connect_spotify,
spotify_commands::authorize_spotify_playback,
spotify_commands::disconnect_spotify,
spotify_commands::sync_from_spotify,
spotify_commands::spotify_search,
Expand Down Expand Up @@ -2174,7 +2162,9 @@ pub fn run() {
if connection.connected && startup_action == StartupAction::Nothing {
log::info!("startup sync skipped; library fresh");
}
let activate_local = connection.connected && settings.playback_backend == "local";
let activate_local = connection.connected
&& connection.playback_authorized
&& settings.playback_backend == "local";
let initial_volume = settings.volume;
let playback = Arc::new(Playback::new(
&settings.repeat,
Expand All @@ -2187,6 +2177,7 @@ pub fn run() {
},
Some(app_data_dir.clone()),
));
playback.set_local_requested(settings.playback_backend == "local");
let media_keys = media_keys::MediaKeys::spawn(app.handle().clone());
app.manage(AppState {
library: Mutex::new(library),
Expand Down Expand Up @@ -3033,12 +3024,14 @@ mod tests {
let connected = ConnectionState {
connected: true,
needs_reauth: false,
playback_authorized: false,
missing_scopes: vec![],
};
let disconnected = ConnectionState::from_tokens(None);
let needs_reauth = ConnectionState {
connected: true,
needs_reauth: true,
playback_authorized: false,
missing_scopes: vec!["playlist-read-private".into()],
};
assert_eq!(
Expand Down Expand Up @@ -3078,9 +3071,14 @@ mod tests {
refresh: String::new(),
expires_at: 0,
scopes: "user-library-read".into(),
playback_credentials: None,
};
let current = Tokens {
scopes: auth::SCOPES.clone(),
playback_credentials: Some(retune_spotify::tokens::PlaybackCredentials {
username: "user".into(),
auth_data: vec![1, 2, 3],
}),
..legacy.clone()
};

Expand All @@ -3089,6 +3087,7 @@ mod tests {
ConnectionState {
connected: true,
needs_reauth: true,
playback_authorized: false,
missing_scopes: auth::REQUIRED_SCOPES
.into_iter()
.filter(|scope| *scope != "user-library-read")
Expand All @@ -3097,13 +3096,26 @@ mod tests {
}
);
assert_eq!(
stored_connection_state(&shared_token_store(Some(current))).unwrap(),
stored_connection_state(&shared_token_store(Some(current.clone()))).unwrap(),
ConnectionState {
connected: true,
needs_reauth: false,
playback_authorized: true,
missing_scopes: vec![],
}
);
let empty_playback = Tokens {
playback_credentials: Some(retune_spotify::tokens::PlaybackCredentials {
username: String::new(),
auth_data: vec![],
}),
..current
};
assert!(
!stored_connection_state(&shared_token_store(Some(empty_playback)))
.unwrap()
.playback_authorized
);
}

#[test]
Expand All @@ -3115,6 +3127,7 @@ mod tests {
refresh: String::new(),
expires_at: 0,
scopes: "user-library-read".into(),
playback_credentials: None,
};
let forbidden = || retune_spotify::Error::Http {
endpoint: "/playlists/id/tracks".into(),
Expand Down
23 changes: 20 additions & 3 deletions apps/desktop/src-tauri/src/media_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use souvlaki::{
};
use tauri::{Emitter, Manager};

use crate::{playback::PlayerStateEvent, AppState};
use crate::{
playback::{PlayOutcome, PlayerStateEvent},
AppState,
};

pub struct MediaKeys {
controls: Option<Mutex<MediaControlsState>>,
Expand Down Expand Up @@ -124,8 +127,22 @@ fn handle_control(app: &tauri::AppHandle, event: MediaControlEvent) {
state.playback.set_playing(client.as_deref(), playing).await
}
PlaybackCommand::Toggle => state.playback.toggle(client.as_deref()).await,
PlaybackCommand::Next => state.playback.next(client).await,
PlaybackCommand::Previous => state.playback.prev(client).await,
PlaybackCommand::Next => match state.playback.next(client).await {
Ok(PlayOutcome::PlaybackAuthorizationRequired(prompt)) => {
state.playback.stop_for_authorization(&app, prompt).await;
Ok(())
}
Ok(_) => Ok(()),
Err(error) => Err(error),
},
PlaybackCommand::Previous => match state.playback.prev(client).await {
Ok(PlayOutcome::PlaybackAuthorizationRequired(prompt)) => {
state.playback.stop_for_authorization(&app, prompt).await;
Ok(())
}
Ok(_) => Ok(()),
Err(error) => Err(error),
},
PlaybackCommand::Seek(seconds) => state.playback.seek(client.as_deref(), seconds).await,
};
if let Err(error) = result {
Expand Down
Loading