aboutsummaryrefslogtreecommitdiff
path: root/common/src/lib.rs
blob: 14c82fc73f8e8e7c17a340ba0339a94af3bcc152 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
    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>
*/
#![feature(array_try_map)]
pub mod api;
pub mod config;
pub mod helpers;
pub mod r#impl;
pub mod jhls;
pub mod routes;
pub mod stream;
pub mod user;

pub use chrono;

use bincode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use std::{
    collections::{BTreeMap, BTreeSet},
    path::PathBuf,
};

#[macro_export]
macro_rules! url_enum {
    ($(#[$a:meta])* enum $i:ident { $($(#[$va:meta])* $vi:ident = $vk:literal),*, }) => {
        $(#[$a])*
        pub enum $i { $($(#[$va])* $vi),* }
        impl $i {
            pub const ALL: &'static [$i] = &[$($i::$vi),*];
            pub fn to_str(&self) -> &'static str { match self { $(Self::$vi => $vk),* } }
            pub fn from_str_opt(s: &str) -> Option<Self> { match s { $($vk => Some(Self::$vi) ),*, _ => None } }
        }
        impl std::fmt::Display for $i {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str(self.to_str())
            }
        }
        impl std::str::FromStr for $i {
            type Err = ();
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Self::from_str_opt(s).ok_or(())
            }
        }
    };
}

#[derive(Clone, Copy, Debug, Encode, Decode, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NodeID(pub [u8; 32]);

pub enum NodeIDOrSlug {
    ID(NodeID),
    Slug(String),
}

#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode, Decode)]
pub struct Node {
    #[serde(default)]
    pub slug: String,
    #[serde(default)]
    pub parents: BTreeSet<NodeID>,
    pub kind: NodeKind,
    pub poster: Option<Asset>,
    pub backdrop: Option<Asset>,
    pub title: Option<String>,
    pub subtitle: Option<String>,
    pub tagline: Option<String>,
    pub description: Option<String>,
    pub release_date: Option<i64>, // in unix millis
    pub index: Option<usize>,
    pub media: Option<MediaInfo>,
    #[serde(default)]
    pub ratings: BTreeMap<Rating, f64>,
    pub federated: Option<String>,
    #[serde(default)]
    pub tags: BTreeSet<String>,
    #[serde(default)]
    pub people: BTreeMap<PeopleGroup, Vec<Appearance>>,
    #[serde(default)]
    pub external_ids: BTreeMap<String, String>,
    #[serde(default)]
    pub visibility: Visibility,
    #[serde(default)]
    pub storage_size: u64,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Encode, Decode)]
pub struct Asset(pub String);

#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode, Decode)]
pub struct Appearance {
    #[serde(default)]
    pub jobs: Vec<String>,
    #[serde(default)]
    pub characters: Vec<String>,
    pub person: Person,
}

#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode, Decode)]
pub struct Person {
    pub name: String,
    pub headshot: Option<Asset>,
    pub ids: ObjectIds,
}

#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode, Decode)]
pub struct ObjectIds {
    pub trakt: Option<u64>,
    pub slug: Option<String>,
    pub imdb: Option<String>,
    pub tmdb: Option<u64>,
    pub omdb: Option<u64>,
    pub tvdb: Option<u64>,
}

url_enum!(
    #[derive(
        Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Encode, Decode,
    )]
    #[serde(rename_all = "snake_case")]
    enum PeopleGroup {
        Cast = "cast",
        Writing = "writing",
        Directing = "directing",
        Art = "art",
        Sound = "sound",
        Camera = "camera",
        Lighting = "lighting",
        Crew = "crew",
        Editing = "editing",
        Production = "production",
        Vfx = "vfx",
        CostumeMakeup = "costume_makeup",
        CreatedBy = "created_by",
        // https://musicbrainz.org/relationships/artist-recording
        // modelling after this, but its too many categories
        Performance = "performance",
        Instrument = "instrument",
        Vocal = "vocal",
        Arranger = "arranger",
        Producer = "producer",
        Engineer = "engineer",
    }
);

#[derive(
    Debug,
    Clone,
    Copy,
    Deserialize,
    Serialize,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Encode,
    Decode,
    Default,
)]
#[serde(rename_all = "snake_case")]
pub enum Visibility {
    Hidden,
    Reduced,
    #[default]
    Visible,
}

url_enum!(
    #[derive(
        Debug,
        Clone,
        Copy,
        Deserialize,
        Serialize,
        PartialEq,
        Eq,
        Default,
        Encode,
        Decode,
        PartialOrd,
        Ord,
    )]
    #[serde(rename_all = "snake_case")]
    enum NodeKind {
        #[default]
        Unknown = "unknown",
        Movie = "movie",
        Video = "video",
        Music = "music",
        ShortFormVideo = "short_form_video",
        Collection = "collection",
        Channel = "channel",
        Show = "show",
        Series = "series",
        Season = "season",
        Episode = "episode",
    }
);

#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
#[serde(rename_all = "snake_case")]
pub enum TrackSource {
    Local(Asset),
    Remote(usize),
}

pub type TrackID = usize;

#[derive(Debug, Clone, Deserialize, Serialize, Hash, Encode, Decode, PartialEq, Eq)]
pub struct LocalTrack {
    pub path: PathBuf,
    pub track: TrackID,
}

#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
pub struct MediaInfo {
    pub duration: f64, // in seconds
    pub tracks: Vec<SourceTrack>,
    #[serde(default)]
    pub chapters: Vec<Chapter>,
}

#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode, Decode)]
pub struct Chapter {
    pub time_start: Option<f64>,
    pub time_end: Option<f64>,
    pub labels: Vec<(String, String)>,
}

#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
pub struct SourceTrack {
    pub source: TrackSource,
    pub kind: SourceTrackKind,
    pub name: String,
    pub codec: String,
    pub language: String,
    pub default_duration: Option<u64>,
    pub seek_pre_roll: u64,
    pub codec_delay: u64,
    pub flag_lacing: u64,
    #[serde(default)]
    pub federated: Vec<String>,
}

url_enum!(
    #[derive(
        Debug, Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode,
    )]
    #[serde(rename_all = "snake_case")]
    enum Rating {
        Imdb = "imdb",
        Tmdb = "tmdb",
        RottenTomatoes = "rotten_tomatoes",
        Metacritic = "metacritic",
        YoutubeViews = "youtube_views",
        YoutubeLikes = "youtube_likes",
        YoutubeFollowers = "youtube_followers",
        Trakt = "trakt",
    }
);

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Encode, Decode)]
#[serde(rename_all = "snake_case")]
pub enum SourceTrackKind {
    Video {
        width: u64,
        height: u64,
        display_width: Option<u64>,
        display_height: Option<u64>,
        display_unit: u64,
        fps: Option<f64>,
    },
    Audio {
        channels: usize,
        sample_rate: f64,
        bit_depth: Option<usize>,
    },
    Subtitle,
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, Hash, PartialEq, Encode, Decode)]
#[serde(rename_all = "snake_case")]
pub enum TraktKind {
    Movie,
    Show,
    Season,
    Episode,
    Person,
    User,
}

#[derive(Debug, Clone, Copy, Hash, Serialize, Deserialize)]
pub enum TmdbKind {
    Tv,
    Movie,
}