Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed library blacklist behaviour #157

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
16 changes: 1 addition & 15 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion jellyfin-rpc-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ serde_json = "1.0"

[dependencies.jellyfin-rpc]
version = "1.3.0"
#path = "../jellyfin-rpc"
path = "../jellyfin-rpc"

[dependencies.clap]
features = ["derive"]
Expand Down
8 changes: 5 additions & 3 deletions jellyfin-rpc/src/jellyfin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ pub struct NowPlayingItem {
pub extra_type: Option<String>,
pub album_id: Option<String>,
pub album: Option<String>,
pub path: Option<String>,
}

#[derive(Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -327,8 +328,9 @@ pub struct PlayState {
pub position_ticks: Option<i64>,
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct Item {
pub struct VirtualFolder {
pub name: Option<String>,
}
pub locations: Vec<String>,
}
99 changes: 73 additions & 26 deletions jellyfin-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use discord_rich_presence::{
};
pub use error::JfError;
pub use jellyfin::{Button, MediaType};
use jellyfin::{ExternalUrl, Item, PlayTime, RawSession, Session};
use jellyfin::{ExternalUrl, PlayTime, RawSession, Session};
use log::debug;
use reqwest::header::{HeaderMap, AUTHORIZATION};
use std::str::FromStr;
use url::Url;
use crate::BlacklistedLibraries::{Initialized, Uninitialized};
use crate::jellyfin::{NowPlayingItem, VirtualFolder};

mod error;
mod external;
Expand Down Expand Up @@ -96,6 +98,14 @@ impl Client {
pub fn set_activity(&mut self) -> JfResult<String> {
self.get_session()?;

// Make sure the blacklist cache is loaded/valid
match &self.blacklist.libraries {
Uninitialized => {
self.reload_blacklist();
},
_ => {}
}

if let Some(session) = &self.session {
if session.now_playing_item.media_type == MediaType::None {
return Err(Box::new(JfError::UnrecognizedMediaType));
Expand Down Expand Up @@ -523,26 +533,8 @@ impl Client {
}
}

fn get_ancestors(&self) -> JfResult<Vec<Item>> {
let session = self.session.as_ref().unwrap();

let ancestors: Vec<Item> = self
.reqwest
.get(
self.url
.join(&format!("Items/{}/Ancestors", session.now_playing_item.id))?,
)
.send()?
.json()?;

debug!("Ancestors: {:?}", ancestors);

Ok(ancestors)
}

fn check_blacklist(&self) -> JfResult<bool> {
let session = self.session.as_ref().unwrap();
let ancestors = self.get_ancestors()?;

if self
.blacklist
Expand All @@ -553,16 +545,34 @@ impl Client {
return Ok(true);
}

if self.blacklist.libraries.iter().any(|l| {
ancestors
.iter()
.any(|a| l == a.name.as_ref().unwrap_or(&"".to_string()))
}) {
if self.blacklist.check_item(&session.now_playing_item)
{
return Ok(true);
}

Ok(false)
}

/// Fetch the virtual folder list and filter out the blacklisted libraries
fn fetch_blacklist(&self) -> JfResult<Vec<VirtualFolder>> {
let virtual_folders : Vec<VirtualFolder> = self.reqwest
.get(
self.url
.join(&"Library/VirtualFolders".to_string())?,
)
.send()?
.json()?;

Ok(virtual_folders
.into_iter()
.filter(|library_folder| self.blacklist.libraries_names.contains(library_folder.name.as_ref().unwrap())).collect()
)
}

/// Reload the library list from Jellyfin and filter out the user-provided blacklisted libraries
fn reload_blacklist(&mut self) {
self.blacklist.libraries = Initialized(self.fetch_blacklist().unwrap())
}
}

struct EpisodeDisplayOptions {
Expand All @@ -578,7 +588,43 @@ struct DisplayOptions {

struct Blacklist {
media_types: Vec<MediaType>,
libraries: Vec<String>,
libraries_names: Vec<String>,
libraries: BlacklistedLibraries
}

enum BlacklistedLibraries {
Uninitialized,
Initialized(Vec<VirtualFolder>),
}

impl Blacklist {

/// Check whether a [NowPlayingItem] is in a blacklisted library
fn check_item(&self, playing_item: &NowPlayingItem) -> bool {
debug!("Checking if an item is blacklisted: {}", playing_item.name);
self.check_path(&*playing_item.path.as_ref().unwrap())
}

/// Check whether a path is in a blacklisted library
fn check_path(&self, item_path: &str) -> bool {
match &self.libraries {
Initialized (libraries) => {
debug!("Checking path: {}", item_path);
libraries
.iter()
.any(|blacklisted_mf| {
blacklisted_mf.locations.iter().any(|physical_folder| {
debug!("BL path: {}", physical_folder);
item_path.starts_with(physical_folder)
})
})

}
_ => {
false
}
}
}
}

struct ImgurOptions {
Expand Down Expand Up @@ -866,7 +912,8 @@ impl ClientBuilder {
},
blacklist: Blacklist {
media_types: self.blacklist_media_types,
libraries: self.blacklist_libraries,
libraries_names: self.blacklist_libraries,
libraries: Uninitialized,
},
show_paused: self.show_paused,
show_images: self.show_images,
Expand Down