aboutsummaryrefslogtreecommitdiff
path: root/common/src/api.rs
blob: a58c4457797255850407b95f7570a22067b10ffe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2025 metamuffin <metamuffin.org>
*/
use crate::{user::NodeUserData, Node, NodeKind};
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, sync::Arc, time::Duration};

type NodesWithUdata = Vec<(Arc<Node>, NodeUserData)>;

#[derive(Serialize, Deserialize)]
pub struct ApiNodeResponse {
    pub parents: NodesWithUdata,
    pub children: NodesWithUdata,
    pub node: Arc<Node>,
    pub userdata: NodeUserData,
}

#[derive(Serialize, Deserialize)]
pub struct ApiSearchResponse {
    pub count: usize,
    pub results: NodesWithUdata,
    pub duration: Duration,
}

#[derive(Serialize, Deserialize)]
pub struct ApiItemsResponse {
    pub count: usize,
    pub pages: usize,
    pub items: NodesWithUdata,
}

#[derive(Serialize, Deserialize)]
pub struct ApiHomeResponse {
    pub toplevel: NodesWithUdata,
    pub categories: Vec<(String, NodesWithUdata)>,
}

#[derive(Serialize, Deserialize)]
pub struct ApiStatsResponse {
    pub kinds: BTreeMap<NodeKind, StatsBin>,
    pub total: StatsBin,
}

#[derive(Default, Serialize, Deserialize)]
pub struct StatsBin {
    pub runtime: f64,
    pub size: u64,
    pub count: usize,
    pub max_runtime: (f64, String),
    pub max_size: (u64, String),
}

#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "rocket", derive(FromForm, UriDisplayQuery))]
pub struct NodeFilterSort {
    pub sort_by: Option<SortProperty>,
    pub filter_kind: Option<Vec<FilterProperty>>,
    pub sort_order: Option<SortOrder>,
}


pub trait UrlEnum: Sized {
    fn to_str(&self) -> &'static str;
    fn from_str(s: &str) -> Option<Self>;
}
macro_rules! url_enum {
    (enum $i:ident { $($vi:ident = $vk:literal),*, }) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq)]
        #[cfg_attr(feature = "rocket", derive(FromFormField, UriDisplayQuery))]
        pub enum $i { $(#[cfg_attr(feature = "rocket", field(value = $vk))] $vi),* }
        impl $i { pub const ALL: &'static [$i] = &[$($i::$vi),*]; }
        impl UrlEnum for $i {
            fn to_str(&self) -> &'static str { match self { $(Self::$vi => $vk),* } }
            fn from_str(s: &str) -> Option<Self> { match s { $($vk => Some(Self::$vi) ),*, _ => None } }
        }
    };
}

url_enum!(
    enum FilterProperty {
        FederationLocal = "fed_local",
        FederationRemote = "fed_remote",
        Watched = "watched",
        Unwatched = "unwatched",
        WatchProgress = "watch_progress",
        KindMovie = "kind_movie",
        KindVideo = "kind_video",
        KindShortFormVideo = "kind_short_form_video",
        KindMusic = "kind_music",
        KindCollection = "kind_collection",
        KindChannel = "kind_channel",
        KindShow = "kind_show",
        KindSeries = "kind_series",
        KindSeason = "kind_season",
        KindEpisode = "kind_episode",
    }
);
url_enum!(
    enum SortProperty {
        ReleaseDate = "release_date",
        Title = "title",
        Index = "index",
        Duration = "duration",
        RatingRottenTomatoes = "rating_rt",
        RatingMetacritic = "rating_mc",
        RatingImdb = "rating_imdb",
        RatingTmdb = "rating_tmdb",
        RatingYoutubeViews = "rating_yt_views",
        RatingYoutubeLikes = "rating_yt_likes",
        RatingYoutubeFollowers = "rating_yt_followers",
        RatingUser = "rating_user",
        RatingLikesDivViews = "rating_loved",
    }
);
url_enum!(
    enum SortOrder {
        Ascending = "ascending",
        Descending = "descending",
    }    
);