Skip to content

Commit

Permalink
connect: prevent loading a context with no tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
photovoltex committed Nov 17, 2024
1 parent b52a176 commit 658dfb0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion connect/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ pub enum StateError {
NoContext(ContextType),
#[error("could not find track {0:?} in context of {1}")]
CanNotFindTrackInContext(Option<usize>, usize),
#[error("Currently {action} is not allowed because {reason}")]
#[error("currently {action} is not allowed because {reason}")]
CurrentlyDisallowed { action: String, reason: String },
#[error("the provided context has no tracks")]
ContextHasNoTracks,
}

impl From<StateError> for Error {
Expand Down
13 changes: 8 additions & 5 deletions connect/src/state/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ impl ConnectState {
pub fn update_context(&mut self, mut context: Context) -> Result<(), Error> {
debug!("context: {}, {}", context.uri, context.url);

let page = match context.pages.pop() {
None => return Ok(()),
Some(page) if page.tracks.is_empty() => {
return Err(StateError::ContextHasNoTracks.into())
}
Some(page) => page,
};

if context.restrictions.is_some() {
self.player.restrictions = context.restrictions.clone();
self.player.context_restrictions = context.restrictions;
Expand All @@ -80,11 +88,6 @@ impl ConnectState {
self.player.context_metadata.insert(key, value);
}

let page = match context.pages.pop() {
None => return Ok(()),
Some(page) => page,
};

debug!(
"updated context from {} ({} tracks) to {} ({} tracks)",
self.player.context_uri,
Expand Down

0 comments on commit 658dfb0

Please sign in to comment.