Debian Codegraph @

podcasts-data/src/models/episode.rs

raw symbols SCIP
1 // episode.rs
2 //
3 // Copyright 2017 Jordan Petridis <[email protected]>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // SPDX-License-Identifier: GPL-3.0-or-later
19
20 use chrono::prelude::*;
21 use diesel::prelude::*;
22
23 use crate::database::connection;
24 use crate::errors::DataError;
25 use crate::make_id_wrapper;
26 use crate::models::{Save, Show, ShowId};
27 use crate::schema::episodes;
28
29 make_id_wrapper!(EpisodeId);
30
31 /// A trait to get Episode data regardless.
32 pub trait EpisodeModel {
33 /// Get the model id. `id()` already exists in Diesel.
34 fn id(&self) -> EpisodeId;
35
36 /// Get the value of the `uri`.
37 ///
38 /// Represents the url(usually) that the media file will be located at.
39 fn uri(&self) -> Option<&str>;
40
41 /// Epoch representation of the last time the episode was played.
42 ///
43 /// None/Null for unplayed.
44 fn played(&self) -> Option<NaiveDateTime>;
45 }
46
47 #[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq, Selectable)]
48 #[diesel(table_name = episodes)]
49 #[diesel(treat_none_as_null = true)]
50 #[diesel(belongs_to(Show, foreign_key = show_id))]
51 #[derive(Debug, Clone)]
52 /// Diesel Model of the episode table.
53 pub struct Episode {
54 id: EpisodeId,
55 title: String,
56 uri: Option<String>,
57 local_uri: Option<String>,
58 description: Option<String>,
59 image_uri: Option<String>,
60 epoch: NaiveDateTime,
61 length: Option<i32>,
62 duration: Option<i32>,
63 guid: Option<String>,
64 played: Option<NaiveDateTime>,
65 play_position: i32,
66 show_id: ShowId,
67 }
68
69 impl EpisodeModel for Episode {
70 /// Get the value of the sqlite's `ROW_ID`
71 fn id(&self) -> EpisodeId {
72 self.id
73 }
74
75 /// Get the value of the `uri`.
76 ///
77 /// Represents the url(usually) that the media file will be located at.
78 fn uri(&self) -> Option<&str> {
79 self.uri.as_deref()
80 }
81
82 /// Epoch representation of the last time the episode was played.
83 ///
84 /// None/Null for unplayed.
85 fn played(&self) -> Option<NaiveDateTime> {
86 self.played
87 }
88 }
89
90 impl Save<Episode> for Episode {
91 type Error = DataError;
92
93 /// Helper method to easily save/"sync" current state of self to the
94 /// Database.
95 fn save(&self) -> Result<Episode, Self::Error> {
96 let db = connection();
97 let mut tempdb = db.get()?;
98
99 self.save_changes::<Episode>(&mut tempdb)
100 .map_err(From::from)
101 }
102 }
103
104 impl Episode {
105 /// Get the value of the sqlite's `ROW_ID`
106 pub fn id(&self) -> EpisodeId {
107 self.id
108 }
109
110 /// Get the value of the `title` field.
111 pub fn title(&self) -> &str {
112 &self.title
113 }
114
115 /// Get the value of the `local_uri`.
116 ///
117 /// Represents the local uri,usually filesystem path,
118 /// that the media file will be located at.
119 pub fn local_uri(&self) -> Option<&str> {
120 self.local_uri.as_deref()
121 }
122
123 /// Get the `description`.
124 pub fn description(&self) -> Option<&str> {
125 self.description.as_deref()
126 }
127
128 /// Get the Episode's `guid`.
129 pub fn guid(&self) -> Option<&str> {
130 self.guid.as_deref()
131 }
132
133 /// Get the `epoch` value.
134 ///
135 /// Retrieved from the rss Item publish date.
136 /// Value is set to Utc whenever possible.
137 pub fn epoch(&self) -> NaiveDateTime {
138 self.epoch
139 }
140
141 /// Get the `length`.
142 ///
143 /// The number represents the size of the file in bytes.
144 pub fn length(&self) -> Option<i32> {
145 self.length
146 }
147
148 /// Get the `duration` value.
149 ///
150 /// The number represents the duration of the item/episode in seconds.
151 pub fn duration(&self) -> Option<i32> {
152 self.duration
153 }
154
155 /// `Show` table foreign key.
156 pub fn show_id(&self) -> ShowId {
157 self.show_id
158 }
159
160 /// Get play_position
161 ///
162 /// The number represents the number of seconds played in the episode.
163 /// 0 means the episode was either not played or continued to play to the end.
164 pub fn play_position(&self) -> i32 {
165 self.play_position
166 }
167
168 /// Get image_uri
169 ///
170 /// The uri to the episode specific cover.
171 /// None means no itunes:image was set for this episode.
172 pub fn image_uri(&self) -> Option<&str> {
173 self.image_uri.as_deref()
174 }
175
176 /// Sets `play_position` and saves the record.
177 pub fn set_play_position(&mut self, seconds: i32) -> Result<(), DataError> {
178 self.play_position = seconds;
179 self.save().map(|_| ())
180 }
181
182 /// Sets `play_position` if it diverges multiple seconds (10) from the last value.
183 /// If it doesn't diverge Ok(()) is returned, nothing is written.
184 pub fn set_play_position_if_divergent(&mut self, seconds: i32) -> Result<(), DataError> {
185 if seconds != 0 && self.play_position != 0 {
186 if (seconds - self.play_position).abs() > 10 {
187 return self.set_play_position(seconds);
188 }
189 } else {
190 return self.set_play_position(seconds);
191 }
192 Ok(())
193 }
194 }
195
196 #[derive(Queryable, AsChangeset, PartialEq, Selectable)]
197 #[diesel(table_name = episodes)]
198 #[diesel(treat_none_as_null = true)]
199 #[diesel(primary_key(title, show_id))]
200 #[derive(Debug, Clone)]
201 /// Diesel Model to be used for constructing `EpisodeWidgets`.
202 pub struct EpisodeWidgetModel {
203 id: EpisodeId,
204 title: String,
205 uri: Option<String>,
206 local_uri: Option<String>,
207 epoch: NaiveDateTime,
208 length: Option<i32>,
209 duration: Option<i32>,
210 played: Option<NaiveDateTime>,
211 play_position: i32,
212 show_id: ShowId,
213 }
214
215 impl EpisodeModel for EpisodeWidgetModel {
216 /// Get the value of the sqlite's `ROW_ID`
217 fn id(&self) -> EpisodeId {
218 self.id
219 }
220
221 /// Get the value of the `uri`.
222 ///
223 /// Represents the url(usually) that the media file will be located at.
224 fn uri(&self) -> Option<&str> {
225 self.uri.as_deref()
226 }
227
228 /// Epoch representation of the last time the episode was played.
229 ///
230 /// None/Null for unplayed.
231 fn played(&self) -> Option<NaiveDateTime> {
232 self.played
233 }
234 }
235
236 impl From<Episode> for EpisodeWidgetModel {
237 fn from(e: Episode) -> EpisodeWidgetModel {
238 EpisodeWidgetModel {
239 id: e.id,
240 title: e.title,
241 uri: e.uri,
242 local_uri: e.local_uri,
243 epoch: e.epoch,
244 length: e.length,
245 duration: e.duration,
246 played: e.played,
247 play_position: e.play_position,
248 show_id: e.show_id,
249 }
250 }
251 }
252
253 impl Save<usize> for EpisodeWidgetModel {
254 type Error = DataError;
255
256 /// Helper method to easily save/"sync" current state of self to the
257 /// Database.
258 fn save(&self) -> Result<usize, Self::Error> {
259 use crate::schema::episodes::dsl::*;
260
261 let db = connection();
262 let mut tempdb = db.get()?;
263
264 diesel::update(episodes.filter(id.eq(self.id)))
265 .set(self)
266 .execute(&mut tempdb)
267 .map_err(From::from)
268 }
269 }
270
271 impl EpisodeWidgetModel {
272 /// Get the value of the `title` field.
273 pub fn title(&self) -> &str {
274 &self.title
275 }
276
277 /// Get the value of the `local_uri`.
278 ///
279 /// Represents the local uri,usually filesystem path,
280 /// that the media file will be located at.
281 pub fn local_uri(&self) -> Option<&str> {
282 self.local_uri.as_deref()
283 }
284
285 /// Set the `local_uri`.
286 pub fn set_local_uri(&mut self, value: Option<&str>) {
287 self.local_uri = value.map(|x| x.to_string());
288 }
289
290 /// Get the `epoch` value.
291 ///
292 /// Retrieved from the rss Item publish date.
293 /// Value is set to Utc whenever possible.
294 pub fn epoch(&self) -> NaiveDateTime {
295 self.epoch
296 }
297
298 /// Get the `length`.
299 ///
300 /// The number represents the size of the file in bytes.
301 pub fn length(&self) -> Option<i32> {
302 self.length
303 }
304
305 /// Set the `length`.
306 pub fn set_length(&mut self, value: Option<i32>) {
307 self.length = value;
308 }
309
310 /// Get the `duration` value.
311 ///
312 /// The number represents the duration of the item/episode in seconds.
313 pub fn duration(&self) -> Option<i32> {
314 self.duration
315 }
316
317 /// Set the `played` value.
318 fn set_played(&mut self, value: Option<NaiveDateTime>) {
319 self.played = value;
320 }
321
322 /// `Show` table foreign key.
323 pub fn show_id(&self) -> ShowId {
324 self.show_id
325 }
326
327 /// Set the `played` value to `None` and save it.
328 pub fn set_unplayed(&mut self) -> Result<(), DataError> {
329 self.set_played(None);
330 self.save().map(|_| ())
331 }
332
333 /// Sets the `played` value with the current `epoch` timestap and save it.
334 pub fn set_played_now(&mut self) -> Result<(), DataError> {
335 self.set_played(Some(Utc::now().naive_utc()));
336 self.save().map(|_| ())
337 }
338
339 /// Get play_position
340 ///
341 /// The number represents the number of seconds played in the episode.
342 /// `0` means the episode was either not played or continued to play to the end.
343 pub fn play_position(&self) -> i32 {
344 self.play_position
345 }
346 }
347
348 #[derive(Queryable, AsChangeset, PartialEq, Selectable)]
349 #[diesel(table_name = episodes)]
350 #[diesel(treat_none_as_null = true)]
351 #[diesel(primary_key(title, show_id))]
352 #[derive(Debug, Clone)]
353 /// Diesel Model to be used internal with the `utils::checkup` function.
354 pub struct EpisodeCleanerModel {
355 id: EpisodeId,
356 local_uri: Option<String>,
357 played: Option<NaiveDateTime>,
358 }
359
360 impl Save<usize> for EpisodeCleanerModel {
361 type Error = DataError;
362
363 /// Helper method to easily save/"sync" current state of self to the
364 /// Database.
365 fn save(&self) -> Result<usize, Self::Error> {
366 use crate::schema::episodes::dsl::*;
367
368 let db = connection();
369 let mut tempdb = db.get()?;
370
371 diesel::update(episodes.filter(id.eq(self.id)))
372 .set(self)
373 .execute(&mut tempdb)
374 .map_err(From::from)
375 }
376 }
377
378 impl From<Episode> for EpisodeCleanerModel {
379 fn from(e: Episode) -> EpisodeCleanerModel {
380 EpisodeCleanerModel {
381 id: e.id(),
382 local_uri: e.local_uri,
383 played: e.played,
384 }
385 }
386 }
387
388 impl EpisodeCleanerModel {
389 /// Get the value of the sqlite's `ROW_ID`
390 pub fn id(&self) -> EpisodeId {
391 self.id
392 }
393
394 /// Get the value of the `local_uri`.
395 ///
396 /// Represents the local uri,usually filesystem path,
397 /// that the media file will be located at.
398 pub fn local_uri(&self) -> Option<&str> {
399 self.local_uri.as_deref()
400 }
401
402 /// Set the `local_uri`.
403 pub fn set_local_uri(&mut self, value: Option<&str>) {
404 self.local_uri = value.map(|x| x.to_string());
405 }
406
407 /// Epoch representation of the last time the episode was played.
408 ///
409 /// None/Null for unplayed.
410 pub fn played(&self) -> Option<NaiveDateTime> {
411 self.played
412 }
413
414 /// Set the `played` value.
415 pub fn set_played(&mut self, value: Option<NaiveDateTime>) {
416 self.played = value;
417 }
418 }
419
420 #[derive(Queryable, AsChangeset, PartialEq, Selectable)]
421 #[diesel(table_name = episodes)]
422 #[diesel(treat_none_as_null = true)]
423 #[diesel(primary_key(title, show_id))]
424 #[derive(Debug, Clone)]
425 /// Diesel Model to be used for FIXME.
426 pub struct EpisodeMinimal {
427 id: EpisodeId,
428 title: String,
429 uri: Option<String>,
430 image_uri: Option<String>,
431 epoch: NaiveDateTime,
432 length: Option<i32>,
433 duration: Option<i32>,
434 play_position: i32,
435 guid: Option<String>,
436 show_id: ShowId,
437 }
438
439 impl From<Episode> for EpisodeMinimal {
440 fn from(e: Episode) -> Self {
441 EpisodeMinimal {
442 id: e.id,
443 title: e.title,
444 uri: e.uri,
445 image_uri: e.image_uri,
446 length: e.length,
447 guid: e.guid,
448 epoch: e.epoch,
449 duration: e.duration,
450 play_position: e.play_position,
451 show_id: e.show_id,
452 }
453 }
454 }
455
456 impl EpisodeMinimal {
457 /// Get the value of the sqlite's `ROW_ID`
458 pub fn id(&self) -> EpisodeId {
459 self.id
460 }
461
462 /// Get the value of the `title` field.
463 pub fn title(&self) -> &str {
464 &self.title
465 }
466
467 /// Get the value of the `uri`.
468 ///
469 /// Represents the url(usually) that the media file will be located at.
470 pub fn uri(&self) -> Option<&str> {
471 self.uri.as_deref()
472 }
473
474 /// A cover image for this specific episode.
475 pub fn image_uri(&self) -> Option<&str> {
476 self.image_uri.as_deref()
477 }
478
479 /// Get the Episode's `guid`.
480 pub fn guid(&self) -> Option<&str> {
481 self.guid.as_deref()
482 }
483
484 /// Get the `epoch` value.
485 ///
486 /// Retrieved from the rss Item publish date.
487 /// Value is set to Utc whenever possible.
488 pub fn epoch(&self) -> NaiveDateTime {
489 self.epoch
490 }
491
492 /// Get the `length`.
493 ///
494 /// The number represents the size of the file in bytes.
495 pub fn length(&self) -> Option<i32> {
496 self.length
497 }
498
499 /// Get the `duration` value.
500 ///
501 /// The number represents the duration of the item/episode in seconds.
502 pub fn duration(&self) -> Option<i32> {
503 self.duration
504 }
505
506 /// `Show` table foreign key.
507 pub fn show_id(&self) -> ShowId {
508 self.show_id
509 }
510 }