From ec47510e525d582a255a58a635baa05f33a5b6c6 Mon Sep 17 00:00:00 2001 From: Radiicall Date: Mon, 22 Jul 2024 20:39:37 +0200 Subject: [PATCH] Fix crash when username is None --- jellyfin-rpc/src/jellyfin.rs | 6 ++-- jellyfin-rpc/src/lib.rs | 55 ++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/jellyfin-rpc/src/jellyfin.rs b/jellyfin-rpc/src/jellyfin.rs index 551a113..c6b318d 100644 --- a/jellyfin-rpc/src/jellyfin.rs +++ b/jellyfin-rpc/src/jellyfin.rs @@ -4,9 +4,9 @@ use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH}; #[derive(Deserialize, Debug)] #[serde(rename_all = "PascalCase")] pub struct RawSession { - pub user_name: String, + pub user_name: Option, pub now_playing_item: Option, - pub play_state: PlayState, + pub play_state: Option, } impl RawSession { @@ -22,7 +22,7 @@ impl RawSession { Session { now_playing_item: self.now_playing_item.unwrap(), - play_state: self.play_state, + play_state: self.play_state.unwrap(), item_id: id.to_string(), } } diff --git a/jellyfin-rpc/src/lib.rs b/jellyfin-rpc/src/lib.rs index 11a9025..c25b0bb 100644 --- a/jellyfin-rpc/src/lib.rs +++ b/jellyfin-rpc/src/lib.rs @@ -194,34 +194,41 @@ impl Client { debug!("Found {} sessions", sessions.len()); for session in sessions { - debug!("Session username is {}", session.user_name); - if self - .usernames - .iter() - .all(|u| session.user_name.to_lowercase() != *u.to_lowercase()) - { - continue; - } + debug!("Session username is {:?}", session.user_name); + if let Some(username) = session.user_name.as_ref() { + if self + .usernames + .iter() + .all(|u| username.to_lowercase() != u.to_lowercase()) + { + continue; + } - if session.now_playing_item.is_none() { - continue; - } - debug!("NowPlayingItem exists"); + if session.now_playing_item.is_none() { + continue; + } + debug!("NowPlayingItem exists"); - let session = session.build(); + if session.play_state.is_none() { + continue; + } + debug!("PlayState exists"); - if session - .now_playing_item - .extra_type - .as_ref() - .is_some_and(|et| et == "ThemeSong") - { - debug!("Session is playing a theme song, continuing loop"); - continue; - } + let session = session.build(); - self.session = Some(session); - return Ok(()); + if session + .now_playing_item + .extra_type + .as_ref() + .is_some_and(|et| et == "ThemeSong") + { + debug!("Session is playing a theme song, continuing loop"); + continue; + } + + self.session = Some(session); + return Ok(()); + } } self.session = None; Ok(())