mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-10-30 23:24:42 +00:00 
			
		
		
		
	Register indpendent service token
This commit is contained in:
		| @@ -78,7 +78,7 @@ impl ServerConfig { | ||||
|  | ||||
|             notifications: NotificationsConfig { | ||||
|                 has_firebase: c.is_firebase_available(), | ||||
|                 has_independent: conf().independent_push_service.is_some(), | ||||
|                 has_independent: conf().is_independent_push_notifications_service_enabled(), | ||||
|             }, | ||||
|  | ||||
|             password_policy: PasswordPolicy { | ||||
|   | ||||
| @@ -4,9 +4,10 @@ | ||||
|  | ||||
| use crate::api_data::push_notifications_status_api::PushNotificationsStatusAPI; | ||||
| use crate::data::base_request_handler::BaseRequestHandler; | ||||
| use crate::data::config::conf; | ||||
| use crate::data::http_request_handler::HttpRequestHandler; | ||||
| use crate::data::user_token::PushNotificationToken; | ||||
| use crate::helpers::account_helper; | ||||
| use crate::helpers::{account_helper, independent_push_notification_service_helper}; | ||||
| use crate::routes::RequestResult; | ||||
|  | ||||
| /// Get current push notifications status for a connection | ||||
| @@ -20,8 +21,6 @@ pub fn get_status(r: &mut HttpRequestHandler) -> RequestResult { | ||||
| pub fn configure(r: &mut HttpRequestHandler) -> RequestResult { | ||||
|     let status = r.post_string("status")?; | ||||
|  | ||||
|     // TODO : check availability of each option | ||||
|  | ||||
|     let status = match status.as_str() { | ||||
|         "disabled" => PushNotificationToken::NONE, | ||||
|  | ||||
| @@ -34,7 +33,13 @@ pub fn configure(r: &mut HttpRequestHandler) -> RequestResult { | ||||
|         } | ||||
|  | ||||
|         "independent" => { | ||||
|             unimplemented!(); | ||||
|             if !conf().is_independent_push_notifications_service_enabled() { | ||||
|                 return r.bad_request("Independent service is unavailable!".to_string()); | ||||
|             } | ||||
|  | ||||
|             let token = independent_push_notification_service_helper::create_token()?; | ||||
|  | ||||
|             PushNotificationToken::INDEPENDENT(token) | ||||
|         } | ||||
|  | ||||
|         _ => { | ||||
|   | ||||
| @@ -154,6 +154,11 @@ impl Config { | ||||
|     pub fn server_listen_address(&self) -> String { | ||||
|         format!("{}:{}", self.listen_address, self.port) | ||||
|     } | ||||
|  | ||||
|     /// Check if independent push notifications service is enabled | ||||
|     pub fn is_independent_push_notifications_service_enabled(&self) -> bool { | ||||
|         self.independent_push_service.is_some() | ||||
|     } | ||||
| } | ||||
|  | ||||
| /// Get an instance of the configuration | ||||
|   | ||||
							
								
								
									
										42
									
								
								src/helpers/independent_push_notification_service_helper.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/helpers/independent_push_notification_service_helper.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| //! # Independent push notification service | ||||
| //! | ||||
| //! @author Pierre Hubert | ||||
|  | ||||
|  | ||||
| use actix_web::http::HeaderValue; | ||||
| use reqwest::blocking::Client; | ||||
| use reqwest::header::HeaderMap; | ||||
| use serde::Deserialize; | ||||
|  | ||||
| use crate::data::config::{conf, IndependentPushService}; | ||||
| use crate::data::error::{ExecError, Res}; | ||||
|  | ||||
| #[derive(Deserialize)] | ||||
| struct CreateTokenResult { | ||||
|     token: String | ||||
| } | ||||
|  | ||||
| fn svc_conf() -> Res<&'static IndependentPushService> { | ||||
|     match &conf().independent_push_service { | ||||
|         Some(c) => Ok(c), | ||||
|         None => Err(ExecError::boxed_new("Missing push configuration!")), | ||||
|     } | ||||
| } | ||||
|  | ||||
| fn create_client() -> Res<Client> { | ||||
|     let mut headers = HeaderMap::new(); | ||||
|     headers.insert("token", HeaderValue::from_str(&svc_conf()?.control_token.clone())?); | ||||
|     Ok(Client::builder().default_headers(headers).build()?) | ||||
| } | ||||
|  | ||||
| /// Create a new client token | ||||
| pub fn create_token() -> Res<String> { | ||||
|     let client = create_client()? | ||||
|         .get(&format!("{}{}", svc_conf()?.control_url, "create_client")) | ||||
|         .send()?; | ||||
|  | ||||
|  | ||||
|     let response: CreateTokenResult = client.json()?; | ||||
|  | ||||
|     Ok(response.token) | ||||
| } | ||||
| @@ -17,4 +17,5 @@ pub mod notifications_helper; | ||||
| pub mod webapp_helper; | ||||
| pub mod requests_limit_helper; | ||||
| pub mod events_helper; | ||||
| pub mod calls_helper; | ||||
| pub mod calls_helper; | ||||
| pub mod independent_push_notification_service_helper; | ||||
		Reference in New Issue
	
	Block a user