From 26d8aed5540677d539957dea16f95491499eff58 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Fri, 29 Nov 2024 12:36:59 +0100 Subject: Use Expo on the AppState This because since Rust 1.83 it is suggested to not use the static reference cloning --- src/expo.rs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'src/expo.rs') diff --git a/src/expo.rs b/src/expo.rs index 1a6a4e5..f4cecc6 100644 --- a/src/expo.rs +++ b/src/expo.rs @@ -1,31 +1,35 @@ -use expo_push_notification_client::{Expo, ExpoClientOptions, ExpoPushMessage, ValidationError}; +use expo_push_notification_client::{Expo, ExpoClientOptions, ExpoPushMessage}; -/// Connection to an Expo client -static mut EXPO_CONNECTION: Option = None; +use crate::errors::AppError; /// Setup a new Expo API -pub fn setup(access_token: String) { - unsafe { - EXPO_CONNECTION = Some(Expo::new(ExpoClientOptions { - access_token: Some(access_token), - })) - } +pub fn setup(access_token: String) -> Expo { + Expo::new(ExpoClientOptions { + access_token: Some(access_token), + }) } /// Send notifications using Expo -pub async fn send(tokens: Vec, body: String, title: String) -> Result<(), ValidationError> { - let expo = unsafe { - EXPO_CONNECTION - .clone() - .expect("You need to call `setup()` first") - }; - +pub async fn send( + expo: Expo, + tokens: Vec, + body: String, + title: String, +) -> Result<(), AppError> { let expo_push_message = ExpoPushMessage::builder(tokens) .body(body) .title(title) .build()?; - let _ = expo.send_push_notifications(expo_push_message).await; + if expo + .send_push_notifications(expo_push_message) + .await + .is_err() + { + return Err(AppError::BadRequest( + "Expo Notifications sending".to_string(), + )); + } Ok(()) } -- cgit v1.2.3-18-g5258