| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
use chrono::{Duration, NaiveDateTime, Utc}; |
| 21 |
use diesel::query_dsl::filter_dsl::FilterDsl; |
| 22 |
use diesel::{ExpressionMethods, RunQueryDsl}; |
| 23 |
|
| 24 |
use crate::database::connection; |
| 25 |
use crate::errors::DataError; |
| 26 |
use crate::make_id_wrapper; |
| 27 |
use crate::models::{Source, SourceId}; |
| 28 |
use crate::schema::shows; |
| 29 |
use crate::utils::{calculate_hash, u64_to_vec_u8, vec_u8_to_u64}; |
| 30 |
|
| 31 |
make_id_wrapper!(ShowId); |
| 32 |
|
| 33 |
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq, Selectable)] |
| 34 |
#[diesel(belongs_to(Source, foreign_key = source_id))] |
| 35 |
#[diesel(treat_none_as_null = true)] |
| 36 |
#[diesel(table_name = shows)] |
| 37 |
#[derive(Debug, Clone)] |
| 38 |
|
| 39 |
pub struct Show { |
| 40 |
id: ShowId, |
| 41 |
title: String, |
| 42 |
link: String, |
| 43 |
description: String, |
| 44 |
image_uri: Option<String>, |
| 45 |
image_uri_hash: Option<Vec<u8>>, |
| 46 |
image_cached: NaiveDateTime, |
| 47 |
source_id: SourceId, |
| 48 |
} |
| 49 |
|
| 50 |
impl Show { |
| 51 |
|
| 52 |
pub fn id(&self) -> ShowId { |
| 53 |
self.id |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
pub fn title(&self) -> &str { |
| 58 |
&self.title |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
pub fn link(&self) -> &str { |
| 65 |
&self.link |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
pub fn description(&self) -> &str { |
| 70 |
&self.description |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
pub fn image_uri(&self) -> Option<&str> { |
| 77 |
self.image_uri.as_deref() |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
pub fn image_uri_hash(&self) -> Option<u64> { |
| 82 |
if let Some(b) = &self.image_uri_hash { |
| 83 |
return Some(vec_u8_to_u64(b.clone())); |
| 84 |
} |
| 85 |
None |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
pub fn image_cached(&self) -> &NaiveDateTime { |
| 90 |
&self.image_cached |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
pub fn source_id(&self) -> SourceId { |
| 95 |
self.source_id |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
#[derive(Queryable, Debug, Clone, Selectable)] |
| 100 |
#[diesel(treat_none_as_null = true)] |
| 101 |
#[diesel(table_name = shows)] |
| 102 |
|
| 103 |
|
| 104 |
pub struct ShowCoverModel { |
| 105 |
id: ShowId, |
| 106 |
title: String, |
| 107 |
image_uri: Option<String>, |
| 108 |
image_uri_hash: Option<Vec<u8>>, |
| 109 |
image_cached: NaiveDateTime, |
| 110 |
} |
| 111 |
|
| 112 |
impl From<Show> for ShowCoverModel { |
| 113 |
fn from(p: Show) -> ShowCoverModel { |
| 114 |
ShowCoverModel { |
| 115 |
id: p.id, |
| 116 |
title: p.title, |
| 117 |
image_uri: p.image_uri, |
| 118 |
image_uri_hash: p.image_uri_hash, |
| 119 |
image_cached: p.image_cached, |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
impl ShowCoverModel { |
| 125 |
|
| 126 |
pub fn id(&self) -> ShowId { |
| 127 |
self.id |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
pub fn title(&self) -> &str { |
| 132 |
&self.title |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
pub fn image_uri(&self) -> Option<&str> { |
| 139 |
self.image_uri.as_deref() |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
pub fn image_uri_hash(&self) -> Option<u64> { |
| 144 |
if let Some(b) = &self.image_uri_hash { |
| 145 |
return Some(vec_u8_to_u64(b.clone())); |
| 146 |
} |
| 147 |
None |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
pub fn image_cached(&self) -> &NaiveDateTime { |
| 152 |
&self.image_cached |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
pub fn is_cached_image_valid(&self, valid: &Duration) -> bool { |
| 160 |
if Utc::now() |
| 161 |
.naive_utc() |
| 162 |
.signed_duration_since(*self.image_cached()) |
| 163 |
> *valid |
| 164 |
{ |
| 165 |
return false; |
| 166 |
} |
| 167 |
if let Some(new) = &self.image_uri() { |
| 168 |
if let Some(orig) = self.image_uri_hash() { |
| 169 |
return calculate_hash(new) == orig; |
| 170 |
} |
| 171 |
} |
| 172 |
false |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
pub(crate) fn update_image_cached(&self) -> Result<(), DataError> { |
| 177 |
use crate::schema::shows::dsl::*; |
| 178 |
let db = connection(); |
| 179 |
let mut con = db.get()?; |
| 180 |
diesel::update(shows.filter(id.eq(self.id))) |
| 181 |
.set(image_cached.eq(Utc::now().naive_utc())) |
| 182 |
.execute(&mut con) |
| 183 |
.map(|_| ()) |
| 184 |
.map_err(From::from) |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
fn update_image_uri_hash(&self) -> Result<(), DataError> { |
| 189 |
use crate::schema::shows::dsl::*; |
| 190 |
let db = connection(); |
| 191 |
let mut con = db.get()?; |
| 192 |
|
| 193 |
let mut hash: Option<Vec<u8>> = None; |
| 194 |
if let Some(i) = &self.image_uri { |
| 195 |
hash = Some(u64_to_vec_u8(calculate_hash(i))); |
| 196 |
} |
| 197 |
|
| 198 |
diesel::update(shows.filter(id.eq(self.id))) |
| 199 |
.set(image_uri_hash.eq(&hash)) |
| 200 |
.execute(&mut con) |
| 201 |
.map(|_| ()) |
| 202 |
.map_err(From::from) |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
pub fn update_image_cache_values(&self) -> Result<(), DataError> { |
| 207 |
match self.image_uri_hash() { |
| 208 |
None => self.update_image_uri_hash()?, |
| 209 |
Some(hash) => match self.image_uri() { |
| 210 |
None => self.update_image_uri_hash()?, |
| 211 |
Some(image_uri) => { |
| 212 |
if calculate_hash(&image_uri) != hash { |
| 213 |
self.update_image_uri_hash()?; |
| 214 |
} |
| 215 |
} |
| 216 |
}, |
| 217 |
} |
| 218 |
self.update_image_cached() |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
#[cfg(test)] |
| 223 |
mod tests { |
| 224 |
use super::*; |
| 225 |
use crate::database::truncate_db; |
| 226 |
use crate::dbqueries; |
| 227 |
use crate::models::{Insert, NewShow, NewShowBuilder, Update}; |
| 228 |
use anyhow::Result; |
| 229 |
use std::sync::LazyLock; |
| 230 |
use std::{thread, time}; |
| 231 |
|
| 232 |
const TEST_SOURCE_ID: SourceId = SourceId(42); |
| 233 |
const TEST_SHOW_ID: ShowId = ShowId(0); |
| 234 |
|
| 235 |
static EXPECTED_INTERCEPTED: LazyLock<NewShow> = LazyLock::new(|| { |
| 236 |
let descr = "The people behind The Intercept’s fearless reporting and incisive \ |
| 237 |
commentary—Jeremy Scahill, Glenn Greenwald, Betsy Reed and \ |
| 238 |
others—discuss the crucial issues of our time: national security, civil \ |
| 239 |
liberties, foreign policy, and criminal justice. Plus interviews with \ |
| 240 |
artists, thinkers, and newsmakers who challenge our preconceptions about \ |
| 241 |
the world we live in."; |
| 242 |
let image_uri = "http://static.megaphone.fm/podcasts/d5735a50-d904-11e6-8532-73c7de466ea6/image/\ |
| 243 |
uploads_2F1484252190700-qhn5krasklbce3dh-a797539282700ea0298a3a26f7e49b0b_\ |
| 244 |
2FIntercepted_COVER%2B_281_29.png"; |
| 245 |
|
| 246 |
NewShowBuilder::default() |
| 247 |
.title("Intercepted with Jeremy Scahill") |
| 248 |
.link("https://theintercept.com/podcasts") |
| 249 |
.description(descr) |
| 250 |
.image_uri(String::from(image_uri)) |
| 251 |
.image_uri_hash(Some(vec![164, 62, 7, 221, 215, 202, 38, 41])) |
| 252 |
.image_cached(Utc::now().naive_utc()) |
| 253 |
.source_id(TEST_SOURCE_ID) |
| 254 |
.build() |
| 255 |
.unwrap() |
| 256 |
}); |
| 257 |
static UPDATED_IMAGE_URI_INTERCEPTED: LazyLock<NewShow> = LazyLock::new(|| { |
| 258 |
let image_uri = "https://assets.fireside.fm/file/fireside-images/podcasts/images/f/f31a453c-fa15-491f-8618-3f71f1d565e5/cover.jpg?v=3"; |
| 259 |
|
| 260 |
NewShowBuilder::default() |
| 261 |
.title("Intercepted with Jeremy Scahill") |
| 262 |
.link("https://theintercept.com/podcasts") |
| 263 |
.description(EXPECTED_INTERCEPTED.description()) |
| 264 |
.image_uri(String::from(image_uri)) |
| 265 |
.image_uri_hash(Some(vec![164, 62, 7, 221, 215, 202, 38, 41])) |
| 266 |
.image_cached(EXPECTED_INTERCEPTED.image_cached().unwrap()) |
| 267 |
.source_id(TEST_SOURCE_ID) |
| 268 |
.build() |
| 269 |
.unwrap() |
| 270 |
}); |
| 271 |
|
| 272 |
#[test] |
| 273 |
fn should_update_timestamp_when_update_image_cached_is_called_after_the_timestamp_has_expired() |
| 274 |
-> Result<()> { |
| 275 |
truncate_db()?; |
| 276 |
EXPECTED_INTERCEPTED.insert()?; |
| 277 |
let show = EXPECTED_INTERCEPTED.to_podcast()?; |
| 278 |
let show: ShowCoverModel = show.into(); |
| 279 |
let original_timestamp = show.image_cached(); |
| 280 |
show.update_image_cached().unwrap(); |
| 281 |
let show = dbqueries::get_podcast_from_id(show.id())?; |
| 282 |
let updated_timestamp = show.image_cached(); |
| 283 |
assert!(original_timestamp < updated_timestamp); |
| 284 |
|
| 285 |
|
| 286 |
assert_eq!( |
| 287 |
show.image_uri().unwrap(), |
| 288 |
"http://static.megaphone.fm/podcasts/d5735a50-d904-11e6-8532-73c7de466ea6/image/\ |
| 289 |
uploads_2F1484252190700-qhn5krasklbce3dh-a797539282700ea0298a3a26f7e49b0b_\ |
| 290 |
2FIntercepted_COVER%2B_281_29.png" |
| 291 |
); |
| 292 |
assert_eq!(show.image_uri_hash().unwrap(), 2965280433145069220); |
| 293 |
Ok(()) |
| 294 |
} |
| 295 |
|
| 296 |
#[test] |
| 297 |
fn should_update_hash_when_update_image_uri_hash_is_called_when_the_hash_is_invalid() |
| 298 |
-> Result<()> { |
| 299 |
truncate_db()?; |
| 300 |
EXPECTED_INTERCEPTED.insert()?; |
| 301 |
let original = EXPECTED_INTERCEPTED.to_podcast()?; |
| 302 |
let original_hash: u64 = 2965280433145069220; |
| 303 |
let updated = &*UPDATED_IMAGE_URI_INTERCEPTED; |
| 304 |
updated.update(original.id())?; |
| 305 |
let show = dbqueries::get_podcast_cover_from_id(original.id())?; |
| 306 |
|
| 307 |
let not_yet_updated_hash = updated.image_uri_hash().unwrap(); |
| 308 |
assert_eq!(not_yet_updated_hash, original_hash); |
| 309 |
|
| 310 |
show.update_image_uri_hash().unwrap(); |
| 311 |
let show = dbqueries::get_podcast_from_id(original.id())?; |
| 312 |
let updated_hash = show.image_uri_hash().unwrap(); |
| 313 |
let expected_updated_hash: u64 = 1748982167920802687; |
| 314 |
assert_eq!(updated_hash, expected_updated_hash); |
| 315 |
assert_eq!( |
| 316 |
show.image_uri().unwrap(), |
| 317 |
"https://assets.fireside.fm/file/fireside-images/podcasts/images/f/f31a453c-fa15-491f-8618-3f71f1d565e5/cover.jpg?v=3" |
| 318 |
); |
| 319 |
Ok(()) |
| 320 |
} |
| 321 |
|
| 322 |
#[test] |
| 323 |
fn should_update_timestamp_only_when_update_image_cached_values_is_called_after_the_timestamp_has_expired() |
| 324 |
-> Result<()> { |
| 325 |
truncate_db()?; |
| 326 |
EXPECTED_INTERCEPTED.insert()?; |
| 327 |
let show = EXPECTED_INTERCEPTED.to_podcast()?; |
| 328 |
let show: ShowCoverModel = show.into(); |
| 329 |
let original_timestamp = show.image_cached(); |
| 330 |
show.update_image_cache_values().unwrap(); |
| 331 |
let show = dbqueries::get_podcast_from_id(show.id())?; |
| 332 |
let updated_timestamp = show.image_cached(); |
| 333 |
assert!(original_timestamp < updated_timestamp); |
| 334 |
assert_eq!( |
| 335 |
show.image_uri().unwrap(), |
| 336 |
"http://static.megaphone.fm/podcasts/d5735a50-d904-11e6-8532-73c7de466ea6/image/\ |
| 337 |
uploads_2F1484252190700-qhn5krasklbce3dh-a797539282700ea0298a3a26f7e49b0b_\ |
| 338 |
2FIntercepted_COVER%2B_281_29.png" |
| 339 |
); |
| 340 |
assert_eq!(show.image_uri_hash().unwrap(), 2965280433145069220); |
| 341 |
Ok(()) |
| 342 |
} |
| 343 |
|
| 344 |
#[test] |
| 345 |
fn should_update_timestamp_and_hash_when_update_image_cached_values_is_called_when_hash_is_invalid() |
| 346 |
-> Result<()> { |
| 347 |
truncate_db()?; |
| 348 |
EXPECTED_INTERCEPTED.insert()?; |
| 349 |
let original = EXPECTED_INTERCEPTED.to_podcast()?; |
| 350 |
let original_timestamp = original.image_cached(); |
| 351 |
let updated = &*UPDATED_IMAGE_URI_INTERCEPTED; |
| 352 |
updated.update(original.id())?; |
| 353 |
let show = dbqueries::get_podcast_cover_from_id(original.id())?; |
| 354 |
|
| 355 |
let not_yet_updated_hash = show.image_uri_hash().unwrap(); |
| 356 |
let original_hash: u64 = 2965280433145069220; |
| 357 |
assert_eq!(not_yet_updated_hash, original_hash); |
| 358 |
|
| 359 |
show.update_image_cache_values().unwrap(); |
| 360 |
let show = dbqueries::get_podcast_from_id(show.id())?; |
| 361 |
let updated_timestamp = show.image_cached(); |
| 362 |
assert!(original_timestamp < updated_timestamp); |
| 363 |
|
| 364 |
let updated_hash = show.image_uri_hash().unwrap(); |
| 365 |
let expected_updated_hash: u64 = 1748982167920802687; |
| 366 |
assert_eq!(updated_hash, expected_updated_hash); |
| 367 |
|
| 368 |
assert_eq!( |
| 369 |
show.image_uri().unwrap(), |
| 370 |
"https://assets.fireside.fm/file/fireside-images/podcasts/images/f/f31a453c-fa15-491f-8618-3f71f1d565e5/cover.jpg?v=3" |
| 371 |
); |
| 372 |
Ok(()) |
| 373 |
} |
| 374 |
|
| 375 |
#[test] |
| 376 |
fn cached_image_should_be_valid_when_uri_and_hash_are_unchanged() -> Result<()> { |
| 377 |
let image_uri = String::from( |
| 378 |
"http://www.jupiterbroadcasting.com/wp-content/uploads/2018/01/lup-0232-v.jpg", |
| 379 |
); |
| 380 |
let hash = vec![191, 166, 24, 137, 178, 75, 5, 227]; |
| 381 |
let cover = ShowCoverModel { |
| 382 |
id: TEST_SHOW_ID, |
| 383 |
title: String::from("Linux Unplugged"), |
| 384 |
image_uri: Some(image_uri), |
| 385 |
image_uri_hash: Some(hash), |
| 386 |
image_cached: Utc::now().naive_utc(), |
| 387 |
}; |
| 388 |
let valid = Duration::weeks(4); |
| 389 |
assert!(cover.is_cached_image_valid(&valid)); |
| 390 |
Ok(()) |
| 391 |
} |
| 392 |
|
| 393 |
#[test] |
| 394 |
fn a_different_uri_should_invalidate_cached_image() -> Result<()> { |
| 395 |
|
| 396 |
|
| 397 |
let new_image_uri = String::from( |
| 398 |
"https://assets.fireside.fm/file/fireside-images/podcasts/images/f/f31a453c-fa15-491f-8618-3f71f1d565e5/cover.jpg?v=3", |
| 399 |
); |
| 400 |
let hash = vec![191, 166, 24, 137, 178, 75, 5, 227]; |
| 401 |
let cover = ShowCoverModel { |
| 402 |
id: TEST_SHOW_ID, |
| 403 |
title: String::from("Linux Unplugged"), |
| 404 |
image_uri: Some(new_image_uri), |
| 405 |
image_uri_hash: Some(hash), |
| 406 |
image_cached: Utc::now().naive_utc(), |
| 407 |
}; |
| 408 |
let valid = Duration::weeks(4); |
| 409 |
assert!(!cover.is_cached_image_valid(&valid)); |
| 410 |
Ok(()) |
| 411 |
} |
| 412 |
|
| 413 |
#[test] |
| 414 |
fn cached_image_should_be_invalidated_after_valid_duration() -> Result<()> { |
| 415 |
let image_uri = String::from( |
| 416 |
"http://www.jupiterbroadcasting.com/wp-content/uploads/2018/01/lup-0232-v.jpg", |
| 417 |
); |
| 418 |
let hash = vec![191, 166, 24, 137, 178, 75, 5, 227]; |
| 419 |
let cover = ShowCoverModel { |
| 420 |
id: TEST_SHOW_ID, |
| 421 |
title: String::from("Linux Unplugged"), |
| 422 |
image_uri: Some(image_uri), |
| 423 |
image_uri_hash: Some(hash), |
| 424 |
image_cached: Utc::now().naive_utc(), |
| 425 |
}; |
| 426 |
let valid = Duration::nanoseconds(1); |
| 427 |
thread::sleep(time::Duration::from_nanos(2)); |
| 428 |
assert!(!cover.is_cached_image_valid(&valid)); |
| 429 |
Ok(()) |
| 430 |
} |
| 431 |
} |