aboutsummaryrefslogtreecommitdiff
path: root/common/src/lib.rs
blob: d06c40c975f19a7db963186b622f89cc1b2f813f (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
    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) 2024 metamuffin <metamuffin.org>
*/
pub mod config;
pub mod helpers;
pub mod r#impl;
pub mod jhls;
pub mod seek_index;
pub mod stream;
pub mod user;

pub use chrono;

use bincode::{Decode, Encode};
#[cfg(feature = "rocket")]
use rocket::{FromFormField, UriDisplayQuery};
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, fmt::Display, path::PathBuf};

#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode, Decode)]
pub struct Node {
    #[serde(default)]
    pub public: NodePublic,
    #[serde(default)]
    pub private: NodePrivate,
}

#[rustfmt::skip]
#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode,Decode)]
pub struct NodePrivate {
    #[serde(default)] pub id: Option<String>,
    #[serde(default)] pub source: Option<Vec<TrackSource>>,

    #[serde(default)] pub poster: Option<PathBuf>,
    #[serde(default)] pub backdrop: Option<PathBuf>,
}

#[rustfmt::skip]
#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode, Decode)]
pub struct NodePublic {
    #[serde(default)] pub kind: Option<NodeKind>,

    #[serde(default)] pub poster: Option<Asset>,
    #[serde(default)] pub backdrop: Option<Asset>,

    #[serde(default)] pub title: Option<String>,
    #[serde(default)] pub subtitle: Option<String>,
    #[serde(default)] pub id: Option<String>,
    #[serde(default)] pub path: Vec<String>,
    #[serde(default)] pub tagline: Option<String>,
    #[serde(default)] pub children: Vec<String>,
    #[serde(default)] pub description: Option<String>,
    #[serde(default)] pub release_date: Option<i64>, // in unix millis
    #[serde(default)] pub index: Option<usize>,
    #[serde(default)] pub media: Option<MediaInfo>,
    #[serde(default)] pub ratings: BTreeMap<Rating, f64>,
    #[serde(default)] pub federated: Option<String>,
}

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

#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode, Decode)]
pub struct ExtendedNode {
    pub ids: ObjectIds,
    pub people: BTreeMap<PeopleGroup, Vec<Appearance>>,
}

#[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>,
}

#[rustfmt::skip]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)]
#[cfg_attr(feature = "rocket", derive(FromFormField, UriDisplayQuery))]
#[serde(rename_all = "snake_case")]
pub enum PeopleGroup {
    #[cfg_attr(feature = "rocket", field(value = "cast"))] Cast,
    #[cfg_attr(feature = "rocket", field(value = "writing"))] Writing,
    #[cfg_attr(feature = "rocket", field(value = "directing"))] Directing,
    #[cfg_attr(feature = "rocket", field(value = "art"))] Art,
    #[cfg_attr(feature = "rocket", field(value = "sound"))] Sound,
    #[cfg_attr(feature = "rocket", field(value = "camera"))] Camera,
    #[cfg_attr(feature = "rocket", field(value = "lighting"))] Lighting,
    #[cfg_attr(feature = "rocket", field(value = "crew"))] Crew,
    #[cfg_attr(feature = "rocket", field(value = "editing"))] Editing,
    #[cfg_attr(feature = "rocket", field(value = "production"))] Production,
    #[cfg_attr(feature = "rocket", field(value = "vfx"))] Vfx,
    #[cfg_attr(feature = "rocket", field(value = "costume"))] CostumeMakeup,
    #[cfg_attr(feature = "rocket", field(value = "createdby"))] CreatedBy,
}

#[derive(Debug, Clone, Deserialize, Serialize, Default, Encode, Decode)]
pub struct ImportOptions {
    pub id: String,
    pub sources: Vec<ImportSource>,
}

#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode)]
#[serde(rename_all = "snake_case")]
pub enum ImportSource {
    Override(Node),
    Tmdb {
        kind: TmdbKind,
        id: u64,
    },
    Trakt {
        kind: TraktKind,
        id: u64,
    },
    AutoChildren {
        path: Option<PathBuf>,
    },
    Media {
        path: PathBuf,
        #[serde(default)]
        ignore_metadata: bool,
        #[serde(default)]
        ignore_attachments: bool,
        #[serde(default)]
        ignore_chapters: bool,
    },
    Federated {
        host: String,
    },
}

