1use crate::domain::tools::rating_serializer;
2use crate::{
3 domain::{
4 other_ids::OtherIds,
5 rs_ids::{ApplyRsIds, RsIds},
6 },
7 url::RsLink,
8};
9use serde::{Deserialize, Serialize};
10use serde_json::Value;
11use strum_macros::{Display, EnumString};
12
13#[derive(Serialize, Deserialize, Default, Debug, PartialEq, Clone, Display, EnumString)]
14#[serde(rename_all = "camelCase")]
15#[strum(serialize_all = "camelCase")]
16pub enum MovieStatus {
17 Returning,
18 Released,
19 InProduction,
20 PostProduction,
21 Planned,
22 Rumored,
23 Canceled,
24 #[strum(default)]
25 Other(String),
26 #[default]
27 Unknown,
28}
29
30#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
31#[serde(rename_all = "camelCase")]
32pub struct Movie {
33 #[serde(default)]
34 pub id: String,
35
36 pub name: String,
37 #[serde(rename = "type")]
38 pub kind: Option<Value>,
39 pub year: Option<u16>,
40 pub airdate: Option<i64>,
41 pub digitalairdate: Option<i64>,
42
43 pub duration: Option<u32>,
44 pub overview: Option<String>,
45 pub country: Option<String>,
46 pub status: Option<MovieStatus>,
47
48 pub imdb: Option<String>,
49 pub slug: Option<String>,
50 pub tmdb: Option<u64>,
51 pub trakt: Option<u64>,
52 pub otherids: Option<OtherIds>,
53
54 pub lang: Option<String>,
55 pub original: Option<String>,
56
57 #[serde(serialize_with = "rating_serializer")]
58 pub imdb_rating: Option<f32>,
59 pub imdb_votes: Option<u64>,
60 #[serde(serialize_with = "rating_serializer")]
61 pub trakt_rating: Option<f32>,
62 pub trakt_votes: Option<u32>,
63
64 pub trailer: Option<RsLink>,
65
66 #[serde(default)]
67 pub modified: u64,
68 #[serde(default)]
69 pub added: u64,
70
71 #[serde(skip_serializing_if = "Option::is_none")]
72 pub watched: Option<i64>,
73 #[serde(skip_serializing_if = "Option::is_none")]
74 pub progress: Option<u64>,
75 #[serde(skip_serializing_if = "Option::is_none")]
77 pub progress_modified: Option<u64>,
78
79 #[serde(default)]
80 pub posterv: u64,
81 #[serde(default)]
82 pub backgroundv: u64,
83 #[serde(default)]
84 pub cardv: u64,
85}
86
87impl From<Movie> for RsIds {
88 fn from(value: Movie) -> Self {
89 let mut ids = RsIds::default();
90 if let Some(v) = value.trakt { ids.set("trakt", v); }
91 if let Some(v) = value.slug { ids.set("slug", v); }
92 if let Some(v) = value.imdb { ids.set("imdb", v); }
93 if let Some(v) = value.tmdb { ids.set("tmdb", v); }
94 if let Some(other) = value.otherids {
95 for entry in other.into_vec() {
96 let _ = ids.try_add(entry);
97 }
98 }
99 if ids.try_add(value.id.clone()).is_err() {
100 ids.set("redseat", value.id);
101 }
102 ids
103 }
104}
105
106impl ApplyRsIds for Movie {
107 fn apply_rs_ids(&mut self, ids: &RsIds) {
108 if let Some(redseat) = ids.redseat() {
109 self.id = redseat.to_string();
110 }
111 if let Some(trakt) = ids.trakt() {
112 self.trakt = Some(trakt);
113 }
114 if let Some(slug) = ids.slug() {
115 self.slug = Some(slug.to_string());
116 }
117 if let Some(imdb) = ids.imdb() {
118 self.imdb = Some(imdb.to_string());
119 }
120 if let Some(tmdb) = ids.tmdb() {
121 self.tmdb = Some(tmdb);
122 }
123 let known: &[&str] = &["redseat", "trakt", "slug", "imdb", "tmdb"];
124 let mut other = self.otherids.take().unwrap_or_default();
125 for (k, v) in ids.iter() {
126 if !known.contains(&k.as_str()) { other.add(k, v); }
127 }
128 if !other.as_slice().is_empty() { self.otherids = Some(other); }
129 }
130}
131
132#[cfg(test)]
133mod tests {
134 use super::Movie;
135 use crate::domain::{
136 other_ids::OtherIds,
137 rs_ids::{ApplyRsIds, RsIds},
138 };
139 use serde_json::json;
140
141 #[test]
142 fn movie_otherids_serializes_as_array_and_rejects_string() {
143 let movie = Movie {
144 id: "movie-1".to_string(),
145 name: "Movie 1".to_string(),
146 otherids: Some(OtherIds(vec!["imdb:tt1234567".to_string()])),
147 ..Default::default()
148 };
149 let value = serde_json::to_value(&movie).unwrap();
150 assert_eq!(value.get("otherids"), Some(&json!(["imdb:tt1234567"])));
151
152 let parsed: Movie = serde_json::from_value(json!({
153 "id": "movie-1",
154 "name": "Movie 1",
155 "otherids": ["tmdb:42"]
156 }))
157 .unwrap();
158 assert_eq!(parsed.otherids, Some(OtherIds(vec!["tmdb:42".to_string()])));
159
160 let invalid = serde_json::from_value::<Movie>(json!({
161 "id": "movie-1",
162 "name": "Movie 1",
163 "otherids": "tmdb:42"
164 }));
165 assert!(invalid.is_err());
166 }
167
168 #[test]
169 fn movie_progress_modified_is_camel_case_and_omitted_when_none() {
170 let movie = Movie {
171 id: "movie-1".to_string(),
172 name: "Movie 1".to_string(),
173 progress: Some(5000),
174 progress_modified: Some(1_790_000_000_000),
175 ..Default::default()
176 };
177 let value = serde_json::to_value(&movie).unwrap();
178 assert_eq!(
179 value.get("progressModified"),
180 Some(&json!(1_790_000_000_000u64))
181 );
182 assert_eq!(serde_json::from_value::<Movie>(value).unwrap(), movie);
183
184 let without = serde_json::to_value(Movie::default()).unwrap();
185 assert!(without.get("progressModified").is_none());
186 }
187
188 #[test]
189 fn movie_apply_rs_ids_updates_only_present_values() {
190 let mut movie = Movie {
191 id: "movie-old".to_string(),
192 name: "Movie".to_string(),
193 tmdb: Some(10),
194 ..Default::default()
195 };
196 let mut ids = RsIds::default();
197 ids.set("redseat", "movie-new");
198 ids.set("trakt", 100u64);
199 ids.set("imdb", "tt0000100");
200 ids.set("slug", "movie-slug");
201
202 movie.apply_rs_ids(&ids);
203
204 assert_eq!(movie.id, "movie-new");
205 assert_eq!(movie.trakt, Some(100));
206 assert_eq!(movie.imdb.as_deref(), Some("tt0000100"));
207 assert_eq!(movie.slug.as_deref(), Some("movie-slug"));
208 assert_eq!(movie.tmdb, Some(10));
209 }
210}
211
212#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
215#[serde(rename_all = "camelCase")]
216pub struct MovieForUpdate {
217 pub add_tags: Option<Vec<crate::domain::media::MediaItemReference>>,
218 pub remove_tags: Option<Vec<String>>,
219 pub add_series: Option<Vec<crate::domain::media::FileEpisode>>,
220 pub remove_series: Option<Vec<crate::domain::media::FileEpisode>>,
221 pub name: Option<String>,
222 #[serde(rename = "type")]
223 pub kind: Option<Value>,
224 pub year: Option<u32>,
225 pub airdate: Option<i64>,
226 pub digitalairdate: Option<i64>,
227
228 pub duration: Option<u32>,
229 pub overview: Option<String>,
230 pub country: Option<String>,
231 pub status: Option<MovieStatus>,
232
233 pub imdb: Option<String>,
234 pub slug: Option<String>,
235 pub tmdb: Option<u64>,
236 pub trakt: Option<u64>,
237 pub otherids: Option<String>,
238
239 pub lang: Option<String>,
240 pub original: Option<String>,
241 #[serde(rename = "imdb_rating")]
242 pub imdb_rating: Option<f32>,
243 #[serde(rename = "imdb_votes")]
244 pub imdb_votes: Option<u64>,
245 #[serde(rename = "trakt_rating")]
246 pub trakt_rating: Option<f32>,
247 #[serde(rename = "trakt_votes")]
248 pub trakt_votes: Option<u32>,
249 pub trailer: Option<RsLink>,
250}
251
252impl MovieForUpdate {
253 pub fn from_refresh(movie: &Movie, new_movie: Movie) -> serde_json::Result<Self> {
254 let mut updates = MovieForUpdate {
255 ..Default::default()
256 };
257
258 if movie.name != new_movie.name {
259 updates.name = Some(new_movie.name);
260 }
261 if movie.kind != new_movie.kind {
262 updates.kind = new_movie.kind;
263 }
264 if movie.year != new_movie.year {
265 updates.year = new_movie.year.map(u32::from);
266 }
267 if movie.duration != new_movie.duration {
268 updates.duration = new_movie.duration;
269 }
270 if movie.overview != new_movie.overview {
271 updates.overview = new_movie.overview;
272 }
273 if movie.country != new_movie.country {
274 updates.country = new_movie.country;
275 }
276 if movie.lang != new_movie.lang {
277 updates.lang = new_movie.lang;
278 }
279 if movie.original != new_movie.original {
280 updates.original = new_movie.original;
281 }
282 if movie.slug != new_movie.slug {
283 updates.slug = new_movie.slug;
284 }
285 if movie.trakt != new_movie.trakt {
286 updates.trakt = new_movie.trakt;
287 }
288 let merged_ids = crate::domain::merge_refresh_ids(
289 movie.otherids.as_ref(),
290 new_movie.otherids.as_ref(),
291 );
292 if merged_ids != movie.otherids {
293 updates.otherids = merged_ids.as_ref().map(serde_json::to_string).transpose()?;
294 }
295 if movie.imdb_rating != new_movie.imdb_rating {
296 updates.imdb_rating = new_movie.imdb_rating;
297 }
298 if movie.imdb_votes != new_movie.imdb_votes {
299 updates.imdb_votes = new_movie.imdb_votes;
300 }
301 if movie.status != new_movie.status {
302 updates.status = new_movie.status;
303 }
304 if movie.trakt_rating != new_movie.trakt_rating {
305 updates.trakt_rating = new_movie.trakt_rating;
306 }
307 if movie.trakt_votes != new_movie.trakt_votes {
308 updates.trakt_votes = new_movie.trakt_votes;
309 }
310 if movie.trailer != new_movie.trailer {
311 updates.trailer = new_movie.trailer;
312 }
313 if movie.imdb != new_movie.imdb {
314 updates.imdb = new_movie.imdb;
315 }
316 if movie.tmdb != new_movie.tmdb {
317 updates.tmdb = new_movie.tmdb;
318 }
319 if movie.digitalairdate != new_movie.digitalairdate {
320 updates.digitalairdate = new_movie.digitalairdate;
321 }
322 if movie.airdate != new_movie.airdate {
323 updates.airdate = new_movie.airdate;
324 }
325 Ok(updates)
326 }
327
328 pub fn has_update(&self) -> bool {
329 self.add_tags.is_some()
330 || self.remove_tags.is_some()
331 || self.add_series.is_some()
332 || self.remove_series.is_some()
333 || self.name.is_some()
334 || self.kind.is_some()
335 || self.status.is_some()
336 || self.digitalairdate.is_some()
337 || self.airdate.is_some()
338 || self.imdb.is_some()
339 || self.slug.is_some()
340 || self.tmdb.is_some()
341 || self.trakt.is_some()
342 || self.otherids.is_some()
343 || self.imdb_rating.is_some()
344 || self.imdb_votes.is_some()
345 || self.trakt_rating.is_some()
346 || self.trakt_votes.is_some()
347 || self.trailer.is_some()
348 || self.year.is_some()
349 || self.duration.is_some()
350 || self.overview.is_some()
351 || self.country.is_some()
352 || self.lang.is_some()
353 || self.original.is_some()
354 }
355}
356
357#[derive(Debug, Serialize, Deserialize, Clone)]
358#[serde(rename_all = "camelCase")]
359pub struct MovieWithAction {
360 pub action: crate::domain::ElementAction,
361 pub movie: crate::domain::ItemWithRelations<Movie>,
362}
363
364#[derive(Debug, Serialize, Deserialize, Clone)]
365#[serde(rename_all = "camelCase")]
366pub struct MoviesMessage {
367 pub library: String,
368 pub movies: Vec<MovieWithAction>,
369}
370
371#[cfg(test)]
372mod update_tests {
373 use super::{Movie, MovieForUpdate};
374 use crate::domain::other_ids::OtherIds;
375
376 #[test]
377 fn movie_refresh_copies_changed_other_ids_and_preserves_missing_ids() {
378 let mut movie = Movie::default();
379 for value in ["provider:first", "provider:changed"] {
380 let ids = OtherIds::from(vec![value.to_string()]);
381 let refreshed = Movie {
382 otherids: Some(ids.clone()),
383 ..movie.clone()
384 };
385 let update = MovieForUpdate::from_refresh(&movie, refreshed.clone()).unwrap();
386 assert!(update.has_update());
387 assert_eq!(
388 serde_json::from_str::<OtherIds>(update.otherids.as_ref().unwrap()).unwrap(),
389 ids
390 );
391 movie = refreshed;
392 }
393 let unchanged = MovieForUpdate::from_refresh(&movie, movie.clone()).unwrap();
394 assert!(!unchanged.has_update());
395 let missing = MovieForUpdate::from_refresh(&movie, Movie::default()).unwrap();
396 assert!(missing.otherids.is_none());
397 assert!(!missing.has_update());
398 }
399
400 #[test]
401 fn movie_refresh_merges_partial_other_ids_by_provider() {
402 let movie = Movie {
403 otherids: Some(OtherIds::from(vec![
404 "provider:first".to_string(),
405 "offline:keep".to_string(),
406 ])),
407 ..Default::default()
408 };
409 let refreshed = Movie {
410 otherids: Some(OtherIds::from(vec![
411 "PROVIDER:changed:123".to_string(),
412 "new:456".to_string(),
413 ])),
414 ..movie.clone()
415 };
416 let update = MovieForUpdate::from_refresh(&movie, refreshed).unwrap();
417 let merged: OtherIds = serde_json::from_str(update.otherids.as_ref().unwrap()).unwrap();
418 assert_eq!(
419 merged.as_slice(),
420 &["provider:changed:123", "offline:keep", "new:456"]
421 );
422 for ids in [
423 OtherIds::default(),
424 OtherIds::from(vec!["provider:first".to_string()]),
425 ] {
426 let refreshed = Movie {
427 otherids: Some(ids),
428 ..movie.clone()
429 };
430 assert!(!MovieForUpdate::from_refresh(&movie, refreshed)
431 .unwrap()
432 .has_update());
433 }
434 }
435
436 #[test]
437 fn movie_update_duration_rejects_values_outside_stored_range() {
438 for duration in [0, u32::MAX as u64] {
439 let update: MovieForUpdate =
440 serde_json::from_value(serde_json::json!({"duration": duration})).unwrap();
441 assert_eq!(update.duration, Some(duration as u32));
442 }
443 for duration in [u32::MAX as u64 + 1, i64::MAX as u64] {
444 assert!(serde_json::from_value::<MovieForUpdate>(
445 serde_json::json!({"duration": duration})
446 )
447 .is_err());
448 }
449 }
450}
451
452#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Display, EnumString, Default)]
456#[strum(serialize_all = "camelCase")]
457#[serde(rename_all = "camelCase")]
458#[serde(untagged)]
459pub enum RsMovieSort {
460 Modified,
461 Added,
462 Created,
463 #[default]
464 Name,
465 Digitalairdate,
466 #[strum(default)]
467 Custom(String),
468}
469
470#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
472pub struct MovieQuery {
473 pub person: Option<String>,
474 pub role: Option<crate::domain::person::PersonType>,
475 pub after: Option<i64>,
477 pub in_digital: Option<bool>,
478
479 pub watched: Option<bool>,
480
481 #[serde(default)]
482 pub sort: RsMovieSort,
483 pub order: Option<crate::domain::media::SqlOrder>,
484 pub limit: Option<u32>,
485 pub offset: Option<u32>,
486}
487
488impl MovieQuery {
489 pub fn new_empty() -> MovieQuery {
490 MovieQuery::default()
491 }
492 pub fn from_after(after: i64) -> MovieQuery {
493 MovieQuery {
494 after: Some(after),
495 ..Default::default()
496 }
497 }
498}