diff --git a/src/constants.rs b/src/constants.rs index a594a6e..421cc56 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -59,6 +59,13 @@ pub mod database_tables_names { pub const NOTIFICATIONS_TABLE: &str = "comunic_notifications"; } +/// Push Notifications Database prefix +pub mod push_notifications_db_prefix { + pub const NONE_PREFIX: &str = "NONE"; + pub const INDEPENDENT_PREFIX: &str = "I:"; + pub const FIREBASE_PREFIX: &str = "F:"; +} + /// Password policy pub mod password_policy { /// Allow email in password ? diff --git a/src/data/user_token.rs b/src/data/user_token.rs index 4584992..1a74414 100644 --- a/src/data/user_token.rs +++ b/src/data/user_token.rs @@ -1,7 +1,44 @@ +use crate::constants::push_notifications_db_prefix::{FIREBASE_PREFIX, INDEPENDENT_PREFIX, NONE_PREFIX}; use crate::constants::USER_ACCESS_TOKEN_ACTIVITY_REFRESH; use crate::data::user::UserID; use crate::utils::date_utils::time; +#[derive(Debug, Clone)] +pub enum PushNotificationToken { + UNDEFINED, + NONE, + INDEPENDENT(String), + FIREBASE(String), +} + +impl PushNotificationToken { + pub fn from_db(token: Option) -> Self { + match token { + None => Self::UNDEFINED, + Some(s) => { + if s.is_empty() { + Self::UNDEFINED + } else if s.starts_with(NONE_PREFIX) { + Self::NONE + } else if s.starts_with(INDEPENDENT_PREFIX) { + Self::INDEPENDENT(s.replacen(INDEPENDENT_PREFIX, "", 1)) + } else { + Self::FIREBASE(s.replacen(FIREBASE_PREFIX, "", 1)) + } + } + } + } + + pub fn to_db(&self) -> Option { + match self { + PushNotificationToken::UNDEFINED => None, + PushNotificationToken::NONE => Some(NONE_PREFIX.to_string()), + PushNotificationToken::INDEPENDENT(k) => Some(format!("{}{}", INDEPENDENT_PREFIX, k)), + PushNotificationToken::FIREBASE(k) => Some(format!("{}{}", FIREBASE_PREFIX, k)), + } + } +} + /// User access token information /// /// Author : Pierre Hubert @@ -13,6 +50,7 @@ pub struct UserAccessToken { pub token: String, pub last_refresh: u64, pub timeout: u64, + pub push_notifications_token: PushNotificationToken, } impl UserAccessToken { diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 4b1db65..8d5101c 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -13,7 +13,7 @@ use crate::data::new_data_conservation_policy::NewDataConservationPolicy; 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::UserAccessToken; +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::database::{DeleteQuery, InsertQuery, QueryInfo, RowResult, UpdateInfo}; use crate::helpers::events_helper::Event; @@ -57,6 +57,7 @@ pub fn login_user(email: &str, password: &str, client: &APIClient) -> ResultBoxE token: rand_str(150), last_refresh: time(), timeout: client.default_expiration_time, + push_notifications_token: PushNotificationToken::UNDEFINED, }; // Save it @@ -66,6 +67,7 @@ pub fn login_user(email: &str, password: &str, client: &APIClient) -> ResultBoxE .add_str("token", &new_token.token) .add_u64("last_refresh", new_token.last_refresh) .add_u64("timeout", new_token.timeout) + .add_opt_str("push_notification_token", new_token.push_notifications_token.to_db().as_ref()) .insert_drop_result()?; Ok(new_token.token) @@ -426,6 +428,8 @@ fn validate_password(user: &User, password: &str) -> Res { } fn db_to_user_access_token(res: &RowResult) -> Res { + let push_notifications_token = PushNotificationToken::from_db(res.get_optional_str("push_notifications_token")?); + Ok(UserAccessToken { id: res.get_u64("id")?, client_id: res.get_u64("client_id")?, @@ -433,5 +437,6 @@ fn db_to_user_access_token(res: &RowResult) -> Res { token: res.get_str("token")?, last_refresh: res.get_u64("last_refresh")?, timeout: res.get_u64("timeout")?, + push_notifications_token, }) } \ No newline at end of file