#[rustfmt::skip]
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default, Encode,Decode)]
#[cfg_attr(feature = "rocket", derive(FromFormField, UriDisplayQuery))]
#[serde(rename_all = "snake_case")]
pub enum NodeKind {
    #[cfg_attr(feature = "rocket", field(value = "movie"))] #[default] Movie,
    #[cfg_attr(feature = "rocket", field(value = "video"))] Video,
    #[cfg_attr(feature = "rocket", field(value = "shortformvideo"))] ShortFormVideo,
    #[cfg_attr(feature = "rocket", field(value = "collection"))] Collection,
    #[cfg_attr(feature = "rocket", field(value = "channel"))] Channel,
    #[cfg_attr(feature = "rocket", field(value = "show"))] Show,
    #[cfg_attr(feature = "rocket", field(value = "series"))] Series,
    #[cfg_attr(feature = "rocket", field(value = "season"))] Season,
    #[cfg_attr(feature = "rocket", field(value = "episode"))] Episode,
}

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

pub type TrackID = usize;

#[derive(Debug, Clone, Deserialize, Serialize, Encode, Decode, Hash)]
pub struct LocalTrack {
    pub path: PathBuf,
    pub track: TrackID,
    pub codec_private: Option<Vec<u8>>,
}

#[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 kind: SourceTrackKind,
    pub name: String,
    pub codec: String,
    pub language: String,
    pub default_duration: Option<u64>,
    #[serde(default)]
    pub federated: Vec<String>,
}

#[derive(
    Debug, Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode,
)]
#[serde(rename_all = "snake_case")]
pub enum Rating {
    Imdb,
    Tmdb,
    RottenTomatoes,
    Metacritic,
    YoutubeViews,
    YoutubeLikes,
    YoutubeFollowers,
    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: Option<u64>,
        fps: Option<f64>,
    },
    Audio {
        channels: usize,
        sample_rate: f64,
        bit_depth: Option<usize>,
    },
    Subtitles,
}

#[rustfmt::skip]
#[derive(Debug, Clone, Copy, Encode, Decode)]
#[cfg_attr(feature = "rocket", derive(FromFormField, UriDisplayQuery))]
pub enum AssetRole {
    #[cfg_attr(feature = "rocket", field(value = "poster"))] Poster,
    #[cfg_attr(feature = "rocket", field(value = "backdrop"))] Backdrop,
}

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

impl TraktKind {
    pub fn singular(self) -> &'static str {
        match self {
            TraktKind::Movie => "movie",
            TraktKind::Show => "show",
            TraktKind::Season => "season",
            TraktKind::Episode => "episode",
            TraktKind::Person => "person",
            TraktKind::User => "user",
        }
    }
    pub fn plural(self) -> &'static str {
        match self {
            TraktKind::Movie => "movies",
            TraktKind::Show => "shows",
            TraktKind::Season => "seasons",
            TraktKind::Episode => "episodes",
            TraktKind::Person => "people",
            TraktKind::User => "users", // //! not used in API
        }
    }
}
impl ToString for TraktKind {
    fn to_string(&self) -> String {
        match self {
            TraktKind::Movie => "Movie",
            TraktKind::Show => "Show",
            TraktKind::Season => "Season",
            TraktKind::Episode => "Episode",
            TraktKind::Person => "Person",
            TraktKind::User => "User",
        }
        .to_string()
    }
}
impl Display for ObjectIds {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(id) = self.trakt {
            f.write_fmt(format_args!("trakt={}", id))?;
        }
        if let Some(_id) = &self.slug {
            f.write_str(",slug")?;
        }
        if let Some(id) = self.tmdb {
            f.write_fmt(format_args!(",tmdb={}", id))?;
        }
        if let Some(_id) = &self.imdb {
            f.write_str(",imdb")?;
        }
        if let Some(_id) = &self.tvdb {
            f.write_str(",tvdb")?;
        }
        if let Some(_id) = &self.omdb {
            f.write_str(",omdb")?;
        }
        Ok(())
    }
}
impl Display for PeopleGroup {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            PeopleGroup::Cast => "Cast",
            PeopleGroup::Writing => "Writing",
            PeopleGroup::Directing => "Directing",
            PeopleGroup::Art => "Art",
            PeopleGroup::Sound => "Sound",
            PeopleGroup::Camera => "Camera",
            PeopleGroup::Lighting => "Lighting",
            PeopleGroup::Crew => "Crew",
            PeopleGroup::Editing => "Editing",
            PeopleGroup::Production => "Production",
            PeopleGroup::Vfx => "Visual Effects",
            PeopleGroup::CostumeMakeup => "Costume & Makeup",
            PeopleGroup::CreatedBy => "Created by:",
        })
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Encode, Decode)]
pub enum TmdbKind {
    Tv,
    Movie,
}
impl Display for TmdbKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            TmdbKind::Tv => "tv",
            TmdbKind::Movie => "movie",
        })
    }
}