diff options
author | Santo Cariotti <santo@dcariotti.me> | 2025-01-17 20:41:20 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2025-01-17 20:41:20 +0100 |
commit | a56ecb83c70e57144a4b01153aab21656c97e366 (patch) | |
tree | 6951efbfa4e89969452b097acb7531d6162de6a6 /src/graphql | |
parent | c6dcfcfe833c28fec288918965fc31cd86199fb1 (diff) |
Sound as binary string
Diffstat (limited to 'src/graphql')
-rw-r--r-- | src/graphql/query.rs | 2 | ||||
-rw-r--r-- | src/graphql/types/alert.rs | 98 | ||||
-rw-r--r-- | src/graphql/types/notification.rs | 12 |
3 files changed, 69 insertions, 43 deletions
diff --git a/src/graphql/query.rs b/src/graphql/query.rs index 63ca49c..ccade65 100644 --- a/src/graphql/query.rs +++ b/src/graphql/query.rs @@ -95,7 +95,7 @@ impl Query { /// -H 'content-type: application/json' /// -d '{"query":"{notifications { /// id, - /// alert { id, userId, createdAt, area, areaLevel2, areaLevel3, text1, text2, text3, reachedUsers }, + /// alert { id, userId, createdAt, area, areaLevel2, areaLevel3, text1, text2, text3, audio1, audio2, audio3, reachedUsers }, /// userId, latitude, longitude, movingActivity, level, seen, createdAt /// }}"}' /// ``` diff --git a/src/graphql/types/alert.rs b/src/graphql/types/alert.rs index 9b71188..1156f3d 100644 --- a/src/graphql/types/alert.rs +++ b/src/graphql/types/alert.rs @@ -29,6 +29,9 @@ pub struct Alert { pub text1: String, pub text2: String, pub text3: String, + pub audio1: Vec<u8>, + pub audio2: Vec<u8>, + pub audio3: Vec<u8>, pub reached_users: i32, } @@ -82,6 +85,9 @@ pub mod query { text1, text2, text3, + audio1, + audio2, + audio3, reached_users FROM alerts WHERE id = $1", @@ -101,6 +107,9 @@ pub mod query { text1, text2, text3, + audio1, + audio2, + audio3, reached_users FROM alerts ORDER BY id DESC @@ -124,6 +133,9 @@ pub mod query { text1: row.get("text1"), text2: row.get("text2"), text3: row.get("text3"), + audio1: row.get("audio1"), + audio2: row.get("audio2"), + audio3: row.get("audio3"), reached_users: row.get("reached_users"), }) .collect(); @@ -190,6 +202,7 @@ pub mod mutations { ST_AsText(ST_Buffer(area::geography, 1000)) as area_level2, ST_AsText(ST_Buffer(area::geography, 2000)) as area_level3, text1, text2, text3, + audio1, audio2, audio3, reached_users FROM alerts WHERE area = {} AND created_at >= NOW() - INTERVAL '10 MINUTE'", polygon @@ -207,6 +220,9 @@ pub mod mutations { text1: row.get("text1"), text2: row.get("text2"), text3: row.get("text3"), + audio1: row.get("audio1"), + audio2: row.get("audio2"), + audio3: row.get("audio3"), reached_users: row.get("reached_users"), }) .collect::<Vec<Alert>>() @@ -215,15 +231,40 @@ pub mod mutations { return Ok(previous_alert); } + let audio1 = match audio::tts(&input.text1).await { + Ok(content) => content, + Err(e) => { + tracing::error!("Error for `{}`: {}", &input.text1, e); + bytes::Bytes::new() + } + }; + + let audio2 = match audio::tts(&input.text2).await { + Ok(content) => content, + Err(e) => { + tracing::error!("Error for `{}`: {}", &input.text2, e); + bytes::Bytes::new() + } + }; + + let audio3 = match audio::tts(&input.text3).await { + Ok(content) => content, + Err(e) => { + tracing::error!("Error for `{}`: {}", &input.text3, e); + bytes::Bytes::new() + } + }; + let insert_query = format!( - "INSERT INTO alerts (user_id, area, text1, text2, text3) - VALUES($1, {}, $2, $3, $4) + "INSERT INTO alerts (user_id, area, text1, text2, text3, audio1, audio2, audio3) + VALUES($1, {}, $2, $3, $4, $5, $6, $7) RETURNING id, user_id, extract(epoch from created_at)::double precision as created_at, ST_AsText(area) as area, ST_AsText(ST_Buffer(area::geography, 1000)) as area_level2, ST_AsText(ST_Buffer(area::geography, 2000)) as area_level3, text1, text2, text3, + audio1, audio2, audio3, reached_users", polygon ); @@ -231,7 +272,15 @@ pub mod mutations { let rows = client .query( &insert_query, - &[&claims.user_id, &input.text1, &input.text2, &input.text3], + &[ + &claims.user_id, + &input.text1, + &input.text2, + &input.text3, + &audio1.to_vec(), + &audio2.to_vec(), + &audio3.to_vec(), + ], ) .await?; let mut alert = rows @@ -246,6 +295,9 @@ pub mod mutations { text1: row.get("text1"), text2: row.get("text2"), text3: row.get("text3"), + audio1: row.get("audio1"), + audio2: row.get("audio2"), + audio3: row.get("audio3"), reached_users: row.get("reached_users"), }) .collect::<Vec<Alert>>() @@ -311,7 +363,7 @@ pub mod mutations { let notification = Notification::insert_db( client, alert.id, - &p, + p, LevelAlert::from_str(level.text).unwrap(), ) .await?; @@ -363,44 +415,6 @@ pub mod mutations { ) .await?; - if let Err(e) = audio::tts( - alert.text1.clone(), - format!("alert-{}-text-1.mp3", alert.id), - ) - .await - { - tracing::error!( - "Error for `{}`: {}", - format!("alert-{}-text-1.mp3", alert.id), - e - ); - } - - if let Err(e) = audio::tts( - alert.text2.clone(), - format!("alert-{}-text-2.mp3", alert.id), - ) - .await - { - tracing::error!( - "Error for `{}`: {}", - format!("alert-{}-text-2.mp3", alert.id), - e - ); - } - if let Err(e) = audio::tts( - alert.text3.clone(), - format!("alert-{}-text-3.mp3", alert.id), - ) - .await - { - tracing::error!( - "Error for `{}`: {}", - format!("alert-{}-text-3.mp3", alert.id), - e - ); - } - Ok(alert) } } diff --git a/src/graphql/types/notification.rs b/src/graphql/types/notification.rs index 21fc1aa..87d478a 100644 --- a/src/graphql/types/notification.rs +++ b/src/graphql/types/notification.rs @@ -186,6 +186,9 @@ pub mod query { a.text1 as alert_text1, a.text2 as alert_text2, a.text3 as alert_text3, + a.audio1 as alert_audio1, + a.audio2 as alert_audio2, + a.audio3 as alert_audio3, a.reached_users as alert_reached_users FROM notifications n JOIN alerts a ON n.alert_id = a.id".to_string(); @@ -242,6 +245,9 @@ pub mod query { text1: row.get("alert_text1"), text2: row.get("alert_text2"), text3: row.get("alert_text3"), + audio1: row.get("alert_audio1"), + audio2: row.get("alert_audio2"), + audio3: row.get("alert_audio3"), reached_users: row.get("alert_reached_users"), }, seen: row.get("seen"), @@ -296,6 +302,9 @@ pub mod mutations { a.text1 as alert_text1, a.text2 as alert_text2, a.text3 as alert_text3, + a.audio1 as alert_audio1, + a.audio2 as alert_audio2, + a.audio3 as alert_audio3, a.reached_users as alert_reached_users FROM notifications n JOIN alerts a ON n.alert_id = a.id @@ -316,6 +325,9 @@ pub mod mutations { text1: row.get("alert_text1"), text2: row.get("alert_text2"), text3: row.get("alert_text3"), + audio1: row.get("alert_audio1"), + audio2: row.get("alert_audio2"), + audio3: row.get("alert_audio3"), reached_users: row.get("alert_reached_users"), }, seen: row.get("seen"), |