Connect relays individually and sync bootstrap state

This commit is contained in:
2026-09-10 07:44:07 +07:00
parent 3cfdffdf82
commit 18433ec239
2 changed files with 58 additions and 50 deletions
+21 -48
View File
@@ -160,15 +160,15 @@ impl Backend {
let task = cx.background_spawn(async move {
for url in BOOTSTRAP_RELAYS {
client.add_relay(url).await?;
client.add_relay(url).and_connect().await?;
}
for url in INDEXER_RELAYS {
client
.add_relay(url)
.capabilities(RelayCapabilities::DISCOVERY)
.and_connect()
.await?;
}
client.connect().await;
Ok::<(), Error>(())
});
@@ -425,6 +425,7 @@ impl Backend {
let path = cache.repo_path(&addr);
let owner = public_key.to_bech32().unwrap();
let servers = grasp_servers.clone();
let client = self.client.clone();
cx.spawn(async move |this, cx| {
// Initialize the local clone and create the user's working copy from it.
@@ -488,10 +489,9 @@ impl Backend {
let commit_sha = Sha1Hash::from_str(&commit).map_err(|_| anyhow!("invalid id"))?;
// The nostr client queues events until each relay is connected.
this.update(cx, |this, cx| {
let urls: Vec<String> = servers.iter().map(ToString::to_string).collect();
this.add_relays(urls, cx);
})?;
for url in &servers {
client.add_relay(url).and_connect().await.ok();
}
// The state event is the push authorization. It must be accepted before the push below.
let announcement = GitRepositoryAnnouncement {
@@ -622,6 +622,7 @@ impl Backend {
let owner = public_key.to_bech32().unwrap();
let servers = grasp_servers.clone();
let client = self.client.clone();
cx.spawn(async move |this, cx| {
let work = cx.background_spawn({
@@ -635,10 +636,9 @@ impl Backend {
let (state, euc) = work.await?;
// The nostr client queues events until each relay is connected.
this.update(cx, |this, cx| {
let urls: Vec<String> = servers.iter().map(ToString::to_string).collect();
this.add_relays(urls, cx);
})?;
for url in &servers {
client.add_relay(url).and_connect().await.ok();
}
// The state event is the push authorization. It must be accepted before the push below.
let announcement = GitRepositoryAnnouncement {
@@ -1045,22 +1045,22 @@ impl Backend {
task.detach();
}
/// Fetch the user's grasp list and add the listed grasp servers as relays.
/// Sync the user's grasp list and add the listed grasp servers as relays.
fn bootstrap_user(&mut self, public_key: PublicKey, cx: &mut Context<Self>) {
let client = self.client.clone();
let task: Task<Result<(), Error>> = cx.spawn(async move |this, cx| {
let result = async {
let events: Vec<Event> = client
.fetch_events(filters::grasp_list(public_key))
.await?
.into_iter()
.collect();
sync_bootstrap_only(
&client,
filters::grasp_list(public_key),
SyncOptions::default(),
)
.await?;
for url in latest_grasp_list_servers(events) {
client.add_relay(url.as_str()).await.ok();
for url in user_grasp_list_servers(client.clone(), public_key).await? {
client.add_relay(url).and_connect().await.ok();
}
client.connect().await;
Ok::<_, Error>(())
}
@@ -1137,32 +1137,6 @@ impl Backend {
task.detach();
}
/// Add relays and connect to them.
pub fn add_relays(&mut self, urls: Vec<String>, cx: &mut Context<Self>) {
let client = self.client.clone();
let task = cx.background_spawn(async move {
for url in urls {
client.add_relay(&url).await?;
}
client.connect().await;
Ok::<(), Error>(())
});
let notify_task: Task<Result<(), Error>> = cx.spawn(async move |this, cx| {
match task.await {
Ok(()) => {
this.update(cx, |_this, cx| cx.notify())?;
}
Err(e) => {
this.update(cx, |_this, cx| cx.emit(BackendEvent::error(e.to_string())))?;
}
}
Ok(())
});
notify_task.detach();
}
/// Connect to a repository's announced relays, its NIP-34 `relays` tag.
///
/// Callers are responsible for not repeating this for relays they already
@@ -1396,8 +1370,7 @@ async fn connect_repo_relays(
// Ensure relay connections
for url in relays.iter() {
client.add_relay(url).await?;
client.connect_relay(url).await?;
client.add_relay(url).and_connect().await?;
}
// Run neg sync for each filter
@@ -1724,9 +1697,9 @@ async fn stage_event_on_relay(
) -> Result<(), String> {
client
.add_relay(relay)
.and_connect()
.await
.map_err(|e| format!("could not add relay {relay}: {e}"))?;
client.connect().await;
let output = client
.send_event(event)