2025-04-27 12:53:45 -07:00
|
|
|
pub mod settings;
|
2025-05-01 01:02:36 -07:00
|
|
|
use settings::keys as SettingsKey;
|
2025-05-15 20:11:10 -07:00
|
|
|
use settings::Settings;
|
2025-04-25 20:02:18 -07:00
|
|
|
|
2025-04-27 12:53:45 -07:00
|
|
|
pub mod events;
|
|
|
|
|
use events::*;
|
|
|
|
|
|
2025-04-27 22:44:05 -07:00
|
|
|
pub mod signals;
|
|
|
|
|
use signals::*;
|
|
|
|
|
|
2025-04-27 12:53:45 -07:00
|
|
|
use anyhow::Result;
|
2025-04-25 16:54:37 -07:00
|
|
|
use directories::ProjectDirs;
|
2025-05-03 21:45:53 -07:00
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2025-05-15 20:11:10 -07:00
|
|
|
use std::error::Error;
|
2025-06-06 16:35:51 -07:00
|
|
|
use std::path::PathBuf;
|
2025-05-03 21:45:53 -07:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2025-04-25 18:02:54 -07:00
|
|
|
use thiserror::Error;
|
2025-05-15 20:11:10 -07:00
|
|
|
use tokio::sync::mpsc::{Receiver, Sender};
|
2025-04-27 13:40:59 -07:00
|
|
|
use tokio::sync::Mutex;
|
2025-05-02 14:22:43 -07:00
|
|
|
use uuid::Uuid;
|
2025-04-25 20:02:18 -07:00
|
|
|
|
2025-04-25 16:54:37 -07:00
|
|
|
use kordophone_db::{
|
2025-04-27 13:40:59 -07:00
|
|
|
database::{Database, DatabaseAccess},
|
2025-06-06 16:28:29 -07:00
|
|
|
models::Conversation,
|
2025-04-25 16:54:37 -07:00
|
|
|
};
|
|
|
|
|
|
2025-05-01 20:45:20 -07:00
|
|
|
use kordophone::api::http_client::HTTPAPIClient;
|
2025-05-15 20:11:10 -07:00
|
|
|
use kordophone::api::APIInterface;
|
2025-05-02 14:22:43 -07:00
|
|
|
use kordophone::model::outgoing_message::OutgoingMessage;
|
2025-05-03 21:45:53 -07:00
|
|
|
use kordophone::model::ConversationID;
|
2025-04-25 18:02:54 -07:00
|
|
|
|
2025-05-01 20:36:43 -07:00
|
|
|
mod update_monitor;
|
|
|
|
|
use update_monitor::UpdateMonitor;
|
|
|
|
|
|
2025-05-01 20:45:20 -07:00
|
|
|
mod auth_store;
|
|
|
|
|
use auth_store::DatabaseAuthenticationStore;
|
|
|
|
|
|
2025-05-02 14:22:43 -07:00
|
|
|
mod post_office;
|
|
|
|
|
use post_office::Event as PostOfficeEvent;
|
2025-05-15 20:11:10 -07:00
|
|
|
use post_office::PostOffice;
|
|
|
|
|
|
2025-06-06 16:28:29 -07:00
|
|
|
mod models;
|
|
|
|
|
pub use models::Attachment;
|
|
|
|
|
pub use models::Message;
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
mod attachment_store;
|
2025-06-05 20:19:34 -07:00
|
|
|
pub use attachment_store::AttachmentStore;
|
|
|
|
|
pub use attachment_store::AttachmentStoreEvent;
|
2025-05-02 14:22:43 -07:00
|
|
|
|
2025-04-25 18:02:54 -07:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
|
pub enum DaemonError {
|
|
|
|
|
#[error("Client Not Configured")]
|
|
|
|
|
ClientNotConfigured,
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-27 12:53:45 -07:00
|
|
|
pub type DaemonResult<T> = Result<T, Box<dyn Error + Send + Sync>>;
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
type DaemonClient = HTTPAPIClient<DatabaseAuthenticationStore>;
|
|
|
|
|
|
2025-05-01 20:36:43 -07:00
|
|
|
pub mod target {
|
2025-04-27 18:07:58 -07:00
|
|
|
pub static SYNC: &str = "sync";
|
2025-04-28 16:00:04 -07:00
|
|
|
pub static EVENT: &str = "event";
|
2025-05-01 20:45:20 -07:00
|
|
|
pub static SETTINGS: &str = "settings";
|
2025-05-01 20:36:43 -07:00
|
|
|
pub static UPDATES: &str = "updates";
|
2025-05-26 16:19:26 -07:00
|
|
|
pub static ATTACHMENTS: &str = "attachments";
|
2025-04-27 18:07:58 -07:00
|
|
|
}
|
2025-05-03 01:06:50 -07:00
|
|
|
|
2025-02-11 23:15:24 -08:00
|
|
|
pub struct Daemon {
|
2025-04-27 12:53:45 -07:00
|
|
|
pub event_sender: Sender<Event>,
|
|
|
|
|
event_receiver: Receiver<Event>,
|
2025-04-27 22:44:05 -07:00
|
|
|
|
|
|
|
|
signal_receiver: Option<Receiver<Signal>>,
|
|
|
|
|
signal_sender: Sender<Signal>,
|
|
|
|
|
|
2025-05-02 14:22:43 -07:00
|
|
|
post_office_sink: Sender<PostOfficeEvent>,
|
|
|
|
|
post_office_source: Option<Receiver<PostOfficeEvent>>,
|
|
|
|
|
|
2025-05-03 21:45:53 -07:00
|
|
|
outgoing_messages: HashMap<ConversationID, Vec<OutgoingMessage>>,
|
|
|
|
|
|
2025-06-05 20:19:34 -07:00
|
|
|
attachment_store_sink: Option<Sender<AttachmentStoreEvent>>,
|
2025-05-15 20:11:10 -07:00
|
|
|
|
2025-04-27 12:53:45 -07:00
|
|
|
version: String,
|
2025-04-27 13:40:59 -07:00
|
|
|
database: Arc<Mutex<Database>>,
|
2025-04-27 12:53:45 -07:00
|
|
|
runtime: tokio::runtime::Runtime,
|
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)?;
|
|
|
|
|
|
2025-04-27 12:53:45 -07:00
|
|
|
// Create event channels
|
|
|
|
|
let (event_sender, event_receiver) = tokio::sync::mpsc::channel(100);
|
2025-04-27 22:44:05 -07:00
|
|
|
let (signal_sender, signal_receiver) = tokio::sync::mpsc::channel(100);
|
2025-05-02 14:22:43 -07:00
|
|
|
let (post_office_sink, post_office_source) = tokio::sync::mpsc::channel(100);
|
2025-05-03 21:45:53 -07:00
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
// Create background task runtime
|
2025-04-27 12:53:45 -07:00
|
|
|
let runtime = tokio::runtime::Builder::new_multi_thread()
|
|
|
|
|
.enable_all()
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2025-04-27 13:40:59 -07:00
|
|
|
let database_impl = Database::new(&database_path.to_string_lossy())?;
|
|
|
|
|
let database = Arc::new(Mutex::new(database_impl));
|
2025-05-15 20:11:10 -07:00
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
|
version: "0.1.0".to_string(),
|
|
|
|
|
database,
|
|
|
|
|
event_receiver,
|
|
|
|
|
event_sender,
|
2025-04-27 22:44:05 -07:00
|
|
|
signal_receiver: Some(signal_receiver),
|
2025-05-15 20:11:10 -07:00
|
|
|
signal_sender,
|
2025-05-02 14:22:43 -07:00
|
|
|
post_office_sink,
|
|
|
|
|
post_office_source: Some(post_office_source),
|
2025-05-03 21:45:53 -07:00
|
|
|
outgoing_messages: HashMap::new(),
|
2025-06-05 20:19:34 -07:00
|
|
|
attachment_store_sink: None,
|
2025-05-15 20:11:10 -07:00
|
|
|
runtime,
|
2025-04-27 22:44:05 -07:00
|
|
|
})
|
2025-04-27 12:53:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn run(&mut self) {
|
2025-04-28 16:00:04 -07:00
|
|
|
log::info!("Starting daemon version {}", self.version);
|
|
|
|
|
log::debug!("Debug logging enabled.");
|
|
|
|
|
|
2025-05-02 14:22:43 -07:00
|
|
|
// Update monitor
|
2025-05-15 20:11:10 -07:00
|
|
|
let mut update_monitor =
|
|
|
|
|
UpdateMonitor::new(self.database.clone(), self.event_sender.clone());
|
2025-05-01 20:36:43 -07:00
|
|
|
tokio::spawn(async move {
|
|
|
|
|
update_monitor.run().await; // should run indefinitely
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-02 14:22:43 -07:00
|
|
|
// Post office
|
|
|
|
|
{
|
|
|
|
|
let mut database = self.database.clone();
|
|
|
|
|
let event_sender = self.event_sender.clone();
|
|
|
|
|
let post_office_source = self.post_office_source.take().unwrap();
|
|
|
|
|
tokio::spawn(async move {
|
2025-05-15 20:11:10 -07:00
|
|
|
let mut post_office =
|
|
|
|
|
PostOffice::new(post_office_source, event_sender, async move || {
|
|
|
|
|
Self::get_client_impl(&mut database).await
|
|
|
|
|
});
|
2025-05-02 14:22:43 -07:00
|
|
|
post_office.run().await;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-05 20:19:34 -07:00
|
|
|
// Attachment store
|
2025-06-06 16:28:29 -07:00
|
|
|
let mut attachment_store = AttachmentStore::new(self.database.clone(), self.event_sender.clone());
|
2025-06-05 20:19:34 -07:00
|
|
|
self.attachment_store_sink = Some(attachment_store.get_event_sink());
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
attachment_store.run().await;
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-27 12:53:45 -07:00
|
|
|
while let Some(event) = self.event_receiver.recv().await {
|
2025-04-28 16:00:04 -07:00
|
|
|
log::debug!(target: target::EVENT, "Received event: {:?}", event);
|
2025-04-27 12:53:45 -07:00
|
|
|
self.handle_event(event).await;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn handle_event(&mut self, event: Event) {
|
|
|
|
|
match event {
|
|
|
|
|
Event::GetVersion(reply) => {
|
|
|
|
|
reply.send(self.version.clone()).unwrap();
|
2025-05-15 20:11:10 -07:00
|
|
|
}
|
2025-04-27 12:53:45 -07:00
|
|
|
|
2025-05-01 20:36:43 -07:00
|
|
|
Event::SyncConversationList(reply) => {
|
|
|
|
|
let mut db_clone = self.database.clone();
|
|
|
|
|
let signal_sender = self.signal_sender.clone();
|
|
|
|
|
self.runtime.spawn(async move {
|
|
|
|
|
let result = Self::sync_conversation_list(&mut db_clone, &signal_sender).await;
|
|
|
|
|
if let Err(e) = result {
|
2025-05-01 20:45:20 -07:00
|
|
|
log::error!(target: target::SYNC, "Error handling sync event: {}", e);
|
2025-05-01 20:36:43 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
// This is a background operation, so return right away.
|
2025-05-01 20:36:43 -07:00
|
|
|
reply.send(()).unwrap();
|
2025-05-15 20:11:10 -07:00
|
|
|
}
|
2025-05-01 20:36:43 -07:00
|
|
|
|
2025-04-27 12:53:45 -07:00
|
|
|
Event::SyncAllConversations(reply) => {
|
2025-04-27 23:27:21 -07:00
|
|
|
let mut db_clone = self.database.clone();
|
2025-04-27 22:44:05 -07:00
|
|
|
let signal_sender = self.signal_sender.clone();
|
2025-04-27 13:40:59 -07:00
|
|
|
self.runtime.spawn(async move {
|
2025-05-15 20:11:10 -07:00
|
|
|
let result =
|
|
|
|
|
Self::sync_all_conversations_impl(&mut db_clone, &signal_sender).await;
|
2025-04-27 13:40:59 -07:00
|
|
|
if let Err(e) = result {
|
2025-05-01 20:45:20 -07:00
|
|
|
log::error!(target: target::SYNC, "Error handling sync event: {}", e);
|
2025-04-27 13:40:59 -07:00
|
|
|
}
|
2025-04-27 12:53:45 -07:00
|
|
|
});
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
// This is a background operation, so return right away.
|
2025-04-27 12:53:45 -07:00
|
|
|
reply.send(()).unwrap();
|
2025-05-15 20:11:10 -07:00
|
|
|
}
|
2025-04-27 12:53:45 -07:00
|
|
|
|
2025-04-28 18:39:52 -07:00
|
|
|
Event::SyncConversation(conversation_id, reply) => {
|
|
|
|
|
let mut db_clone = self.database.clone();
|
|
|
|
|
let signal_sender = self.signal_sender.clone();
|
|
|
|
|
self.runtime.spawn(async move {
|
2025-05-15 20:11:10 -07:00
|
|
|
let result = Self::sync_conversation_impl(
|
|
|
|
|
&mut db_clone,
|
|
|
|
|
&signal_sender,
|
|
|
|
|
conversation_id,
|
|
|
|
|
)
|
|
|
|
|
.await;
|
2025-04-28 18:39:52 -07:00
|
|
|
if let Err(e) = result {
|
2025-05-01 20:45:20 -07:00
|
|
|
log::error!(target: target::SYNC, "Error handling sync event: {}", e);
|
2025-04-28 18:39:52 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
reply.send(()).unwrap();
|
2025-05-15 20:11:10 -07:00
|
|
|
}
|
2025-04-28 18:39:52 -07:00
|
|
|
|
2025-05-03 18:19:48 -07:00
|
|
|
Event::GetAllConversations(limit, offset, reply) => {
|
|
|
|
|
let conversations = self.get_conversations_limit_offset(limit, offset).await;
|
2025-04-27 12:53:45 -07:00
|
|
|
reply.send(conversations).unwrap();
|
2025-05-15 20:11:10 -07:00
|
|
|
}
|
2025-04-27 18:07:58 -07:00
|
|
|
|
|
|
|
|
Event::GetAllSettings(reply) => {
|
2025-05-15 20:11:10 -07:00
|
|
|
let settings = self.get_settings().await.unwrap_or_else(|e| {
|
|
|
|
|
log::error!(target: target::SETTINGS, "Failed to get settings: {:#?}", e);
|
|
|
|
|
Settings::default()
|
|
|
|
|
});
|
2025-04-27 18:07:58 -07:00
|
|
|
|
|
|
|
|
reply.send(settings).unwrap();
|
2025-05-15 20:11:10 -07:00
|
|
|
}
|
2025-04-27 18:07:58 -07:00
|
|
|
|
|
|
|
|
Event::UpdateSettings(settings, reply) => {
|
2025-05-15 20:11:10 -07:00
|
|
|
self.update_settings(&settings).await.unwrap_or_else(|e| {
|
|
|
|
|
log::error!(target: target::SETTINGS, "Failed to update settings: {}", e);
|
|
|
|
|
});
|
2025-04-27 18:07:58 -07:00
|
|
|
|
|
|
|
|
reply.send(()).unwrap();
|
2025-05-15 20:11:10 -07:00
|
|
|
}
|
2025-04-28 16:00:04 -07:00
|
|
|
|
|
|
|
|
Event::GetMessages(conversation_id, last_message_id, reply) => {
|
|
|
|
|
let messages = self.get_messages(conversation_id, last_message_id).await;
|
|
|
|
|
reply.send(messages).unwrap();
|
2025-05-15 20:11:10 -07:00
|
|
|
}
|
2025-05-01 01:08:13 -07:00
|
|
|
|
|
|
|
|
Event::DeleteAllConversations(reply) => {
|
2025-05-15 20:11:10 -07:00
|
|
|
self.delete_all_conversations().await.unwrap_or_else(|e| {
|
|
|
|
|
log::error!(target: target::SYNC, "Failed to delete all conversations: {}", e);
|
|
|
|
|
});
|
2025-05-01 01:08:13 -07:00
|
|
|
|
|
|
|
|
reply.send(()).unwrap();
|
2025-05-15 20:11:10 -07:00
|
|
|
}
|
2025-05-02 14:22:43 -07:00
|
|
|
|
|
|
|
|
Event::SendMessage(conversation_id, text, reply) => {
|
2025-05-03 21:45:53 -07:00
|
|
|
let conversation_id = conversation_id.clone();
|
2025-05-15 20:11:10 -07:00
|
|
|
let uuid = self
|
|
|
|
|
.enqueue_outgoing_message(text, conversation_id.clone())
|
|
|
|
|
.await;
|
2025-05-02 14:22:43 -07:00
|
|
|
reply.send(uuid).unwrap();
|
2025-05-03 21:45:53 -07:00
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
// Send message updated signal, we have a placeholder message we will return.
|
|
|
|
|
self.signal_sender
|
|
|
|
|
.send(Signal::MessagesUpdated(conversation_id.clone()))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
2025-05-03 21:45:53 -07:00
|
|
|
|
|
|
|
|
Event::MessageSent(message, outgoing_message, conversation_id) => {
|
|
|
|
|
log::info!(target: target::EVENT, "Daemon: message sent: {}", message.id);
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
// Insert the message into the database.
|
2025-05-03 21:45:53 -07:00
|
|
|
log::debug!(target: target::EVENT, "inserting sent message into database: {}", message.id);
|
2025-05-15 20:11:10 -07:00
|
|
|
self.database
|
|
|
|
|
.lock()
|
|
|
|
|
.await
|
2025-06-06 16:28:29 -07:00
|
|
|
.with_repository(|r| r.insert_message(&conversation_id, message.into()))
|
2025-05-15 20:11:10 -07:00
|
|
|
.await
|
|
|
|
|
.unwrap();
|
2025-05-03 21:45:53 -07:00
|
|
|
|
|
|
|
|
// Remove from outgoing messages.
|
|
|
|
|
log::debug!(target: target::EVENT, "Removing message from outgoing messages: {}", outgoing_message.guid);
|
2025-05-15 20:11:10 -07:00
|
|
|
self.outgoing_messages
|
|
|
|
|
.get_mut(&conversation_id)
|
2025-05-03 21:45:53 -07:00
|
|
|
.map(|messages| messages.retain(|m| m.guid != outgoing_message.guid));
|
|
|
|
|
|
|
|
|
|
// Send message updated signal.
|
2025-05-15 20:11:10 -07:00
|
|
|
self.signal_sender
|
|
|
|
|
.send(Signal::MessagesUpdated(conversation_id))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
2025-05-25 18:52:18 -07:00
|
|
|
|
|
|
|
|
Event::GetAttachment(guid, reply) => {
|
2025-06-05 20:19:34 -07:00
|
|
|
self.attachment_store_sink
|
|
|
|
|
.as_ref()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.send(AttachmentStoreEvent::GetAttachmentInfo(guid, reply))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
2025-05-25 18:52:18 -07:00
|
|
|
}
|
2025-05-26 16:19:26 -07:00
|
|
|
|
2025-06-05 20:19:34 -07:00
|
|
|
Event::DownloadAttachment(attachment_id, preview, reply) => {
|
|
|
|
|
log::info!(target: target::ATTACHMENTS, "Download requested for attachment: {}, preview: {}", &attachment_id, preview);
|
|
|
|
|
|
|
|
|
|
self.attachment_store_sink
|
|
|
|
|
.as_ref()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.send(AttachmentStoreEvent::QueueDownloadAttachment(attachment_id, preview))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2025-05-26 16:19:26 -07:00
|
|
|
reply.send(()).unwrap();
|
|
|
|
|
}
|
2025-04-27 12:53:45 -07:00
|
|
|
}
|
2025-04-25 20:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
/// Panics if the signal receiver has already been taken.
|
|
|
|
|
pub fn obtain_signal_receiver(&mut self) -> Receiver<Signal> {
|
2025-04-27 22:44:05 -07:00
|
|
|
self.signal_receiver.take().unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-27 13:40:59 -07:00
|
|
|
async fn get_conversations(&mut self) -> Vec<Conversation> {
|
2025-05-15 20:11:10 -07:00
|
|
|
self.database
|
|
|
|
|
.lock()
|
|
|
|
|
.await
|
|
|
|
|
.with_repository(|r| r.all_conversations(i32::MAX, 0).unwrap())
|
|
|
|
|
.await
|
2025-05-03 18:19:48 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
async fn get_conversations_limit_offset(
|
|
|
|
|
&mut self,
|
|
|
|
|
limit: i32,
|
|
|
|
|
offset: i32,
|
|
|
|
|
) -> Vec<Conversation> {
|
|
|
|
|
self.database
|
|
|
|
|
.lock()
|
|
|
|
|
.await
|
|
|
|
|
.with_repository(|r| r.all_conversations(limit, offset).unwrap())
|
|
|
|
|
.await
|
2025-04-25 20:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
async fn get_messages(
|
|
|
|
|
&mut self,
|
|
|
|
|
conversation_id: String,
|
|
|
|
|
last_message_id: Option<String>,
|
|
|
|
|
) -> Vec<Message> {
|
|
|
|
|
// Get outgoing messages for this conversation.
|
2025-05-03 21:45:53 -07:00
|
|
|
let empty_vec: Vec<OutgoingMessage> = vec![];
|
2025-05-15 20:11:10 -07:00
|
|
|
let outgoing_messages: &Vec<OutgoingMessage> = self
|
|
|
|
|
.outgoing_messages
|
|
|
|
|
.get(&conversation_id)
|
2025-05-03 21:45:53 -07:00
|
|
|
.unwrap_or(&empty_vec);
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
self.database
|
|
|
|
|
.lock()
|
|
|
|
|
.await
|
|
|
|
|
.with_repository(|r| {
|
2025-05-03 21:45:53 -07:00
|
|
|
r.get_messages_for_conversation(&conversation_id)
|
2025-05-15 20:11:10 -07:00
|
|
|
.unwrap()
|
|
|
|
|
.into_iter()
|
2025-06-06 16:28:29 -07:00
|
|
|
.map(|m| m.into()) // Convert db::Message to daemon::Message
|
2025-05-15 20:11:10 -07:00
|
|
|
.chain(outgoing_messages.into_iter().map(|m| m.into()))
|
|
|
|
|
.collect()
|
|
|
|
|
})
|
2025-05-03 21:45:53 -07:00
|
|
|
.await
|
2025-04-28 16:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-02 14:22:43 -07:00
|
|
|
async fn enqueue_outgoing_message(&mut self, text: String, conversation_id: String) -> Uuid {
|
2025-05-03 21:45:53 -07:00
|
|
|
let conversation_id = conversation_id.clone();
|
2025-05-02 14:22:43 -07:00
|
|
|
let outgoing_message = OutgoingMessage::builder()
|
|
|
|
|
.text(text)
|
2025-05-03 21:45:53 -07:00
|
|
|
.conversation_id(conversation_id.clone())
|
2025-05-02 14:22:43 -07:00
|
|
|
.build();
|
|
|
|
|
|
2025-05-03 21:45:53 -07:00
|
|
|
// Keep a record of this so we can provide a consistent model to the client.
|
2025-05-15 20:11:10 -07:00
|
|
|
self.outgoing_messages
|
|
|
|
|
.entry(conversation_id)
|
2025-05-03 21:45:53 -07:00
|
|
|
.or_insert(vec![])
|
|
|
|
|
.push(outgoing_message.clone());
|
|
|
|
|
|
2025-05-02 14:22:43 -07:00
|
|
|
let guid = outgoing_message.guid.clone();
|
2025-05-15 20:11:10 -07:00
|
|
|
self.post_office_sink
|
|
|
|
|
.send(PostOfficeEvent::EnqueueOutgoingMessage(outgoing_message))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
2025-05-02 15:46:33 -07:00
|
|
|
|
2025-05-02 14:22:43 -07:00
|
|
|
guid
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
async fn sync_conversation_list(
|
|
|
|
|
database: &mut Arc<Mutex<Database>>,
|
|
|
|
|
signal_sender: &Sender<Signal>,
|
|
|
|
|
) -> Result<()> {
|
2025-05-01 20:36:43 -07:00
|
|
|
log::info!(target: target::SYNC, "Starting list conversation sync");
|
|
|
|
|
|
|
|
|
|
let mut client = Self::get_client_impl(database).await?;
|
|
|
|
|
|
|
|
|
|
// Fetch conversations from server
|
|
|
|
|
let fetched_conversations = client.get_conversations().await?;
|
2025-05-15 20:11:10 -07:00
|
|
|
let db_conversations: Vec<kordophone_db::models::Conversation> = fetched_conversations
|
|
|
|
|
.into_iter()
|
2025-05-01 20:36:43 -07:00
|
|
|
.map(kordophone_db::models::Conversation::from)
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
// Insert each conversation
|
|
|
|
|
let num_conversations = db_conversations.len();
|
|
|
|
|
for conversation in db_conversations {
|
2025-05-15 20:11:10 -07:00
|
|
|
database
|
|
|
|
|
.with_repository(|r| r.insert_conversation(conversation))
|
|
|
|
|
.await?;
|
2025-05-01 20:36:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send conversations updated signal
|
|
|
|
|
signal_sender.send(Signal::ConversationsUpdated).await?;
|
|
|
|
|
|
2025-05-02 15:46:33 -07:00
|
|
|
log::info!(target: target::SYNC, "List synchronized: {} conversations", num_conversations);
|
2025-05-01 20:36:43 -07:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
async fn sync_all_conversations_impl(
|
|
|
|
|
database: &mut Arc<Mutex<Database>>,
|
|
|
|
|
signal_sender: &Sender<Signal>,
|
|
|
|
|
) -> Result<()> {
|
2025-05-01 20:36:43 -07:00
|
|
|
log::info!(target: target::SYNC, "Starting full conversation sync");
|
2025-04-27 12:53:45 -07:00
|
|
|
|
2025-04-27 23:27:21 -07:00
|
|
|
let mut client = Self::get_client_impl(database).await?;
|
2025-05-15 20:11:10 -07:00
|
|
|
|
2025-04-27 13:40:59 -07:00
|
|
|
// Fetch conversations from server
|
2025-04-25 20:02:18 -07:00
|
|
|
let fetched_conversations = client.get_conversations().await?;
|
2025-05-15 20:11:10 -07:00
|
|
|
let db_conversations: Vec<kordophone_db::models::Conversation> = fetched_conversations
|
|
|
|
|
.into_iter()
|
2025-04-28 16:06:51 -07:00
|
|
|
.map(kordophone_db::models::Conversation::from)
|
2025-04-25 20:02:18 -07:00
|
|
|
.collect();
|
2025-05-15 20:11:10 -07:00
|
|
|
|
2025-04-25 20:02:18 -07:00
|
|
|
// Process each conversation
|
2025-04-27 18:07:58 -07:00
|
|
|
let num_conversations = db_conversations.len();
|
2025-04-25 20:02:18 -07:00
|
|
|
for conversation in db_conversations {
|
|
|
|
|
let conversation_id = conversation.guid.clone();
|
2025-05-15 20:11:10 -07:00
|
|
|
|
2025-04-25 20:02:18 -07:00
|
|
|
// Insert the conversation
|
2025-05-15 20:11:10 -07:00
|
|
|
database
|
|
|
|
|
.with_repository(|r| r.insert_conversation(conversation))
|
|
|
|
|
.await?;
|
|
|
|
|
|
2025-04-28 18:39:52 -07:00
|
|
|
// Sync individual conversation.
|
|
|
|
|
Self::sync_conversation_impl(database, signal_sender, conversation_id).await?;
|
2025-04-27 13:40:59 -07:00
|
|
|
}
|
2025-04-27 18:07:58 -07:00
|
|
|
|
2025-04-27 22:44:05 -07:00
|
|
|
// Send conversations updated signal.
|
|
|
|
|
signal_sender.send(Signal::ConversationsUpdated).await?;
|
|
|
|
|
|
2025-05-02 15:46:33 -07:00
|
|
|
log::info!(target: target::SYNC, "Full sync complete, {} conversations processed", num_conversations);
|
2025-04-25 20:02:18 -07:00
|
|
|
Ok(())
|
2025-05-15 20:11:10 -07:00
|
|
|
}
|
2025-04-25 16:54:37 -07:00
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
async fn sync_conversation_impl(
|
|
|
|
|
database: &mut Arc<Mutex<Database>>,
|
|
|
|
|
signal_sender: &Sender<Signal>,
|
|
|
|
|
conversation_id: String,
|
|
|
|
|
) -> Result<()> {
|
2025-05-03 22:13:03 -07:00
|
|
|
log::debug!(target: target::SYNC, "Starting conversation sync for {}", conversation_id);
|
2025-04-28 18:39:52 -07:00
|
|
|
|
|
|
|
|
let mut client = Self::get_client_impl(database).await?;
|
|
|
|
|
|
2025-05-01 20:36:43 -07:00
|
|
|
// Check if conversation exists in database.
|
2025-05-15 20:11:10 -07:00
|
|
|
let conversation = database
|
|
|
|
|
.with_repository(|r| r.get_conversation_by_guid(&conversation_id))
|
|
|
|
|
.await?;
|
2025-05-01 20:36:43 -07:00
|
|
|
if conversation.is_none() {
|
2025-05-15 20:11:10 -07:00
|
|
|
// If the conversation doesn't exist, first do a conversation list sync.
|
2025-05-02 15:46:33 -07:00
|
|
|
log::warn!(target: target::SYNC, "Conversation {} not found, performing list sync", conversation_id);
|
2025-05-01 20:36:43 -07:00
|
|
|
Self::sync_conversation_list(database, signal_sender).await?;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-28 18:39:52 -07:00
|
|
|
// Fetch and sync messages for this conversation
|
2025-05-15 20:11:10 -07:00
|
|
|
let last_message_id = database
|
|
|
|
|
.with_repository(|r| -> Option<String> {
|
|
|
|
|
r.get_last_message_for_conversation(&conversation_id)
|
|
|
|
|
.unwrap_or(None)
|
|
|
|
|
.map(|m| m.id)
|
|
|
|
|
})
|
|
|
|
|
.await;
|
2025-04-28 18:39:52 -07:00
|
|
|
|
|
|
|
|
log::debug!(target: target::SYNC, "Fetching messages for conversation {}", &conversation_id);
|
|
|
|
|
log::debug!(target: target::SYNC, "Last message id: {:?}", last_message_id);
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
let messages = client
|
|
|
|
|
.get_messages(&conversation_id, None, None, last_message_id)
|
|
|
|
|
.await?;
|
|
|
|
|
let db_messages: Vec<kordophone_db::models::Message> = messages
|
|
|
|
|
.into_iter()
|
2025-04-28 18:39:52 -07:00
|
|
|
.map(kordophone_db::models::Message::from)
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
// Insert each message
|
|
|
|
|
let num_messages = db_messages.len();
|
|
|
|
|
log::debug!(target: target::SYNC, "Inserting {} messages for conversation {}", num_messages, &conversation_id);
|
2025-05-15 20:11:10 -07:00
|
|
|
database
|
|
|
|
|
.with_repository(|r| r.insert_messages(&conversation_id, db_messages))
|
|
|
|
|
.await?;
|
2025-04-28 18:39:52 -07:00
|
|
|
|
|
|
|
|
// Send messages updated signal, if we actually inserted any messages.
|
|
|
|
|
if num_messages > 0 {
|
2025-05-15 20:11:10 -07:00
|
|
|
signal_sender
|
|
|
|
|
.send(Signal::MessagesUpdated(conversation_id.clone()))
|
|
|
|
|
.await?;
|
2025-04-28 18:39:52 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-03 22:13:03 -07:00
|
|
|
log::debug!(target: target::SYNC, "Synchronized {} messages for conversation {}", num_messages, &conversation_id);
|
2025-04-28 18:39:52 -07:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-27 13:40:59 -07:00
|
|
|
async fn get_settings(&mut self) -> Result<Settings> {
|
2025-05-01 01:02:36 -07:00
|
|
|
let settings = self.database.with_settings(Settings::from_db).await?;
|
2025-04-25 20:02:18 -07:00
|
|
|
Ok(settings)
|
2025-04-25 16:54:37 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-27 23:27:21 -07:00
|
|
|
async fn update_settings(&mut self, settings: &Settings) -> Result<()> {
|
2025-04-27 18:07:58 -07:00
|
|
|
self.database.with_settings(|s| settings.save(s)).await
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 01:02:36 -07:00
|
|
|
async fn get_client(&mut self) -> Result<HTTPAPIClient<DatabaseAuthenticationStore>> {
|
2025-04-27 23:27:21 -07:00
|
|
|
Self::get_client_impl(&mut self.database).await
|
2025-04-27 18:07:58 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
async fn get_client_impl(
|
|
|
|
|
database: &mut Arc<Mutex<Database>>,
|
|
|
|
|
) -> Result<HTTPAPIClient<DatabaseAuthenticationStore>> {
|
2025-05-01 01:02:36 -07:00
|
|
|
let settings = database.with_settings(Settings::from_db).await?;
|
2025-04-25 20:02:18 -07:00
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
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(),
|
2025-05-15 20:11:10 -07:00
|
|
|
DatabaseAuthenticationStore::new(database.clone()),
|
2025-04-25 20:02:18 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Ok(client)
|
2025-04-25 18:02:54 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-01 01:08:13 -07:00
|
|
|
async fn delete_all_conversations(&mut self) -> Result<()> {
|
2025-05-15 20:11:10 -07:00
|
|
|
self.database
|
|
|
|
|
.with_repository(|r| -> Result<()> {
|
|
|
|
|
r.delete_all_conversations()?;
|
|
|
|
|
r.delete_all_messages()?;
|
|
|
|
|
Ok(())
|
|
|
|
|
})
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
self.signal_sender
|
|
|
|
|
.send(Signal::ConversationsUpdated)
|
|
|
|
|
.await?;
|
2025-05-01 01:08:13 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 20:11:10 -07:00
|
|
|
fn get_data_dir() -> Option<PathBuf> {
|
|
|
|
|
ProjectDirs::from("net", "buzzert", "kordophonecd").map(|p| PathBuf::from(p.data_dir()))
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-25 16:54:37 -07:00
|
|
|
fn get_database_path() -> PathBuf {
|
2025-05-15 20:11:10 -07:00
|
|
|
if let Some(data_dir) = Self::get_data_dir() {
|
2025-04-25 16:54:37 -07:00
|
|
|
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
|
|
|
}
|