1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Un-register from push notification service

This commit is contained in:
Pierre HUBERT 2021-04-12 17:20:06 +02:00
parent 9c1675235c
commit d6e5cb6fed
4 changed files with 60 additions and 9 deletions

View File

@ -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<Vec<UserAccessToken>> {
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)

View File

@ -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<String> {
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())))
}

View File

@ -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;

View File

@ -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(())
}