create preset set on channel creation, only select/insert/update presets that belongs to channel

This commit is contained in:
jb-alvarado 2024-08-27 11:19:17 +02:00
parent 537efc6d97
commit 9b1e75b62a
7 changed files with 31 additions and 14 deletions

4
Cargo.lock generated
View File

@ -1210,7 +1210,7 @@ checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a"
[[package]] [[package]]
name = "ffplayout" name = "ffplayout"
version = "0.24.0-beta2" version = "0.24.0-beta3"
dependencies = [ dependencies = [
"actix-files", "actix-files",
"actix-multipart", "actix-multipart",
@ -3540,7 +3540,7 @@ dependencies = [
[[package]] [[package]]
name = "tests" name = "tests"
version = "0.24.0-beta2" version = "0.24.0-beta3"
dependencies = [ dependencies = [
"actix-rt", "actix-rt",
"actix-test", "actix-test",

View File

@ -4,7 +4,7 @@ default-members = ["ffplayout", "tests"]
resolver = "2" resolver = "2"
[workspace.package] [workspace.package]
version = "0.24.0-beta2" version = "0.24.0-beta3"
license = "GPL-3.0" license = "GPL-3.0"
repository = "https://github.com/ffplayout/ffplayout" repository = "https://github.com/ffplayout/ffplayout"
authors = ["Jonathan Baecker <jonbae77@gmail.com>"] authors = ["Jonathan Baecker <jonbae77@gmail.com>"]

View File

@ -670,7 +670,7 @@ async fn update_playout_config(
/// **Get all Presets** /// **Get all Presets**
/// ///
/// ```BASH /// ```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 <TOKEN>' /// -H 'Authorization: Bearer <TOKEN>'
/// ``` /// ```
#[get("/presets/{id}")] #[get("/presets/{id}")]
@ -699,19 +699,21 @@ async fn get_presets(
/// -d '{ "name": "<PRESET NAME>", "text": "<TEXT>", "x": "<X>", "y": "<Y>", "fontsize": 24, "line_spacing": 4, "fontcolor": "#ffffff", "box": 1, "boxcolor": "#000000", "boxborderw": 4, "alpha": 1.0, "channel_id": 1 }' \ /// -d '{ "name": "<PRESET NAME>", "text": "<TEXT>", "x": "<X>", "y": "<Y>", "fontsize": 24, "line_spacing": 4, "fontcolor": "#ffffff", "box": 1, "boxcolor": "#000000", "boxborderw": 4, "alpha": 1.0, "channel_id": 1 }' \
/// -H 'Authorization: Bearer <TOKEN>' /// -H 'Authorization: Bearer <TOKEN>'
/// ``` /// ```
#[put("/presets/{id}")] #[put("/presets/{channel}/{id}")]
#[protect( #[protect(
any("Role::GlobalAdmin", "Role::ChannelAdmin", "Role::User"), any("Role::GlobalAdmin", "Role::ChannelAdmin", "Role::User"),
ty = "Role", 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( async fn update_preset(
pool: web::Data<Pool<Sqlite>>, pool: web::Data<Pool<Sqlite>>,
id: web::Path<i32>, path: web::Path<(i32, i32)>,
data: web::Json<TextPreset>, data: web::Json<TextPreset>,
role: AuthDetails<Role>, role: AuthDetails<Role>,
user: web::ReqData<UserMeta>, user: web::ReqData<UserMeta>,
) -> Result<impl Responder, ServiceError> { ) -> Result<impl Responder, ServiceError> {
let (_, id) = path.into_inner();
if handles::update_preset(&pool, &id, data.into_inner()) if handles::update_preset(&pool, &id, data.into_inner())
.await .await
.is_ok() .is_ok()
@ -755,21 +757,23 @@ async fn add_preset(
/// **Delete Preset** /// **Delete Preset**
/// ///
/// ```BASH /// ```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 <TOKEN>' /// -H 'Authorization: Bearer <TOKEN>'
/// ``` /// ```
#[delete("/presets/{id}")] #[delete("/presets/{channel}/{id}")]
#[protect( #[protect(
any("Role::GlobalAdmin", "Role::ChannelAdmin", "Role::User"), any("Role::GlobalAdmin", "Role::ChannelAdmin", "Role::User"),
ty = "Role", 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( async fn delete_preset(
pool: web::Data<Pool<Sqlite>>, pool: web::Data<Pool<Sqlite>>,
id: web::Path<i32>, path: web::Path<(i32, i32)>,
role: AuthDetails<Role>, role: AuthDetails<Role>,
user: web::ReqData<UserMeta>, user: web::ReqData<UserMeta>,
) -> Result<impl Responder, ServiceError> { ) -> Result<impl Responder, ServiceError> {
let (_, id) = path.into_inner();
if handles::delete_preset(&pool, &id).await.is_ok() { if handles::delete_preset(&pool, &id).await.is_ok() {
return Ok("Delete preset Success"); return Ok("Delete preset Success");
} }

View File

@ -487,6 +487,19 @@ pub async fn insert_preset(
.await .await
} }
pub async fn new_channel_presets(
conn: &Pool<Sqlite>,
channel_id: i32,
) -> Result<SqliteQueryResult, sqlx::Error> {
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( pub async fn delete_preset(
conn: &Pool<Sqlite>, conn: &Pool<Sqlite>,
id: &i32, id: &i32,

View File

@ -63,6 +63,7 @@ pub async fn create_channel(
) -> Result<Channel, ServiceError> { ) -> Result<Channel, ServiceError> {
let global = handles::select_global(conn).await?; let global = handles::select_global(conn).await?;
let mut channel = handles::insert_channel(conn, target_channel).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); channel.preview_url = preview_url(&channel.preview_url, channel.id);

@ -1 +1 @@
Subproject commit 0b1e083ce5b1818589b899d2fe0cc04f4100df32 Subproject commit 8d8ab58ef43d450968a3e4fa23f1141c77a31b88

View File

@ -49,8 +49,7 @@ CREATE TABLE
boxborderw TEXT NOT NULL, boxborderw TEXT NOT NULL,
alpha TEXT NOT NULL, alpha TEXT NOT NULL,
channel_id INTEGER NOT NULL DEFAULT 1, channel_id INTEGER NOT NULL DEFAULT 1,
FOREIGN KEY (channel_id) REFERENCES channels (id) ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY (channel_id) REFERENCES channels (id) ON UPDATE CASCADE ON DELETE CASCADE
UNIQUE (name)
); );
CREATE TABLE CREATE TABLE