From d6e5cb6fed209331616d844a1271f99a2e120421 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 12 Apr 2021 17:20:06 +0200 Subject: [PATCH] Un-register from push notification service --- src/helpers/account_helper.rs | 24 +++++++++++++------ ...endent_push_notification_service_helper.rs | 24 +++++++++++++++++-- src/helpers/mod.rs | 1 + src/helpers/push_notifications_helper.rs | 20 ++++++++++++++++ 4 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 src/helpers/push_notifications_helper.rs diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 4c3c0e3..1cc08f4 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -14,7 +14,7 @@ use crate::data::new_notifications_settings::NewNotificationsSettings; use crate::data::security_settings::SecuritySettings; use crate::data::user::{AccountImageVisibility, User, UserID, UserPageStatus}; use crate::data::user_token::{PushNotificationToken, UserAccessToken}; -use crate::helpers::{comments_helper, conversations_helper, custom_emojies_helper, database, events_helper, friends_helper, groups_helper, likes_helper, notifications_helper, posts_helper, survey_helper, user_helper}; +use crate::helpers::{comments_helper, conversations_helper, custom_emojies_helper, database, events_helper, friends_helper, groups_helper, likes_helper, notifications_helper, posts_helper, push_notifications_helper, survey_helper, user_helper}; use crate::helpers::database::{DeleteQuery, InsertQuery, QueryInfo, RowResult, UpdateInfo}; use crate::helpers::events_helper::Event; use crate::helpers::likes_helper::LikeType; @@ -101,8 +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 + // Un-register from independent push notifications service // (continue to destroy token even in case of failure) + push_notifications_helper::un_register_from_previous_service(access_tokens)?; DeleteQuery::new(USER_ACCESS_TOKENS_TABLE) .cond_u64("id", access_tokens.id) @@ -128,14 +129,22 @@ pub fn clean_up_old_access_tokens() -> Res { Ok(()) } +/// Get all the login tokens of a user +pub fn get_all_login_tokens(id: &UserID) -> Res> { + database::QueryInfo::new(USER_ACCESS_TOKENS_TABLE) + .cond_user_id("user_id", id) + .exec(db_to_user_access_token) +} + /// Destroy all login tokens of a user 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() + for token in get_all_login_tokens(id)? { + destroy_login_tokens(&token)?; + } + + Ok(()) } /// Generate a new password reset token @@ -312,7 +321,8 @@ pub fn set_notifications_settings(new_settings: NewNotificationsSettings) -> Res /// 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 + // In case of independent push service, remove previous client + push_notifications_helper::un_register_from_previous_service(client)?; database::UpdateInfo::new(USER_ACCESS_TOKENS_TABLE) .cond_u64("id", client.id) diff --git a/src/helpers/independent_push_notification_service_helper.rs b/src/helpers/independent_push_notification_service_helper.rs index 6bc2824..206160e 100644 --- a/src/helpers/independent_push_notification_service_helper.rs +++ b/src/helpers/independent_push_notification_service_helper.rs @@ -3,10 +3,10 @@ //! @author Pierre Hubert -use actix_web::http::HeaderValue; +use actix_web::http::{HeaderValue, StatusCode}; use reqwest::blocking::Client; use reqwest::header::HeaderMap; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use crate::data::config::{conf, IndependentPushService}; use crate::data::error::{ExecError, Res}; @@ -16,6 +16,11 @@ struct CreateTokenResult { token: String } +#[derive(Serialize)] +struct RemoveTokenRequest { + client: String, +} + fn svc_conf() -> Res<&'static IndependentPushService> { match &conf().independent_push_service { Some(c) => Ok(c), @@ -39,4 +44,19 @@ pub fn create_token() -> Res { let response: CreateTokenResult = client.json()?; Ok(response.token) +} + +/// Destroy a client token +pub fn remove_token(t: &str) -> Res { + let client = create_client()? + .get(&format!("{}{}", svc_conf()?.control_url, "remove_client")) + .json(&RemoveTokenRequest { client: t.to_string() }) + .send()?; + + if client.status() == StatusCode::OK { + return Ok(()); + } + + Err(ExecError::boxed_string( + format!("Failed to remove client, got a status {} for service!", client.status().as_u16()))) } \ No newline at end of file diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 25b74da..4932c0d 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -18,4 +18,5 @@ pub mod webapp_helper; pub mod requests_limit_helper; pub mod events_helper; pub mod calls_helper; +pub mod push_notifications_helper; pub mod independent_push_notification_service_helper; \ No newline at end of file diff --git a/src/helpers/push_notifications_helper.rs b/src/helpers/push_notifications_helper.rs new file mode 100644 index 0000000..8f35b0a --- /dev/null +++ b/src/helpers/push_notifications_helper.rs @@ -0,0 +1,20 @@ +//! # Push notifications helper +//! +//! @author Pierre Hubert + +use crate::data::user_token::{UserAccessToken, PushNotificationToken}; +use crate::data::error::Res; +use crate::helpers::independent_push_notification_service_helper; + +/// Un-register for previous push notifications service +pub fn un_register_from_previous_service(client: &UserAccessToken) -> Res { + + // This method must not fail in case of error + if let PushNotificationToken::INDEPENDENT(old_token) = &client.push_notifications_token { + if let Err(e) = independent_push_notification_service_helper::remove_token(old_token) { + eprintln!("Failed to un-register from independent push notifications service! {}", e); + } + } + + Ok(()) +} \ No newline at end of file