2025-04-25 20:02:18 -07:00
|
|
|
mod settings;
|
|
|
|
|
use settings::Settings;
|
|
|
|
|
|
2025-04-25 21:42:29 -07:00
|
|
|
use std::sync::mpsc;
|
2025-04-25 16:54:37 -07:00
|
|
|
use directories::ProjectDirs;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use anyhow::Result;
|
2025-04-25 18:02:54 -07:00
|
|
|
use thiserror::Error;
|
2025-04-25 20:02:18 -07:00
|
|
|
|
2025-04-25 16:54:37 -07:00
|
|
|
use kordophone_db::{
|
|
|
|
|
database::Database,
|
|
|
|
|
models::Conversation,
|
2025-04-25 20:02:18 -07:00
|
|
|
repository::Repository,
|
2025-04-25 16:54:37 -07:00
|
|
|
};
|
|
|
|
|
|
2025-04-25 20:02:18 -07:00
|
|
|
use kordophone::model::JwtToken;
|
|
|
|
|
use kordophone::api::{
|
|
|
|
|
http_client::{Credentials, HTTPAPIClient},
|
|
|
|
|
APIInterface,
|
|
|
|
|
TokenManagement,
|
|
|
|
|
};
|
2025-04-25 18:02:54 -07:00
|
|
|
|
2025-04-25 21:42:29 -07:00
|
|
|
pub enum Event {
|
|
|
|
|
SyncAllConversations,
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-25 18:02:54 -07:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
|
pub enum DaemonError {
|
|
|
|
|
#[error("Client Not Configured")]
|
|
|
|
|
ClientNotConfigured,
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 23:15:24 -08:00
|
|
|
pub struct Daemon {
|
|
|
|
|
pub version: String,
|
2025-04-25 16:54:37 -07:00
|
|
|
database: Database,
|
2025-02-11 23:15:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Daemon {
|
2025-04-25 16:54:37 -07:00
|
|
|
pub fn new() -> Result<Self> {
|
|
|
|
|
let database_path = Self::get_database_path();
|
|
|
|
|
log::info!("Database path: {}", database_path.display());
|
|
|
|
|
|
|
|
|
|
// Create the database directory if it doesn't exist
|
|
|
|
|
let database_dir = database_path.parent().unwrap();
|
|
|
|
|
std::fs::create_dir_all(database_dir)?;
|
|
|
|
|
|
|
|
|
|
let database = Database::new(&database_path.to_string_lossy())?;
|
2025-04-25 20:02:18 -07:00
|
|
|
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 async fn sync_all_conversations(&mut self) -> Result<()> {
|
|
|
|
|
let mut client = self.get_client()
|
|
|
|
|
.map_err(|_| DaemonError::ClientNotConfigured)?;
|
2025-04-25 16:54:37 -07:00
|
|
|
|
2025-04-25 20:02:18 -07:00
|
|
|
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();
|
2025-04-25 18:02:54 -07:00
|
|
|
|
2025-04-25 20:02:18 -07:00
|
|
|
// 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)?;
|
2025-04-25 18:02:54 -07:00
|
|
|
|
2025-04-25 20:02:18 -07:00
|
|
|
// 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(())
|
2025-04-25 16:54:37 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-25 20:02:18 -07:00
|
|
|
pub fn get_settings(&mut self) -> Result<Settings> {
|
|
|
|
|
let settings = self.database.with_settings(|s|
|
|
|
|
|
Settings::from_db(s)
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
Ok(settings)
|
2025-04-25 16:54:37 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-25 21:42:29 -07:00
|
|
|
pub async fn handle_event(&mut self, event: Event) {
|
|
|
|
|
match event {
|
|
|
|
|
Event::SyncAllConversations => {
|
|
|
|
|
self.sync_all_conversations().await.unwrap_or_else(|e| {
|
|
|
|
|
log::error!("Error handling sync event: {}", e);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-25 20:02:18 -07:00
|
|
|
fn get_client(&mut self) -> Result<HTTPAPIClient> {
|
|
|
|
|
let settings = self.database.with_settings(|s|
|
|
|
|
|
Settings::from_db(s)
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
let server_url = settings.server_url
|
2025-04-25 18:02:54 -07:00
|
|
|
.ok_or(DaemonError::ClientNotConfigured)?;
|
|
|
|
|
|
2025-04-25 20:02:18 -07:00
|
|
|
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)
|
2025-04-25 18:02:54 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-25 16:54:37 -07:00
|
|
|
fn get_database_path() -> PathBuf {
|
2025-04-25 20:02:18 -07:00
|
|
|
if let Some(proj_dirs) = ProjectDirs::from("net", "buzzert", "kordophonecd") {
|
2025-04-25 16:54:37 -07:00
|
|
|
let data_dir = proj_dirs.data_dir();
|
|
|
|
|
data_dir.join("database.db")
|
|
|
|
|
} else {
|
|
|
|
|
// Fallback to a local path if we can't get the system directories
|
|
|
|
|
PathBuf::from("database.db")
|
|
|
|
|
}
|
2025-02-11 23:15:24 -08:00
|
|
|
}
|
2025-04-25 20:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|