Migrate from window.electron to tauri IPC functions across components
- Replaced `window.electron.invoke` calls with equivalent `tauri` function calls for all IPC interactions. - Removed `electron.d.ts` TypeScript definitions as they are no longer needed. - Updated related logic for offline/online state synchronization. - Added `types.rs` and `shared/mod.rs` modules to support Tauri IPC integration with Rust enums and shared logic. - Refactored IPC request queues to use updated handler names for consistency with Tauri.
This commit is contained in:
156
src-tauri/src/domains/user/service.rs
Normal file
156
src-tauri/src/domains/user/service.rs
Normal file
@@ -0,0 +1,156 @@
|
||||
use rusqlite::Connection;
|
||||
|
||||
use crate::crypto::encryption::{encrypt_data_with_user_key, decrypt_data_with_user_key, hash_element};
|
||||
use crate::crypto::key_manager::get_user_encryption_key;
|
||||
use crate::domains::user::repo;
|
||||
use crate::error::AppResult;
|
||||
use crate::shared::types::Lang;
|
||||
|
||||
pub struct UserAccount {
|
||||
pub first_name: String,
|
||||
pub last_name: String,
|
||||
pub username: String,
|
||||
pub author_name: String,
|
||||
pub email: String,
|
||||
}
|
||||
|
||||
pub struct GuideTour {
|
||||
pub key: String,
|
||||
pub value: bool,
|
||||
}
|
||||
|
||||
pub struct BookSummary {
|
||||
pub book_id: String,
|
||||
pub title: String,
|
||||
pub sub_title: Option<String>,
|
||||
}
|
||||
|
||||
pub struct UserInfoResponse {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub last_name: String,
|
||||
pub username: String,
|
||||
pub email: String,
|
||||
pub account_verified: bool,
|
||||
pub author_name: String,
|
||||
pub group_id: i64,
|
||||
pub terms_accepted: bool,
|
||||
pub guide_tour: Vec<GuideTour>,
|
||||
}
|
||||
|
||||
/// Retrieves complete user information including associated books.
|
||||
/// * `conn` - Database connection
|
||||
/// * `user_id` - The unique identifier of the user to fetch
|
||||
/// * `lang` - The language for error messages
|
||||
/// Returns the complete user information response.
|
||||
/// Errors if the user is not found or decryption fails.
|
||||
pub fn return_user_infos(conn: &Connection, user_id: &str, lang: Lang) -> AppResult<UserInfoResponse> {
|
||||
let user_infos_data: repo::UserInfosQueryResponse = repo::fetch_user_infos(conn, user_id, lang)?;
|
||||
let user_encryption_key: String = get_user_encryption_key(user_id)?;
|
||||
|
||||
let first_name: String = decrypt_data_with_user_key(&user_infos_data.first_name, &user_encryption_key)?;
|
||||
let last_name: String = decrypt_data_with_user_key(&user_infos_data.last_name, &user_encryption_key)?;
|
||||
let username: String = decrypt_data_with_user_key(&user_infos_data.username, &user_encryption_key)?;
|
||||
let email: String = decrypt_data_with_user_key(&user_infos_data.email, &user_encryption_key)?;
|
||||
let account_verified: bool = user_infos_data.account_verified == 1;
|
||||
let author_name: String = if let Some(ref author_name_val) = user_infos_data.author_name { decrypt_data_with_user_key(author_name_val, &user_encryption_key)? } else { String::new() };
|
||||
let group_id: i64 = user_infos_data.user_group;
|
||||
let terms_accepted: bool = user_infos_data.term_accepted == 1;
|
||||
let guide_tour_status: Vec<GuideTour> = vec![];
|
||||
|
||||
Ok(UserInfoResponse {
|
||||
id: user_id.to_string(),
|
||||
name: first_name,
|
||||
last_name,
|
||||
username,
|
||||
email,
|
||||
account_verified,
|
||||
author_name,
|
||||
group_id,
|
||||
terms_accepted,
|
||||
guide_tour: guide_tour_status,
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates a new user in the database with encrypted personal information.
|
||||
/// * `conn` - Database connection
|
||||
/// * `user_id` - The unique identifier for the new user
|
||||
/// * `first_name` - The user's first name (will be encrypted)
|
||||
/// * `last_name` - The user's last name (will be encrypted)
|
||||
/// * `username` - The user's username (will be encrypted and hashed)
|
||||
/// * `email` - The user's email address (will be encrypted and hashed)
|
||||
/// * `not_encrypt_password` - The user's password in plain text (unused in current implementation)
|
||||
/// * `lang` - The preferred language for the user
|
||||
/// Returns the created user's identifier.
|
||||
/// Errors if encryption or insertion fails.
|
||||
pub fn add_user(
|
||||
conn: &Connection, user_id: &str, first_name: &str, last_name: &str, username: &str,
|
||||
email: &str, _not_encrypt_password: &str, lang: Lang,
|
||||
) -> AppResult<String> {
|
||||
let user_encryption_key: String = get_user_encryption_key(user_id)?;
|
||||
let encrypted_first_name: String = encrypt_data_with_user_key(first_name, &user_encryption_key)?;
|
||||
let encrypted_last_name: String = encrypt_data_with_user_key(last_name, &user_encryption_key)?;
|
||||
let encrypted_username: String = encrypt_data_with_user_key(username, &user_encryption_key)?;
|
||||
let encrypted_email: String = encrypt_data_with_user_key(email, &user_encryption_key)?;
|
||||
let hashed_email: String = hash_element(email);
|
||||
let hashed_username: String = hash_element(username);
|
||||
|
||||
repo::insert_user(conn, user_id, &encrypted_first_name, &encrypted_last_name, &encrypted_username, &hashed_username, &encrypted_email, &hashed_email, lang)
|
||||
}
|
||||
|
||||
/// Updates an existing user's profile information in the database.
|
||||
/// * `conn` - Database connection
|
||||
/// * `user_key` - The encryption key for the user's data
|
||||
/// * `user_id` - The unique identifier of the user to update
|
||||
/// * `first_name` - The updated first name (will be encrypted)
|
||||
/// * `last_name` - The updated last name (will be encrypted)
|
||||
/// * `username` - The updated username (will be encrypted and hashed)
|
||||
/// * `email` - The updated email address (will be encrypted and hashed)
|
||||
/// * `author_name` - The optional author/pen name (will be encrypted and hashed if provided)
|
||||
/// * `lang` - The preferred language for the user
|
||||
/// Returns true if the update was successful.
|
||||
/// Errors if encryption or update fails.
|
||||
pub fn update_user_infos(
|
||||
conn: &Connection, user_key: &str, user_id: &str, first_name: &str, last_name: &str,
|
||||
username: &str, email: &str, author_name: Option<&str>, lang: Lang,
|
||||
) -> AppResult<bool> {
|
||||
let encrypted_first_name: String = encrypt_data_with_user_key(first_name, user_key)?;
|
||||
let encrypted_last_name: String = encrypt_data_with_user_key(last_name, user_key)?;
|
||||
let encrypted_username: String = encrypt_data_with_user_key(username, user_key)?;
|
||||
let encrypted_email: String = encrypt_data_with_user_key(email, user_key)?;
|
||||
let hashed_email: String = hash_element(email);
|
||||
let hashed_username: String = hash_element(username);
|
||||
let mut encrypted_author_name: String = String::new();
|
||||
let mut hashed_author_name: String = String::new();
|
||||
if let Some(author_name_val) = author_name {
|
||||
encrypted_author_name = encrypt_data_with_user_key(author_name_val, user_key)?;
|
||||
hashed_author_name = hash_element(author_name_val);
|
||||
}
|
||||
|
||||
repo::update_user_infos(conn, user_id, &encrypted_first_name, &encrypted_last_name, &encrypted_username, &hashed_username, &encrypted_email, &hashed_email, &hashed_author_name, &encrypted_author_name, lang)
|
||||
}
|
||||
|
||||
/// Retrieves and decrypts the user's account information from the database.
|
||||
/// * `conn` - Database connection
|
||||
/// * `user_id` - The unique identifier of the user
|
||||
/// * `lang` - The language for error messages
|
||||
/// Returns the decrypted user account information.
|
||||
/// Errors if the user is not found or decryption fails.
|
||||
pub fn get_user_account_information(conn: &Connection, user_id: &str, lang: Lang) -> AppResult<UserAccount> {
|
||||
let account_data: repo::UserAccountQuery = repo::fetch_account_information(conn, user_id, lang)?;
|
||||
let user_encryption_key: String = get_user_encryption_key(user_id)?;
|
||||
|
||||
let decrypted_first_name: String = if let Some(ref first_name) = account_data.first_name { decrypt_data_with_user_key(first_name, &user_encryption_key)? } else { String::new() };
|
||||
let decrypted_last_name: String = if let Some(ref last_name) = account_data.last_name { decrypt_data_with_user_key(last_name, &user_encryption_key)? } else { String::new() };
|
||||
let decrypted_username: String = decrypt_data_with_user_key(&account_data.username, &user_encryption_key)?;
|
||||
let decrypted_author_name: String = if let Some(ref author_name) = account_data.author_name { decrypt_data_with_user_key(author_name, &user_encryption_key)? } else { String::new() };
|
||||
let decrypted_email: String = decrypt_data_with_user_key(&account_data.email, &user_encryption_key)?;
|
||||
|
||||
Ok(UserAccount {
|
||||
first_name: decrypted_first_name,
|
||||
last_name: decrypted_last_name,
|
||||
username: decrypted_username,
|
||||
author_name: decrypted_author_name,
|
||||
email: decrypted_email,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user