diff options
Diffstat (limited to 'src/sync/relay_connection.rs')
| -rw-r--r-- | src/sync/relay_connection.rs | 28 |
1 files changed, 7 insertions, 21 deletions
diff --git a/src/sync/relay_connection.rs b/src/sync/relay_connection.rs index fdb32cb..fa229c4 100644 --- a/src/sync/relay_connection.rs +++ b/src/sync/relay_connection.rs | |||
| @@ -18,7 +18,6 @@ | |||
| 18 | use nostr_sdk::prelude::*; | 18 | use nostr_sdk::prelude::*; |
| 19 | use tokio::sync::mpsc; | 19 | use tokio::sync::mpsc; |
| 20 | 20 | ||
| 21 | use super::filters::build_announcement_filter; | ||
| 22 | use crate::nostr::builder::SharedDatabase; | 21 | use crate::nostr::builder::SharedDatabase; |
| 23 | 22 | ||
| 24 | /// Events from a relay connection | 23 | /// Events from a relay connection |
| @@ -107,28 +106,24 @@ impl RelayConnection { | |||
| 107 | } | 106 | } |
| 108 | } | 107 | } |
| 109 | 108 | ||
| 110 | /// Connect to the relay and subscribe to Layer 1 (announcements) | 109 | /// Connect to the relay |
| 111 | /// | 110 | /// |
| 112 | /// This method: | 111 | /// This method: |
| 113 | /// 1. Adds the relay to the client | 112 | /// 1. Adds the relay to the client |
| 114 | /// 2. Establishes the WebSocket connection | 113 | /// 2. Establishes the WebSocket connection |
| 115 | /// 3. Verifies connection was established | 114 | /// 3. Verifies connection was established |
| 116 | /// 4. Subscribes to Layer 1 filter (kinds 30617 + 30618) | 115 | /// |
| 116 | /// Subscriptions are handled separately via handle_connect_or_reconnect. | ||
| 117 | /// | 117 | /// |
| 118 | /// # Arguments | 118 | /// # Arguments |
| 119 | /// * `since` - Optional timestamp for incremental sync on reconnect | ||
| 120 | /// * `connection_timeout_secs` - Timeout for the connection attempt in seconds. | 119 | /// * `connection_timeout_secs` - Timeout for the connection attempt in seconds. |
| 121 | /// Should be no larger than base_backoff_secs to ensure the connection attempt | 120 | /// Should be no larger than base_backoff_secs to ensure the connection attempt |
| 122 | /// completes before the next retry would be scheduled. | 121 | /// completes before the next retry would be scheduled. |
| 123 | /// | 122 | /// |
| 124 | /// # Returns | 123 | /// # Returns |
| 125 | /// * `Ok(SubscriptionId)` - The subscription ID on successful connection | 124 | /// * `Ok(())` - Connection established successfully |
| 126 | /// * `Err(String)` with error description on failure | 125 | /// * `Err(String)` with error description on failure |
| 127 | pub async fn connect_and_subscribe( | 126 | pub async fn connect(&self, connection_timeout_secs: u64) -> Result<(), String> { |
| 128 | &self, | ||
| 129 | since: Option<Timestamp>, | ||
| 130 | connection_timeout_secs: u64, | ||
| 131 | ) -> Result<SubscriptionId, String> { | ||
| 132 | // Add relay to client | 127 | // Add relay to client |
| 133 | self.client | 128 | self.client |
| 134 | .add_relay(&self.url) | 129 | .add_relay(&self.url) |
| @@ -157,17 +152,8 @@ impl RelayConnection { | |||
| 157 | .await | 152 | .await |
| 158 | .map_err(|e| format!("Failed to connect to relay {}: {}", self.url, e))?; | 153 | .map_err(|e| format!("Failed to connect to relay {}: {}", self.url, e))?; |
| 159 | 154 | ||
| 160 | // Subscribe to Layer 1 (announcements) | 155 | tracing::info!(url = %self.url, "Connected to relay"); |
| 161 | let filter = build_announcement_filter(since); | 156 | Ok(()) |
| 162 | let output = self.client.subscribe(filter, None).await.map_err(|e| { | ||
| 163 | format!( | ||
| 164 | "Failed to subscribe to announcements on {}: {}", | ||
| 165 | self.url, e | ||
| 166 | ) | ||
| 167 | })?; | ||
| 168 | |||
| 169 | tracing::info!(url = %self.url, sub_id = %output.val, "Connected and subscribed to Layer 1 (announcements)"); | ||
| 170 | Ok(output.val) | ||
| 171 | } | 157 | } |
| 172 | 158 | ||
| 173 | /// Run the event loop, sending events through the provided channel | 159 | /// Run the event loop, sending events through the provided channel |