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

Create client push notification token

This commit is contained in:
Pierre HUBERT 2021-04-11 17:32:50 +02:00
parent e5f9de0507
commit 9c6bd0a75a
3 changed files with 51 additions and 1 deletions

View File

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

View File

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

View File

@ -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<bool> {
}
fn db_to_user_access_token(res: &RowResult) -> Res<UserAccessToken> {
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<UserAccessToken> {
token: res.get_str("token")?,
last_refresh: res.get_u64("last_refresh")?,
timeout: res.get_u64("timeout")?,
push_notifications_token,
})
}