46 lines
1.6 KiB
Rust
Raw Normal View History

use super::RpcHandler;
use crate::network::dwn_sync;
use crate::peers;
use anyhow::Result;
impl RpcHandler {
/// Get DWN status and sync state.
pub(super) async fn handle_dwn_status(&self) -> Result<serde_json::Value> {
let sync_state = dwn_sync::load_sync_state(&self.config.data_dir).await?;
let server_status = dwn_sync::get_dwn_status().await.unwrap_or(dwn_sync::DwnStatusResponse {
running: false,
version: String::new(),
});
Ok(serde_json::json!({
"running": server_status.running,
"version": server_status.version,
"sync_status": sync_state.status,
"last_sync": sync_state.last_sync,
"messages_synced": sync_state.messages_synced,
"storage_bytes": sync_state.storage_bytes,
"registered_protocols": sync_state.registered_protocols,
"peer_sync_targets": sync_state.peer_sync_targets,
}))
}
/// Trigger DWN sync with connected peers.
pub(super) async fn handle_dwn_sync(&self) -> Result<serde_json::Value> {
// Get list of connected peers' onion addresses
let peer_list = peers::load_peers(&self.config.data_dir).await?;
let onions: Vec<String> = peer_list
.iter()
.filter(|p| !p.onion.is_empty())
.map(|p| p.onion.clone())
.collect();
let state = dwn_sync::sync_with_peers(&self.config.data_dir, &onions).await?;
Ok(serde_json::json!({
"sync_status": state.status,
"last_sync": state.last_sync,
"messages_synced": state.messages_synced,
}))
}
}