implements settings, conversation dbus encoding
This commit is contained in:
@@ -50,6 +50,10 @@ impl TokenStore for DatabaseTokenStore {
|
||||
}
|
||||
}
|
||||
|
||||
mod target {
|
||||
pub static SYNC: &str = "sync";
|
||||
}
|
||||
|
||||
pub struct Daemon {
|
||||
pub event_sender: Sender<Event>,
|
||||
event_receiver: Receiver<Event>,
|
||||
@@ -110,6 +114,25 @@ impl Daemon {
|
||||
let conversations = self.get_conversations().await;
|
||||
reply.send(conversations).unwrap();
|
||||
},
|
||||
|
||||
Event::GetAllSettings(reply) => {
|
||||
let settings = self.get_settings().await
|
||||
.unwrap_or_else(|e| {
|
||||
log::error!("Failed to get settings: {:#?}", e);
|
||||
Settings::default()
|
||||
});
|
||||
|
||||
reply.send(settings).unwrap();
|
||||
},
|
||||
|
||||
Event::UpdateSettings(settings, reply) => {
|
||||
self.update_settings(settings).await
|
||||
.unwrap_or_else(|e| {
|
||||
log::error!("Failed to update settings: {}", e);
|
||||
});
|
||||
|
||||
reply.send(()).unwrap();
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,32 +141,9 @@ impl Daemon {
|
||||
}
|
||||
|
||||
async fn sync_all_conversations_impl(mut database: Arc<Mutex<Database>>) -> Result<()> {
|
||||
log::info!("Starting conversation sync");
|
||||
|
||||
// Get client from the database
|
||||
let settings = database.with_settings(|s| Settings::from_db(s))
|
||||
.await?;
|
||||
log::info!(target: target::SYNC, "Starting conversation sync");
|
||||
|
||||
let server_url = settings.server_url
|
||||
.ok_or(DaemonError::ClientNotConfigured)?;
|
||||
|
||||
let mut client = HTTPAPIClient::new(
|
||||
server_url.parse().unwrap(),
|
||||
match (settings.username, settings.credential_item) {
|
||||
(Some(username), Some(password)) => Some(
|
||||
Credentials {
|
||||
username,
|
||||
password,
|
||||
}
|
||||
),
|
||||
_ => None,
|
||||
},
|
||||
DatabaseTokenStore { database: database.clone() }
|
||||
);
|
||||
|
||||
// This function needed to implement TokenManagement
|
||||
// let token = database.lock().await.get_token();
|
||||
// TODO: Clent.token = token
|
||||
let mut client = Self::get_client_impl(database.clone()).await?;
|
||||
|
||||
// Fetch conversations from server
|
||||
let fetched_conversations = client.get_conversations().await?;
|
||||
@@ -152,6 +152,7 @@ impl Daemon {
|
||||
.collect();
|
||||
|
||||
// Process each conversation
|
||||
let num_conversations = db_conversations.len();
|
||||
for conversation in db_conversations {
|
||||
let conversation_id = conversation.guid.clone();
|
||||
|
||||
@@ -159,21 +160,18 @@ impl Daemon {
|
||||
database.with_repository(|r| r.insert_conversation(conversation)).await?;
|
||||
|
||||
// Fetch and sync messages for this conversation
|
||||
log::info!(target: target::SYNC, "Fetching messages for conversation {}", conversation_id);
|
||||
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
|
||||
database.with_repository(|r| -> Result<()> {
|
||||
for message in db_messages {
|
||||
r.insert_message(&conversation_id, message)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}).await?;
|
||||
log::info!(target: target::SYNC, "Inserting {} messages for conversation {}", db_messages.len(), conversation_id);
|
||||
database.with_repository(|r| r.insert_messages(&conversation_id, db_messages)).await?;
|
||||
}
|
||||
|
||||
|
||||
log::info!(target: target::SYNC, "Synchronized {} conversations", num_conversations);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -185,8 +183,16 @@ impl Daemon {
|
||||
Ok(settings)
|
||||
}
|
||||
|
||||
async fn update_settings(&mut self, settings: Settings) -> Result<()> {
|
||||
self.database.with_settings(|s| settings.save(s)).await
|
||||
}
|
||||
|
||||
async fn get_client(&mut self) -> Result<HTTPAPIClient<DatabaseTokenStore>> {
|
||||
let settings = self.database.with_settings(|s|
|
||||
Self::get_client_impl(self.database.clone()).await
|
||||
}
|
||||
|
||||
async fn get_client_impl(mut database: Arc<Mutex<Database>>) -> Result<HTTPAPIClient<DatabaseTokenStore>> {
|
||||
let settings = database.with_settings(|s|
|
||||
Settings::from_db(s)
|
||||
).await?;
|
||||
|
||||
@@ -205,7 +211,7 @@ impl Daemon {
|
||||
),
|
||||
_ => None,
|
||||
},
|
||||
DatabaseTokenStore { database: self.database.clone() }
|
||||
DatabaseTokenStore { database: database.clone() }
|
||||
);
|
||||
|
||||
Ok(client)
|
||||
|
||||
Reference in New Issue
Block a user