diff --git a/src/controllers/push_notifications_controller.rs b/src/controllers/push_notifications_controller.rs index fd0da10..6190c40 100644 --- a/src/controllers/push_notifications_controller.rs +++ b/src/controllers/push_notifications_controller.rs @@ -5,6 +5,8 @@ use crate::api_data::push_notifications_status_api::PushNotificationsStatusAPI; use crate::data::base_request_handler::BaseRequestHandler; use crate::data::http_request_handler::HttpRequestHandler; +use crate::data::user_token::PushNotificationToken; +use crate::helpers::account_helper; use crate::routes::RequestResult; /// Get current push notifications status for a connection @@ -12,4 +14,31 @@ pub fn get_status(r: &mut HttpRequestHandler) -> RequestResult { let status = &r.user_access_token().unwrap().push_notifications_token.clone(); r.set_response(PushNotificationsStatusAPI::new(status)) +} + +/// Configure push notifications for a client +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, + + "firebase" => { + PushNotificationToken::FIREBASE(r.post_string("firebase_token")?) + } + + "independent" => { + unimplemented!(); + } + + _ => { + return r.bad_request(format!("Unknown status: {}", status)); + } + }; + + account_helper::set_push_notification_token(r.user_access_token().unwrap(), status)?; + + r.ok() } \ No newline at end of file diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 105c231..4c3c0e3 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -101,6 +101,9 @@ pub fn refresh_access_token(token: &UserAccessToken) -> Res { /// Destroy a given user login tokens pub fn destroy_login_tokens(access_tokens: &UserAccessToken) -> Res { + // TODO : un-register from independent push notifications service + // (continue to destroy token even in case of failure) + DeleteQuery::new(USER_ACCESS_TOKENS_TABLE) .cond_u64("id", access_tokens.id) .exec()?; @@ -129,6 +132,7 @@ pub fn clean_up_old_access_tokens() -> Res { pub fn destroy_all_user_tokens(id: &UserID) -> ResultBoxError { user_ws_controller::disconnect_user_from_all_sockets(id)?; + // TODO : call destroy_login_tokens for each call to do proper cleanup database::DeleteQuery::new(USER_ACCESS_TOKENS_TABLE) .cond_user_id("user_id", id) .exec() @@ -306,6 +310,16 @@ pub fn set_notifications_settings(new_settings: NewNotificationsSettings) -> Res .exec() } +/// Set new push notification token +pub fn set_push_notification_token(client: &UserAccessToken, new_token: PushNotificationToken) -> Res { + // TODO : in case of independent push service, remove previous client + + database::UpdateInfo::new(USER_ACCESS_TOKENS_TABLE) + .cond_u64("id", client.id) + .set_opt_str("push_notifications_token", new_token.to_db()) + .exec() +} + /// Export an account's data pub fn export(user_id: &UserID) -> ResultBoxError { let mut data = AccountExport { diff --git a/src/routes.rs b/src/routes.rs index a0b7591..1e992ab 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -180,6 +180,7 @@ pub fn get_routes() -> Vec { // Push notifications controller Route::post("/push_notifications/status", Box::new(push_notifications_controller::get_status)), + Route::post("/push_notifications/configure", Box::new(push_notifications_controller::configure)), // Friends controller Route::post("/friends/getList", Box::new(friends_controller::get_list)),