daemon: setting foundation for client creation
This commit is contained in:
@@ -1,13 +1,23 @@
|
||||
mod settings;
|
||||
use settings::Settings;
|
||||
|
||||
use directories::ProjectDirs;
|
||||
use std::path::PathBuf;
|
||||
use anyhow::Result;
|
||||
use thiserror::Error;
|
||||
|
||||
use kordophone_db::{
|
||||
database::Database,
|
||||
models::Conversation,
|
||||
repository::Repository,
|
||||
};
|
||||
|
||||
use kordophone::api::http_client::HTTPAPIClient;
|
||||
use kordophone::model::JwtToken;
|
||||
use kordophone::api::{
|
||||
http_client::{Credentials, HTTPAPIClient},
|
||||
APIInterface,
|
||||
TokenManagement,
|
||||
};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum DaemonError {
|
||||
@@ -18,7 +28,6 @@ pub enum DaemonError {
|
||||
pub struct Daemon {
|
||||
pub version: String,
|
||||
database: Database,
|
||||
client: Option<HTTPAPIClient>,
|
||||
}
|
||||
|
||||
impl Daemon {
|
||||
@@ -31,27 +40,80 @@ impl Daemon {
|
||||
std::fs::create_dir_all(database_dir)?;
|
||||
|
||||
let database = Database::new(&database_path.to_string_lossy())?;
|
||||
|
||||
// TODO: Check to see if we have client settings in the database
|
||||
|
||||
|
||||
Ok(Self { version: "0.1.0".to_string(), database, client: None })
|
||||
Ok(Self { version: "0.1.0".to_string(), database })
|
||||
}
|
||||
|
||||
pub fn get_conversations(&mut self) -> Vec<Conversation> {
|
||||
self.database.with_repository(|r| r.all_conversations().unwrap())
|
||||
}
|
||||
|
||||
pub fn sync_all_conversations(&mut self) -> Result<()> {
|
||||
let client = self.client
|
||||
.as_mut()
|
||||
.ok_or(DaemonError::ClientNotConfigured)?;
|
||||
pub async fn sync_all_conversations(&mut self) -> Result<()> {
|
||||
let mut client = self.get_client()
|
||||
.map_err(|_| DaemonError::ClientNotConfigured)?;
|
||||
|
||||
let fetched_conversations = client.get_conversations().await?;
|
||||
let db_conversations: Vec<kordophone_db::models::Conversation> = fetched_conversations.into_iter()
|
||||
.map(|c| kordophone_db::models::Conversation::from(c))
|
||||
.collect();
|
||||
|
||||
// Process each conversation
|
||||
let mut repository = Repository::new(&mut self.database.connection);
|
||||
for conversation in db_conversations {
|
||||
let conversation_id = conversation.guid.clone();
|
||||
|
||||
// Insert the conversation
|
||||
repository.insert_conversation(conversation)?;
|
||||
|
||||
// Fetch and sync messages for this conversation
|
||||
let messages = client.get_messages(&conversation_id).await?;
|
||||
let db_messages: Vec<kordophone_db::models::Message> = messages.into_iter()
|
||||
.map(|m| kordophone_db::models::Message::from(m))
|
||||
.collect();
|
||||
|
||||
// Insert each message
|
||||
for message in db_messages {
|
||||
repository.insert_message(&conversation_id, message)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_settings(&mut self) -> Result<Settings> {
|
||||
let settings = self.database.with_settings(|s|
|
||||
Settings::from_db(s)
|
||||
)?;
|
||||
|
||||
Ok(settings)
|
||||
}
|
||||
|
||||
fn get_client(&mut self) -> Result<HTTPAPIClient> {
|
||||
let settings = self.database.with_settings(|s|
|
||||
Settings::from_db(s)
|
||||
)?;
|
||||
|
||||
let server_url = settings.server_url
|
||||
.ok_or(DaemonError::ClientNotConfigured)?;
|
||||
|
||||
let client = HTTPAPIClient::new(
|
||||
server_url.parse().unwrap(),
|
||||
|
||||
match (settings.username, settings.credential_item) {
|
||||
(Some(username), Some(password)) => Some(
|
||||
Credentials {
|
||||
username,
|
||||
password,
|
||||
}
|
||||
),
|
||||
_ => None,
|
||||
}
|
||||
);
|
||||
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
fn get_database_path() -> PathBuf {
|
||||
if let Some(proj_dirs) = ProjectDirs::from("com", "kordophone", "kordophone") {
|
||||
if let Some(proj_dirs) = ProjectDirs::from("net", "buzzert", "kordophonecd") {
|
||||
let data_dir = proj_dirs.data_dir();
|
||||
data_dir.join("database.db")
|
||||
} else {
|
||||
@@ -59,4 +121,15 @@ impl Daemon {
|
||||
PathBuf::from("database.db")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TokenManagement for &mut Daemon {
|
||||
fn get_token(&mut self) -> Option<JwtToken> {
|
||||
self.database.get_token()
|
||||
}
|
||||
|
||||
fn set_token(&mut self, token: JwtToken) {
|
||||
self.database.set_token(token);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user