Private
Public Access
1
0

daemon: add support for getting messages from db

This commit is contained in:
2025-04-28 16:00:04 -07:00
parent 9c245a5b52
commit c189e5f9e3
11 changed files with 267 additions and 15 deletions

View File

@@ -19,7 +19,7 @@ use async_trait::async_trait;
use kordophone_db::{
database::{Database, DatabaseAccess},
models::Conversation,
models::{Conversation, Message},
};
use kordophone::model::JwtToken;
@@ -54,6 +54,7 @@ impl TokenStore for DatabaseTokenStore {
mod target {
pub static SYNC: &str = "sync";
pub static EVENT: &str = "event";
}
pub struct Daemon {
@@ -100,7 +101,11 @@ impl Daemon {
}
pub async fn run(&mut self) {
log::info!("Starting daemon version {}", self.version);
log::debug!("Debug logging enabled.");
while let Some(event) = self.event_receiver.recv().await {
log::debug!(target: target::EVENT, "Received event: {:?}", event);
self.handle_event(event).await;
}
}
@@ -148,6 +153,11 @@ impl Daemon {
reply.send(()).unwrap();
},
Event::GetMessages(conversation_id, last_message_id, reply) => {
let messages = self.get_messages(conversation_id, last_message_id).await;
reply.send(messages).unwrap();
},
}
}
@@ -160,6 +170,10 @@ impl Daemon {
self.database.lock().await.with_repository(|r| r.all_conversations().unwrap()).await
}
async fn get_messages(&mut self, conversation_id: String, last_message_id: Option<String>) -> Vec<Message> {
self.database.lock().await.with_repository(|r| r.get_messages_for_conversation(&conversation_id).unwrap()).await
}
async fn sync_all_conversations_impl(database: &mut Arc<Mutex<Database>>, signal_sender: &Sender<Signal>) -> Result<()> {
log::info!(target: target::SYNC, "Starting conversation sync");
@@ -180,8 +194,16 @@ impl Daemon {
database.with_repository(|r| r.insert_conversation(conversation)).await?;
// Fetch and sync messages for this conversation
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;
log::debug!(target: target::SYNC, "Fetching messages for conversation {}", conversation_id);
let messages = client.get_messages(&conversation_id, None, None, None).await?;
log::debug!(target: target::SYNC, "Last message id: {:?}", last_message_id);
let messages = client.get_messages(&conversation_id, None, None, last_message_id).await?;
let db_messages: Vec<kordophone_db::models::Message> = messages.into_iter()
.map(|m| kordophone_db::models::Message::from(m))
.collect();