From f73245f9c3822e78393852838f61e354c84bbe70 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Sat, 13 Sep 2025 09:18:55 +0000 Subject: [PATCH] chore: improve querying Signed-off-by: Awiteb --- src/cli/commands/issue/mod.rs | 12 +++++++- src/cli/common_commands.rs | 7 ++--- src/cli/types.rs | 10 +++---- src/nostr_utils/mod.rs | 53 ++++++++++++++--------------------- 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/cli/commands/issue/mod.rs b/src/cli/commands/issue/mod.rs index 73d105a..bb43a88 100644 --- a/src/cli/commands/issue/mod.rs +++ b/src/cli/commands/issue/mod.rs @@ -72,9 +72,19 @@ pub enum IssueStatus { } impl IssueStatus { + /// Returns all issue statuses as kinds + #[inline] + pub const fn all_kinds() -> [Kind; 3] { + [ + Self::Open.kind(), + Self::Resolved.kind(), + Self::Closed.kind(), + ] + } + /// Maps the issue status to its corresponding Nostr kind. #[inline] - pub fn kind(&self) -> Kind { + pub const fn kind(&self) -> Kind { match self { Self::Open => Kind::GitStatusOpen, Self::Resolved => Kind::GitStatusApplied, diff --git a/src/cli/common_commands.rs b/src/cli/common_commands.rs index 3272335..1e11bfa 100644 --- a/src/cli/common_commands.rs +++ b/src/cli/common_commands.rs @@ -310,7 +310,6 @@ pub async fn list_pr_patches_and_issues( arc_client .fetch_events(filter) .await? - .into_iter() .take(limit) .map(|event| { let c = arc_client.clone(); @@ -527,15 +526,13 @@ pub async fn view_pr_issue( let pr_data = if IS_PR { // Check if there is an update let pr_update = client - .fetch_events( + .fetch_event( Filter::new() .kind(crate::cli::pr::PR_UPDATE_KIND) .custom_tag(SingleLetterTag::uppercase(Alphabet::E), event.id) .author(event.pubkey), ) - .await? - .into_iter() - .max_by_key(|e| e.created_at.as_u64()); + .await?; let commit = pr_update .as_ref() diff --git a/src/cli/types.rs b/src/cli/types.rs index 21730c6..3944006 100644 --- a/src/cli/types.rs +++ b/src/cli/types.rs @@ -276,16 +276,16 @@ impl PatchPrStatus { #[inline] pub const fn all_kinds() -> [Kind; 4] { [ - Kind::GitStatusOpen, - Kind::GitStatusApplied, - Kind::GitStatusClosed, - Kind::GitStatusDraft, + Self::Open.kind(), + Self::MergedApplied.kind(), + Self::Closed.kind(), + Self::Draft.kind(), ] } /// Maps the patch/pr status to its corresponding Nostr kind. #[inline] - pub fn kind(&self) -> Kind { + pub const fn kind(&self) -> Kind { match self { Self::Open => Kind::GitStatusOpen, Self::MergedApplied => Kind::GitStatusApplied, diff --git a/src/nostr_utils/mod.rs b/src/nostr_utils/mod.rs index d15848c..daa3cef 100644 --- a/src/nostr_utils/mod.rs +++ b/src/nostr_utils/mod.rs @@ -212,21 +212,17 @@ impl NostrClient { /// Fetches the first event matching the given filter, or None if no event /// is found. pub async fn fetch_event(&self, filter: Filter) -> N34Result> { - Ok(self - .client - .fetch_events(filter.limit(1), CLIENT_TIMEOUT) - .await? - .first_owned()) + Ok(self.fetch_events(filter).await?.next()) } /// Fetches the events matching the given filter - pub async fn fetch_events(&self, filter: Filter) -> N34Result> { + pub async fn fetch_events(&self, filter: Filter) -> N34Result> { // Multiply timeout by 5 to account for multiple events being fetched Ok(self .client .fetch_events(filter, CLIENT_TIMEOUT * 5) .await? - .to_vec()) + .into_iter()) } /// Try to fetch the repositories and returns them @@ -289,19 +285,13 @@ impl NostrClient { issue_id: EventId, authorized_pubkeys: Vec, ) -> N34Result { - self.fetch_events( + self.fetch_event( Filter::new() .event(issue_id) - .kinds([ - Kind::GitStatusOpen, - Kind::GitStatusApplied, - Kind::GitStatusClosed, - ]) + .kinds(IssueStatus::all_kinds()) .authors(utils::dedup(authorized_pubkeys.into_iter())), ) .await? - .into_iter() - .max_by_key(|e| e.created_at) .map(|status| IssueStatus::try_from(status.kind)) .unwrap_or_else(|| Ok(IssueStatus::Open)) } @@ -314,15 +304,13 @@ impl NostrClient { pr_id: EventId, authorized_pubkeys: Vec, ) -> N34Result { - self.fetch_events( + self.fetch_event( Filter::new() .event(pr_id) .kinds(PatchPrStatus::all_kinds()) .authors(utils::dedup(authorized_pubkeys.into_iter())), ) .await? - .into_iter() - .max_by_key(|e| e.created_at) .map(|status| PatchPrStatus::try_from(status.kind)) .unwrap_or_else(|| Ok(PatchPrStatus::Open)) } @@ -338,15 +326,13 @@ impl NostrClient { authorized_pubkeys: Vec, ) -> N34Result { let (root_status, event_tags) = self - .fetch_events( + .fetch_event( Filter::new() .event(root_patch) .kinds(PatchPrStatus::all_kinds()) .authors(utils::dedup(authorized_pubkeys.into_iter())), ) .await? - .into_iter() - .max_by_key(|e| e.created_at) .map(|status| N34Result::Ok((PatchPrStatus::try_from(status.kind)?, status.tags))) .unwrap_or_else(|| Ok((PatchPrStatus::Open, Tags::new())))?; @@ -375,7 +361,6 @@ impl NostrClient { .event(root_patch_id), ) .await? - .into_iter() .filter(|e| { e.tags.iter().any(|t| { t.is_root() && t.content().is_some_and(|c| c == root_patch_id.to_hex()) @@ -450,16 +435,20 @@ impl NostrClient { /// Returns the read relays of the given users if found, otherwise empty /// vector pub async fn read_relays_from_users(&self, users: &[PublicKey]) -> Vec { - self.fetch_events( - Filter::new() - .kind(nostr::event::Kind::RelayList) - .authors(utils::dedup(users.iter().copied())), - ) - .await - .unwrap_or_default() - .into_iter() - .flat_map(|e| utils::add_read_relays(Some(&e))) - .collect() + if let Ok(iterator) = self + .fetch_events( + Filter::new() + .kind(nostr::event::Kind::RelayList) + .authors(utils::dedup(users.iter().copied())), + ) + .await + { + iterator + .flat_map(|e| utils::add_read_relays(Some(&e))) + .collect() + } else { + Vec::new() + } } /// Parse the given content and returns the details that inside it