From 9b1e75b62aa0fb54c69cc7f2a19d27a2b195f41a Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Tue, 27 Aug 2024 11:19:17 +0200 Subject: [PATCH] create preset set on channel creation, only select/insert/update presets that belongs to channel --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- ffplayout/src/api/routes.rs | 20 ++++++++++++-------- ffplayout/src/db/handles.rs | 13 +++++++++++++ ffplayout/src/utils/channels.rs | 1 + frontend | 2 +- migrations/00001_create_tables.sql | 3 +-- 7 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 05799478..68018969 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1210,7 +1210,7 @@ checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" [[package]] name = "ffplayout" -version = "0.24.0-beta2" +version = "0.24.0-beta3" dependencies = [ "actix-files", "actix-multipart", @@ -3540,7 +3540,7 @@ dependencies = [ [[package]] name = "tests" -version = "0.24.0-beta2" +version = "0.24.0-beta3" dependencies = [ "actix-rt", "actix-test", diff --git a/Cargo.toml b/Cargo.toml index 0e53419f..8e3613cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ default-members = ["ffplayout", "tests"] resolver = "2" [workspace.package] -version = "0.24.0-beta2" +version = "0.24.0-beta3" license = "GPL-3.0" repository = "https://github.com/ffplayout/ffplayout" authors = ["Jonathan Baecker "] diff --git a/ffplayout/src/api/routes.rs b/ffplayout/src/api/routes.rs index 7dd3bd4b..45855876 100644 --- a/ffplayout/src/api/routes.rs +++ b/ffplayout/src/api/routes.rs @@ -670,7 +670,7 @@ async fn update_playout_config( /// **Get all Presets** /// /// ```BASH -/// curl -X GET http://127.0.0.1:8787/api/presets/ -H 'Content-Type: application/json' \ +/// curl -X GET http://127.0.0.1:8787/api/presets/1 -H 'Content-Type: application/json' \ /// -H 'Authorization: Bearer ' /// ``` #[get("/presets/{id}")] @@ -699,19 +699,21 @@ async fn get_presets( /// -d '{ "name": "", "text": "", "x": "", "y": "", "fontsize": 24, "line_spacing": 4, "fontcolor": "#ffffff", "box": 1, "boxcolor": "#000000", "boxborderw": 4, "alpha": 1.0, "channel_id": 1 }' \ /// -H 'Authorization: Bearer ' /// ``` -#[put("/presets/{id}")] +#[put("/presets/{channel}/{id}")] #[protect( any("Role::GlobalAdmin", "Role::ChannelAdmin", "Role::User"), ty = "Role", - expr = "user.channels.contains(&*id) || role.has_authority(&Role::GlobalAdmin)" + expr = "user.channels.contains(&path.0) || role.has_authority(&Role::GlobalAdmin)" )] async fn update_preset( pool: web::Data>, - id: web::Path, + path: web::Path<(i32, i32)>, data: web::Json, role: AuthDetails, user: web::ReqData, ) -> Result { + let (_, id) = path.into_inner(); + if handles::update_preset(&pool, &id, data.into_inner()) .await .is_ok() @@ -755,21 +757,23 @@ async fn add_preset( /// **Delete Preset** /// /// ```BASH -/// curl -X DELETE http://127.0.0.1:8787/api/presets/1 -H 'Content-Type: application/json' \ +/// curl -X DELETE http://127.0.0.1:8787/api/presets/1/1 -H 'Content-Type: application/json' \ /// -H 'Authorization: Bearer ' /// ``` -#[delete("/presets/{id}")] +#[delete("/presets/{channel}/{id}")] #[protect( any("Role::GlobalAdmin", "Role::ChannelAdmin", "Role::User"), ty = "Role", - expr = "user.channels.contains(&*id) || role.has_authority(&Role::GlobalAdmin)" + expr = "user.channels.contains(&path.0) || role.has_authority(&Role::GlobalAdmin)" )] async fn delete_preset( pool: web::Data>, - id: web::Path, + path: web::Path<(i32, i32)>, role: AuthDetails, user: web::ReqData, ) -> Result { + let (_, id) = path.into_inner(); + if handles::delete_preset(&pool, &id).await.is_ok() { return Ok("Delete preset Success"); } diff --git a/ffplayout/src/db/handles.rs b/ffplayout/src/db/handles.rs index 713c5f7f..238e7856 100644 --- a/ffplayout/src/db/handles.rs +++ b/ffplayout/src/db/handles.rs @@ -487,6 +487,19 @@ pub async fn insert_preset( .await } +pub async fn new_channel_presets( + conn: &Pool, + channel_id: i32, +) -> Result { + let query = "INSERT INTO presets (name, text, x, y, fontsize, line_spacing, fontcolor, box, boxcolor, boxborderw, alpha, channel_id) + VALUES ('Default', 'Wellcome to ffplayout messenger!', '(w-text_w)/2', '(h-text_h)/2', '24', '4', '#ffffff@0xff', '0', '#000000@0x80', '4', '1.0', $1), + ('Empty Text', '', '0', '0', '24', '4', '#000000', '0', '#000000', '0', '0', $1), + ('Bottom Text fade in', 'The upcoming event will be delayed by a few minutes.', '(w-text_w)/2', '(h-line_h)*0.9', '24', '4', '#ffffff', '1', '#000000@0x80', '4', 'ifnot(ld(1),st(1,t));if(lt(t,ld(1)+1),0,if(lt(t,ld(1)+2),(t-(ld(1)+1))/1,if(lt(t,ld(1)+8),1,if(lt(t,ld(1)+9),(1-(t-(ld(1)+8)))/1,0))))', $1), + ('Scrolling Text', 'We have a very important announcement to make.', 'ifnot(ld(1),st(1,t));if(lt(t,ld(1)+1),w+4,w-w/12*mod(t-ld(1),12*(w+tw)/w))', '(h-line_h)*0.9', '24', '4', '#ffffff', '1', '#000000@0x80', '4', '1.0', $1);"; + + sqlx::query(query).bind(channel_id).execute(conn).await +} + pub async fn delete_preset( conn: &Pool, id: &i32, diff --git a/ffplayout/src/utils/channels.rs b/ffplayout/src/utils/channels.rs index 696e047f..ac48a924 100644 --- a/ffplayout/src/utils/channels.rs +++ b/ffplayout/src/utils/channels.rs @@ -63,6 +63,7 @@ pub async fn create_channel( ) -> Result { let global = handles::select_global(conn).await?; let mut channel = handles::insert_channel(conn, target_channel).await?; + handles::new_channel_presets(conn, channel.id).await?; channel.preview_url = preview_url(&channel.preview_url, channel.id); diff --git a/frontend b/frontend index 0b1e083c..8d8ab58e 160000 --- a/frontend +++ b/frontend @@ -1 +1 @@ -Subproject commit 0b1e083ce5b1818589b899d2fe0cc04f4100df32 +Subproject commit 8d8ab58ef43d450968a3e4fa23f1141c77a31b88 diff --git a/migrations/00001_create_tables.sql b/migrations/00001_create_tables.sql index 25f3a83b..72f64562 100644 --- a/migrations/00001_create_tables.sql +++ b/migrations/00001_create_tables.sql @@ -49,8 +49,7 @@ CREATE TABLE boxborderw TEXT NOT NULL, alpha TEXT NOT NULL, channel_id INTEGER NOT NULL DEFAULT 1, - FOREIGN KEY (channel_id) REFERENCES channels (id) ON UPDATE CASCADE ON DELETE CASCADE, - UNIQUE (name) + FOREIGN KEY (channel_id) REFERENCES channels (id) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE