use jellycommon::{ helpers::SortAnyway, user::{NodeUserData, WatchedState}, NodeKind, NodePublic, Rating, }; use markup::RenderAttributeValue; use rocket::{ http::uri::fmt::{Query, UriDisplay}, FromForm, FromFormField, UriDisplayQuery, }; #[derive(FromForm, UriDisplayQuery, Default, Clone)] pub struct NodeFilterSort { sort_by: Option, filter_kind: Option>, sort_order: Option, } macro_rules! form_enum { (enum $i:ident { $($vi:ident = $vk:literal),*, }) => { #[derive(FromFormField, UriDisplayQuery, Clone, PartialEq, Eq)] enum $i { $(#[field(value = $vk)] $vi),* } impl $i { #[allow(unused)] const ALL: &'static [$i] = &[$($i::$vi),*]; } }; } form_enum!( enum FilterProperty { FederationLocal = "fed_local", FederationRemote = "fed_remote", Watched = "watched", Unwatched = "unwatched", WatchProgress = "watch_progress", KindMovie = "kind_movie", KindVideo = "kind_video", KindCollection = "kind_collection", KindChannel = "kind_channel", KindShow = "kind_show", KindSeries = "kind_series", KindSeason = "kind_season", KindEpisode = "kind_episode", } ); form_enum!( enum SortProperty { ReleaseDate = "release_date", Title = "title", RatingRottenTomatoes = "rating_rt", RatingMetacritic = "rating_mc", RatingImdb = "rating_imdb", RatingTmdb = "rating_tmdb", RatingYoutubeViews = "rating_yt_views", RatingYoutubeLikes = "rating_yt_likes", RatingYoutubeFollowers = "rating_yt_followers", } ); impl SortProperty { const CATS: &'static [(&'static str, &'static [(SortProperty, &'static str)])] = { use SortProperty::*; &[ ( "General", &[(Title, "Title"), (ReleaseDate, "Release Date")], ), ( "By Rating", &[ (RatingImdb, "IMDb Rating"), (RatingTmdb, "TMDB Rating"), (RatingMetacritic, "Metacritic Rating"), (RatingRottenTomatoes, "Rotten Tomatoes"), (RatingYoutubeFollowers, "Youtube Subscribers"), (RatingYoutubeLikes, "Youtube Likes"), (RatingYoutubeViews, "Youtube Views"), ], ), ] }; } impl FilterProperty { const CATS: &'static [(&'static str, &'static [(FilterProperty, &'static str)])] = { use FilterProperty::*; &[ ( "By Kind", &[ (KindMovie, "Movie"), (KindVideo, "Video"), (KindCollection, "Collection"), (KindChannel, "Channel"), (KindShow, "Show"), (KindSeries, "Series"), (KindSeason, "Season"), (KindEpisode, "Episode"), ], ), ( "By Federation", &[(FederationLocal, "Local"), (FederationRemote, "Remote")], ), ( "By Watched", &[ (Watched, "Watched"), (Unwatched, "Unwatched"), (WatchProgress, "Partially Watched"), ], ), ] }; } impl NodeFilterSort { pub fn is_open(&self) -> bool { self.filter_kind.is_some() || self.sort_by.is_some() } } #[rustfmt::skip] #[derive(FromFormField, UriDisplayQuery, Clone, Copy, PartialEq, Eq)] enum SortOrder { #[field(value = "ascending")] Ascending, #[field(value = "descending")] Descending, } pub fn filter_and_sort_nodes( f: &NodeFilterSort, nodes: &mut Vec<(String, NodePublic, NodeUserData)>, ) { nodes.retain(|(_id, node, udata)| { let mut o = true; if let Some(prop) = &f.filter_kind { for p in FilterProperty::ALL { if prop.contains(p) { continue; } o &= !match p { FilterProperty::FederationLocal => node.federated.is_none(), FilterProperty::FederationRemote => node.federated.is_some(), FilterProperty::KindMovie => node.kind == Some(NodeKind::Movie), FilterProperty::KindVideo => node.kind == Some(NodeKind::Video), FilterProperty::KindCollection => node.kind == Some(NodeKind::Collection), FilterProperty::KindChannel => node.kind == Some(NodeKind::Channel), FilterProperty::KindShow => node.kind == Some(NodeKind::Show), FilterProperty::KindSeries => node.kind == Some(NodeKind::Series), FilterProperty::KindSeason => node.kind == Some(NodeKind::Season), FilterProperty::KindEpisode => node.kind == Some(NodeKind::Episode), FilterProperty::Watched => udata.watched == WatchedState::Watched, FilterProperty::Unwatched => udata.watched == WatchedState::None, FilterProperty::WatchProgress => { matches!(udata.watched, WatchedState::Progress(_)) } } } } if let Some(SortProperty::ReleaseDate) = &f.sort_by { o &= node.release_date.is_some() } o }); if let Some(sort_prop) = &f.sort_by { match sort_prop { SortProperty::ReleaseDate => { nodes.sort_by_key(|(_, n, _)| n.release_date.expect("asserted above")) } SortProperty::Title => nodes.sort_by(|(_, a, _), (_, b, _)| a.title.cmp(&b.title)), SortProperty::RatingRottenTomatoes => nodes.sort_by_cached_key(|(_, n, _)| { SortAnyway(*n.ratings.get(&Rating::RottenTomatoes).unwrap_or(&0.)) }), SortProperty::RatingMetacritic => nodes.sort_by_cached_key(|(_, n, _)| { SortAnyway(*n.ratings.get(&Rating::Metacritic).unwrap_or(&0.)) }), SortProperty::RatingImdb => nodes.sort_by_cached_key(|(_, n, _)| { SortAnyway(*n.ratings.get(&Rating::Imdb).unwrap_or(&0.)) }), SortProperty::RatingTmdb => nodes.sort_by_cached_key(|(_, n, _)| { SortAnyway(*n.ratings.get(&Rating::Tmdb).unwrap_or(&0.)) }), SortProperty::RatingYoutubeViews => nodes.sort_by_cached_key(|(_, n, _)| { SortAnyway(*n.ratings.get(&Rating::YoutubeViews).unwrap_or(&0.)) }), SortProperty::RatingYoutubeLikes => nodes.sort_by_cached_key(|(_, n, _)| { SortAnyway(*n.ratings.get(&Rating::YoutubeLikes).unwrap_or(&0.)) }), SortProperty::RatingYoutubeFollowers => nodes.sort_by_cached_key(|(_, n, _)| { SortAnyway(*n.ratings.get(&Rating::YoutubeFollowers).unwrap_or(&0.)) }), } } match f.sort_order.unwrap_or(SortOrder::Ascending) { SortOrder::Ascending => (), SortOrder::Descending => nodes.reverse(), } } markup::define! { NodeFilterSortForm<'a>(f: &'a NodeFilterSort) { details.filtersort[open=f.is_open()] { summary { "Filter and Sort" } form[method="GET", action=""] { fieldset.filter { legend { "Filter" } .categories { @for (cname, cat) in FilterProperty::CATS { .category { h3 { @cname } @for (value, label) in *cat { label { input[type="checkbox", name="filter_kind", value=value, checked=f.filter_kind.as_ref().map(|k|k.contains(&value)).unwrap_or(true)]; @label } br; } } } } } fieldset.sortby { legend { "Sort" } .categories { @for (cname, cat) in SortProperty::CATS { .category { h3 { @cname } @for (value, label) in *cat { label { input[type="radio", name="sort_by", value=value, checked=Some(value)==f.sort_by.as_ref()]; @label } br; } } } } } fieldset.sortorder { legend { "Sort Order" } @use SortOrder::*; @for (value, label) in [(Ascending, "Ascending"), (Descending, "Descending")] { label { input[type="radio", name="sort_order", value=value, checked=Some(value)==f.sort_order]; @label } br; } } input[type="submit", value="Apply"]; a[href="?"] { "Clear" } } } } } impl markup::Render for SortProperty { fn render(&self, writer: &mut impl std::fmt::Write) -> std::fmt::Result { writer.write_fmt(format_args!("{}", self as &dyn UriDisplay)) } } impl markup::Render for SortOrder { fn render(&self, writer: &mut impl std::fmt::Write) -> std::fmt::Result { writer.write_fmt(format_args!("{}", self as &dyn UriDisplay)) } } impl markup::Render for FilterProperty { fn render(&self, writer: &mut impl std::fmt::Write) -> std::fmt::Result { writer.write_fmt(format_args!("{}", self as &dyn UriDisplay)) } } impl RenderAttributeValue for SortOrder {} impl RenderAttributeValue for FilterProperty {} impl RenderAttributeValue for SortProperty {}