Skip to content

Commit

Permalink
Forbid zero used as duration value
Browse files Browse the repository at this point in the history
  • Loading branch information
stagrim committed Nov 3, 2023
1 parent 0f64724 commit 6a24ac3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
6 changes: 5 additions & 1 deletion sasta/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,10 @@ async fn update_playlist(State(state): State<AppState>, Path(uuid): Path<Uuid>,
error!("[Api] Name is already used by Playlist {}", uuid);
return Err((StatusCode::BAD_REQUEST, Json((2, format!("Avoid using the name {} as it is already used by another Playlist", playlist.name)).into())))
}
if let Some(item) = playlist.items.iter().find(|&i| i.duration() == 0) {
error!("[Api] The Playlist item {} has a duration of 0", item.name());
return Err((StatusCode::BAD_REQUEST, Json((3, format!("Zero is not a valid duration for {}", item.name())).into())))
}
drop(read);

store.update_playlist(uuid, playlist.name, playlist.items).await;
Expand All @@ -546,7 +550,7 @@ async fn update_playlist(State(state): State<AppState>, Path(uuid): Path<Uuid>,
Ok(Json(update::Payload::Playlist(vec![(uuid, p.clone()).into()])))
} else {
error!("[Api] Could not find Playlist with {uuid} after update");
Err((StatusCode::INTERNAL_SERVER_ERROR, Json((3, format!("Could not find Playlist with {uuid} after update")).into())))
Err((StatusCode::INTERNAL_SERVER_ERROR, Json((4, format!("Could not find Playlist with {uuid} after update")).into())))
};
}

Expand Down
14 changes: 14 additions & 0 deletions sasta/src/store/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,20 @@ mod test {
assert_eq!(default_uuid, schedule.current_playlist(&Local.with_ymd_and_hms(2023, 4, 18, 14, 0, 1).unwrap()));
}

#[test]
fn test_invalid_date() {
let scheduled_uuid = Uuid::parse_str("8626f6e1-df7c-48d9-83c8-d7845b774ecd").unwrap();
let default_uuid = Uuid::parse_str("25cd63df-1f10-4c3f-afdb-58156ca47ebd").unwrap();
let playlist = vec![
ScheduledPlaylistInput {
playlist: scheduled_uuid,
start: "0 * 10 32 10 * *".to_string(),
end: "0 0 14 32 10 * *".to_string(),
}
];
assert!(Schedule::new("test".into(), playlist, default_uuid).is_err());
}

#[test]
fn test_current_playlist_smooth_transition() {
let scheduled_uuid = Uuid::parse_str("8626f6e1-df7c-48d9-83c8-d7845b774ecd").unwrap();
Expand Down
22 changes: 21 additions & 1 deletion sasta/src/store/store.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet};

use chrono::Local;
use redis::{aio::Connection, Client, AsyncCommands, JsonAsyncCommands};
use redis::{aio::Connection, Client, JsonAsyncCommands};
use serde::{Deserialize, Serialize};
use tokio::{sync::{broadcast::{self, Sender, Receiver}, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard, oneshot}, time::{sleep_until, Instant}};
use tracing::{warn, info, error, trace, warn_span, error_span};
Expand Down Expand Up @@ -37,6 +37,26 @@ pub enum PlaylistItem {
BackgroundAudio { name: String, settings: ImageData }
}

impl PlaylistItem {
pub fn duration(&self) -> u64 {
match self {
PlaylistItem::Website { settings, .. } => settings.duration,
PlaylistItem::Text { settings, .. } => settings.duration,
PlaylistItem::Image { settings, .. } => settings.duration,
PlaylistItem::BackgroundAudio { settings, .. } => settings.duration,
}
}

pub fn name(&self) -> &String {
match self {
PlaylistItem::Website { name, .. } => name,
PlaylistItem::Text { name, .. } => name,
PlaylistItem::Image { name, .. } => name,
PlaylistItem::BackgroundAudio { name, .. } => name,
}
}
}

#[derive(Deserialize, Serialize, Debug, Clone, ToSchema, TS)]
#[ts(export, export_to = "api_bindings/update/")]
pub struct WebsiteData {
Expand Down

0 comments on commit 6a24ac3

Please sign in to comment.