Add playback preferences API endpoint for remote control

- GET /api/playback/preferences - get current autoplay mode
- POST /api/playback/autoplay - set autoplay mode (continue/track_only/infinite)
This commit is contained in:
vicrodh committed 2026-02-03 07:54:39 -06:00
1 parent 8e4d39684a
commit 0651acf1db
1 file changed
+34
+34
View File
@@ -25,6 +25,7 @@ use crate::{
commands::{self, search::SearchAllResults}, commands::{self, search::SearchAllResults},
config::{ config::{
audio_settings::AudioSettingsState, audio_settings::AudioSettingsState,
playback_preferences::{AutoplayMode, PlaybackPreferencesState, PlaybackPreferences},
remote_control_settings::{RemoteControlSettings, RemoteControlSettingsState}, remote_control_settings::{RemoteControlSettings, RemoteControlSettingsState},
}, },
offline_cache::OfflineCacheState, offline_cache::OfflineCacheState,
@@ -399,6 +400,8 @@ fn build_router(ctx: ApiContext) -> Router {
.route("/api/album/play", post(play_album)) .route("/api/album/play", post(play_album))
.route("/api/album/:id", get(get_album)) .route("/api/album/:id", get(get_album))
.route("/api/artist/:id", get(get_artist)) .route("/api/artist/:id", get(get_artist))
.route("/api/playback/preferences", get(get_playback_preferences))
.route("/api/playback/autoplay", post(set_autoplay))
.route("/api/ws", get(ws_handler)) .route("/api/ws", get(ws_handler))
.with_state(ctx.clone()) .with_state(ctx.clone())
.layer(middleware::from_fn(lan_only)) .layer(middleware::from_fn(lan_only))
@@ -560,6 +563,37 @@ struct PlayAlbumRequest {
album_id: String, album_id: String,
} }
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct SetAutoplayRequest {
mode: String,
}
async fn get_playback_preferences(
State(ctx): State<ApiContext>,
) -> Result<Json<PlaybackPreferences>, StatusCode> {
let prefs_state = ctx.app_handle.state::<PlaybackPreferencesState>();
prefs_state.get_preferences()
.map(Json)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}
async fn set_autoplay(
State(ctx): State<ApiContext>,
Json(payload): Json<SetAutoplayRequest>,
) -> Result<StatusCode, StatusCode> {
let prefs_state = ctx.app_handle.state::<PlaybackPreferencesState>();
let mode = match payload.mode.as_str() {
"continue" => AutoplayMode::ContinueWithinSource,
"track_only" => AutoplayMode::PlayTrackOnly,
"infinite" => AutoplayMode::InfiniteRadio,
_ => return Err(StatusCode::BAD_REQUEST),
};
prefs_state.set_autoplay_mode(mode)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(StatusCode::NO_CONTENT)
}
async fn search_tracks( async fn search_tracks(
State(ctx): State<ApiContext>, State(ctx): State<ApiContext>,
Query(query): Query<SearchQuery>, Query(query): Query<SearchQuery>,