diff options
author | Santo Cariotti <santo@dcariotti.me> | 2024-09-05 14:21:32 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2024-09-05 14:21:32 +0200 |
commit | f56b18fd28054aa5b241a897d22bcfc58c321286 (patch) | |
tree | 10908e8ada3641be175f1af9857c3be6b7489f7c /src/graphql | |
parent | b592d19f1a74a32df0aaed7dbf7484d0ef5ad35a (diff) |
Send notifications by Expo API
Diffstat (limited to 'src/graphql')
-rw-r--r-- | src/graphql/mutation.rs | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/src/graphql/mutation.rs b/src/graphql/mutation.rs index 4387b77..dfffad8 100644 --- a/src/graphql/mutation.rs +++ b/src/graphql/mutation.rs @@ -1,4 +1,5 @@ use crate::{ + expo, graphql::types::{ alert, jwt::{self, Authentication}, @@ -282,8 +283,8 @@ impl Mutation { .collect(); let mut notification_ids = vec![]; - for id in position_ids { - let notification = notification::Notification::new(client, alert.id, id) + for id in &position_ids { + let notification = notification::Notification::new(client, alert.id, *id) .await .unwrap(); notification_ids.push(notification); @@ -298,6 +299,37 @@ impl Mutation { .await .unwrap(); + let placeholders: Vec<String> = (1..=position_ids.len()) + .map(|i| format!("${}", i)) + .collect(); + let query = format!( + "SELECT u.notification_token FROM positions p JOIN users u ON u.id = p.user_id + WHERE p.id IN ({}) AND notification_token IS NOT NULL", + placeholders.join(", ") + ); + + let tokens: Vec<String> = client + .query( + &query, + &position_ids + .iter() + .map(|id| id as &(dyn tokio_postgres::types::ToSql + Sync)) + .collect::<Vec<&(dyn tokio_postgres::types::ToSql + Sync)>>(), + ) + .await + .unwrap() + .iter() + .map(|row| format!("ExponentPushToken[{}]", row.get::<usize, String>(0))) + .collect(); + + expo::send( + tokens, + "New Alert!".to_string(), + "Keep an eye open".to_string(), + ) + .await + .unwrap(); + Ok(alert) } } |