diff --git a/src/helpers/independent_push_notifications_service_helper.rs b/src/helpers/independent_push_notifications_service_helper.rs index 206160e..c4cfc71 100644 --- a/src/helpers/independent_push_notifications_service_helper.rs +++ b/src/helpers/independent_push_notifications_service_helper.rs @@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize}; use crate::data::config::{conf, IndependentPushService}; use crate::data::error::{ExecError, Res}; +use crate::data::push_notification::PushNotification; #[derive(Deserialize)] struct CreateTokenResult { @@ -21,6 +22,23 @@ struct RemoveTokenRequest { client: String, } +#[derive(Serialize)] +struct PushNotificationRequest { + clients: Vec, + id: String, + title: String, + body: String, + image: Option, + timeout: Option, +} + + +#[derive(Serialize)] +struct CancelNotificationRequest { + clients: Vec, + id: String, +} + fn svc_conf() -> Res<&'static IndependentPushService> { match &conf().independent_push_service { Some(c) => Ok(c), @@ -59,4 +77,44 @@ pub fn remove_token(t: &str) -> Res { Err(ExecError::boxed_string( format!("Failed to remove client, got a status {} for service!", client.status().as_u16()))) +} + +/// Push notifications +pub fn push_notifications(n: &PushNotification, targets: Vec) -> Res { + let client = create_client()? + .get(&format!("{}{}", svc_conf()?.control_url, "push_notification")) + .json(&PushNotificationRequest { + clients: targets, + id: n.id.clone(), + title: n.title.clone(), + body: n.body.clone(), + image: n.image.clone(), + timeout: n.timeout.clone(), + }) + .send()?; + + if client.status() == StatusCode::OK { + return Ok(()); + } + + Err(ExecError::boxed_string( + format!("Failed to push notification, got a status {} for service!", client.status().as_u16()))) +} + +/// Cancel notifications +pub fn cancel_notifications(id: String, targets: Vec) -> Res { + let client = create_client()? + .get(&format!("{}{}", svc_conf()?.control_url, "remove_notification")) + .json(&CancelNotificationRequest { + clients: targets, + id: id.clone(), + }) + .send()?; + + if client.status() == StatusCode::OK { + return Ok(()); + } + + Err(ExecError::boxed_string( + format!("Failed to remove notification, got a status {} for service!", client.status().as_u16()))) } \ No newline at end of file diff --git a/src/helpers/push_notifications_helper.rs b/src/helpers/push_notifications_helper.rs index bcd906e..e2911cf 100644 --- a/src/helpers/push_notifications_helper.rs +++ b/src/helpers/push_notifications_helper.rs @@ -25,18 +25,41 @@ pub fn un_register_from_previous_service(client: &UserAccessToken) -> Res { Ok(()) } +/// Split tokens in categories : (Independent, Firebase) +fn split_tokens(targets: Vec) -> (Vec) { + let mut independent = vec![]; + + for target in targets { + match target.push_notifications_token { + PushNotificationToken::INDEPENDENT(token) => { + independent.push(token) + } + _ => {} + } + } + + (independent) +} + /// Push a notification to specific tokens -fn push_notification(n: &PushNotification, targets: Vec) -> Res { - // TODO : implement - println!("* Push notification: {:#?} \n* To {:?}", n, targets); +fn push_notification(n: &PushNotification, targets: Vec) -> Res { + let independents = split_tokens(targets); + + // Push independent notifications + if !independents.is_empty() { + independent_push_notifications_service_helper::push_notifications(n, independents)?; + } Ok(()) } /// Cancel a notification for specific tokens (optional) -fn cancel_notification(id: String, targets: Vec) -> Res { - // TODO : implement - println!("* Cancel push notification: {:#?} \n* For {:?}", id, targets); +fn cancel_notification(id: String, targets: Vec) -> Res { + let independents = split_tokens(targets); + + if !independents.is_empty() { + independent_push_notifications_service_helper::cancel_notifications(id, independents)?; + } Ok(()) } @@ -51,7 +74,7 @@ fn push_notification_to_users(n: &PushNotification, users: Vec) -> Res { for user in dest_users { for token in account_helper::get_all_login_tokens(&user)? { - dest_tokens.push(token.push_notifications_token); + dest_tokens.push(token); } } @@ -64,7 +87,7 @@ fn cancel_notification_for_users(id: String, users: Vec) -> Res { for user in users { for token in account_helper::get_all_login_tokens(&user)? { - dest_tokens.push(token.push_notifications_token); + dest_tokens.push(token); } }