From 0c6d7003956f9249c31de41ac9c62d9f6b57c886 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Thu, 6 Aug 2026 14:52:43 +0700 Subject: [PATCH] refactor --- crates/signed_nostr/src/backend.rs | 5 ----- crates/signed_nostr/src/update.rs | 9 ++------- crates/signed_state/src/repo.rs | 27 ++++++++++++++++----------- crates/signed_state/src/repo_list.rs | 9 +++++---- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/crates/signed_nostr/src/backend.rs b/crates/signed_nostr/src/backend.rs index 1a11220..04bf083 100644 --- a/crates/signed_nostr/src/backend.rs +++ b/crates/signed_nostr/src/backend.rs @@ -104,13 +104,8 @@ impl NostrBackend { /// event is immediately visible to [`NostrBackend::query`]. pub async fn send(&self, builder: EventBuilder) -> Result { let event = builder.finalize_async(&self.signer).await?; - let output = self.client.send_event(&event).await?; - // Keep our own events in the local database; the notification pump - // only fires for events received from relays. - self.client.database().save_event(&event).await?; - if output.success.is_empty() && !output.failed.is_empty() { let reasons = output .failed diff --git a/crates/signed_nostr/src/update.rs b/crates/signed_nostr/src/update.rs index 1f2222f..1b3f1de 100644 --- a/crates/signed_nostr/src/update.rs +++ b/crates/signed_nostr/src/update.rs @@ -7,7 +7,7 @@ use nostr_sdk::prelude::*; pub struct Update { pub kind: Kind, /// First `a` tag value of the event, if any (e.g. the repository coordinate). - pub coordinate: Option, + pub coordinate: Option, pub author: PublicKey, pub event_id: EventId, } @@ -15,12 +15,7 @@ pub struct Update { impl Update { /// Build an update from a received event. pub fn from_event(event: &Event) -> Self { - let coordinate = event - .tags - .iter() - .find(|t| t.kind() == "a") - .and_then(|t| t.content()) - .map(str::to_owned); + let coordinate = event.tags.coordinates().nth(0); Self { kind: event.kind, diff --git a/crates/signed_state/src/repo.rs b/crates/signed_state/src/repo.rs index 43de9ef..c98fce6 100644 --- a/crates/signed_state/src/repo.rs +++ b/crates/signed_state/src/repo.rs @@ -9,7 +9,6 @@ use crate::backend::{Backend, BackendEvent}; /// their resolved statuses. Always derived from the local database. pub struct RepoStore { addr: RepoAddr, - addr_string: String, pub announcement: Option, /// `(refname, commit-id)` pairs from the latest state announcement. pub refs: Vec<(String, String)>, @@ -29,20 +28,27 @@ pub struct RepoStore { impl RepoStore { pub fn new(addr: RepoAddr, cx: &mut Context) -> Self { - let addr_string = addr.to_string(); + let backend = Backend::global(cx); - let subscription = cx.subscribe(&Backend::global(cx), |this, _backend, event, cx| { + let subscription = cx.subscribe(&backend, |this, _backend, event, cx| { let relevant = match event { BackendEvent::NostrUpdate(update) => { - update.coordinate.as_deref() == Some(this.addr_string.as_str()) - || (update.kind == Kind::GitRepoAnnouncement - && update.author == this.addr.owner) + let coordinate = update.coordinate.as_ref() == Some(&this.addr.coordinate()); + let author = update.author == this.addr.owner; + let kind = update.kind == Kind::GitRepoAnnouncement; + + coordinate || (author && kind) } BackendEvent::Published(event) => { - event.kind == Kind::GitRepoAnnouncement && event.pubkey == this.addr.owner - || event.tags.iter().any(|t| { - t.kind() == "a" && t.content() == Some(this.addr_string.as_str()) - }) + let kind = event.kind == Kind::GitRepoAnnouncement; + let author = event.pubkey == this.addr.owner; + let coordinate = event + .tags + .coordinates() + .into_iter() + .any(|c| c == this.addr.coordinate()); + + coordinate || (kind && author) } _ => false, }; @@ -54,7 +60,6 @@ impl RepoStore { let mut store = Self { addr, - addr_string, announcement: None, refs: Vec::new(), head: None, diff --git a/crates/signed_state/src/repo_list.rs b/crates/signed_state/src/repo_list.rs index b0fdd4b..0b61c14 100644 --- a/crates/signed_state/src/repo_list.rs +++ b/crates/signed_state/src/repo_list.rs @@ -21,15 +21,16 @@ impl RepoListStore { /// Create a store. If `author` is `None`, all announcements are listed. pub fn new(author: Option, cx: &mut Context) -> Self { let backend = Backend::global(cx); + let subscription = cx.subscribe(&backend, |this, _backend, event, cx| { + let git_kind = Kind::GitRepoAnnouncement; + let relevant = match event { BackendEvent::NostrUpdate(update) => { - update.kind == Kind::GitRepoAnnouncement - && this.author.is_none_or(|a| a == update.author) + update.kind == git_kind && this.author.is_none_or(|a| a == update.author) } BackendEvent::Published(event) => { - event.kind == Kind::GitRepoAnnouncement - && this.author.is_none_or(|a| a == event.pubkey) + event.kind == git_kind && this.author.is_none_or(|a| a == event.pubkey) } BackendEvent::Synced | BackendEvent::SyncProgress { .. } => true, _ => false,