2021-04-11 18:58:34 +02:00
|
|
|
//! # Push notifications controller
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
|
|
|
use crate::api_data::push_notifications_status_api::PushNotificationsStatusAPI;
|
|
|
|
use crate::data::base_request_handler::BaseRequestHandler;
|
2021-04-12 17:00:08 +02:00
|
|
|
use crate::data::config::conf;
|
2021-04-11 18:58:34 +02:00
|
|
|
use crate::data::http_request_handler::HttpRequestHandler;
|
2021-04-11 19:14:59 +02:00
|
|
|
use crate::data::user_token::PushNotificationToken;
|
2021-04-12 17:59:00 +02:00
|
|
|
use crate::helpers::{account_helper, independent_push_notifications_service_helper};
|
2021-04-11 18:58:34 +02:00
|
|
|
use crate::routes::RequestResult;
|
|
|
|
|
|
|
|
/// Get current push notifications status for a connection
|
2022-03-11 21:56:08 +01:00
|
|
|
pub async fn get_status(r: &mut HttpRequestHandler) -> RequestResult {
|
2023-05-31 19:10:05 +02:00
|
|
|
let status = &r
|
|
|
|
.user_access_token()
|
|
|
|
.unwrap()
|
|
|
|
.push_notifications_token
|
|
|
|
.clone();
|
2021-04-11 18:58:34 +02:00
|
|
|
|
|
|
|
r.set_response(PushNotificationsStatusAPI::new(status))
|
2021-04-11 19:14:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Configure push notifications for a client
|
2022-03-11 21:56:08 +01:00
|
|
|
pub async fn configure(r: &mut HttpRequestHandler) -> RequestResult {
|
2021-04-11 19:14:59 +02:00
|
|
|
let status = r.post_string("status")?;
|
|
|
|
|
|
|
|
let status = match status.as_str() {
|
|
|
|
"disabled" => PushNotificationToken::NONE,
|
|
|
|
|
|
|
|
"firebase" => {
|
2021-04-11 19:18:28 +02:00
|
|
|
if !r.api_client().is_firebase_available() {
|
|
|
|
return r.bad_request("Firebase is unavailable!".to_string());
|
|
|
|
}
|
|
|
|
|
2021-04-11 19:14:59 +02:00
|
|
|
PushNotificationToken::FIREBASE(r.post_string("firebase_token")?)
|
|
|
|
}
|
|
|
|
|
|
|
|
"independent" => {
|
2021-04-12 17:00:08 +02:00
|
|
|
if !conf().is_independent_push_notifications_service_enabled() {
|
|
|
|
return r.bad_request("Independent service is unavailable!".to_string());
|
|
|
|
}
|
|
|
|
|
2022-03-12 09:10:22 +01:00
|
|
|
let token = independent_push_notifications_service_helper::create_token().await?;
|
2021-04-12 17:00:08 +02:00
|
|
|
|
|
|
|
PushNotificationToken::INDEPENDENT(token)
|
2021-04-11 19:14:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
return r.bad_request(format!("Unknown status: {}", status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-12 09:10:22 +01:00
|
|
|
account_helper::set_push_notification_token(r.user_access_token().unwrap(), status).await?;
|
2021-04-11 19:14:59 +02:00
|
|
|
|
|
|
|
r.ok()
|
2023-05-31 19:10:05 +02:00
|
|
|
}
|