chore: improve querying

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb
2025-09-13 09:18:55 +00:00
parent c3be6248b5
commit f73245f9c3
4 changed files with 39 additions and 43 deletions

View File

@@ -72,9 +72,19 @@ pub enum IssueStatus {
} }
impl 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. /// Maps the issue status to its corresponding Nostr kind.
#[inline] #[inline]
pub fn kind(&self) -> Kind { pub const fn kind(&self) -> Kind {
match self { match self {
Self::Open => Kind::GitStatusOpen, Self::Open => Kind::GitStatusOpen,
Self::Resolved => Kind::GitStatusApplied, Self::Resolved => Kind::GitStatusApplied,

View File

@@ -310,7 +310,6 @@ pub async fn list_pr_patches_and_issues<const ENTITY_TYPE: u8>(
arc_client arc_client
.fetch_events(filter) .fetch_events(filter)
.await? .await?
.into_iter()
.take(limit) .take(limit)
.map(|event| { .map(|event| {
let c = arc_client.clone(); let c = arc_client.clone();
@@ -527,15 +526,13 @@ pub async fn view_pr_issue<const IS_PR: bool>(
let pr_data = if IS_PR { let pr_data = if IS_PR {
// Check if there is an update // Check if there is an update
let pr_update = client let pr_update = client
.fetch_events( .fetch_event(
Filter::new() Filter::new()
.kind(crate::cli::pr::PR_UPDATE_KIND) .kind(crate::cli::pr::PR_UPDATE_KIND)
.custom_tag(SingleLetterTag::uppercase(Alphabet::E), event.id) .custom_tag(SingleLetterTag::uppercase(Alphabet::E), event.id)
.author(event.pubkey), .author(event.pubkey),
) )
.await? .await?;
.into_iter()
.max_by_key(|e| e.created_at.as_u64());
let commit = pr_update let commit = pr_update
.as_ref() .as_ref()

View File

@@ -276,16 +276,16 @@ impl PatchPrStatus {
#[inline] #[inline]
pub const fn all_kinds() -> [Kind; 4] { pub const fn all_kinds() -> [Kind; 4] {
[ [
Kind::GitStatusOpen, Self::Open.kind(),
Kind::GitStatusApplied, Self::MergedApplied.kind(),
Kind::GitStatusClosed, Self::Closed.kind(),
Kind::GitStatusDraft, Self::Draft.kind(),
] ]
} }
/// Maps the patch/pr status to its corresponding Nostr kind. /// Maps the patch/pr status to its corresponding Nostr kind.
#[inline] #[inline]
pub fn kind(&self) -> Kind { pub const fn kind(&self) -> Kind {
match self { match self {
Self::Open => Kind::GitStatusOpen, Self::Open => Kind::GitStatusOpen,
Self::MergedApplied => Kind::GitStatusApplied, Self::MergedApplied => Kind::GitStatusApplied,

View File

@@ -212,21 +212,17 @@ impl NostrClient {
/// Fetches the first event matching the given filter, or None if no event /// Fetches the first event matching the given filter, or None if no event
/// is found. /// is found.
pub async fn fetch_event(&self, filter: Filter) -> N34Result<Option<Event>> { pub async fn fetch_event(&self, filter: Filter) -> N34Result<Option<Event>> {
Ok(self Ok(self.fetch_events(filter).await?.next())
.client
.fetch_events(filter.limit(1), CLIENT_TIMEOUT)
.await?
.first_owned())
} }
/// Fetches the events matching the given filter /// Fetches the events matching the given filter
pub async fn fetch_events(&self, filter: Filter) -> N34Result<Vec<Event>> { pub async fn fetch_events(&self, filter: Filter) -> N34Result<impl Iterator<Item = Event>> {
// Multiply timeout by 5 to account for multiple events being fetched // Multiply timeout by 5 to account for multiple events being fetched
Ok(self Ok(self
.client .client
.fetch_events(filter, CLIENT_TIMEOUT * 5) .fetch_events(filter, CLIENT_TIMEOUT * 5)
.await? .await?
.to_vec()) .into_iter())
} }
/// Try to fetch the repositories and returns them /// Try to fetch the repositories and returns them
@@ -289,19 +285,13 @@ impl NostrClient {
issue_id: EventId, issue_id: EventId,
authorized_pubkeys: Vec<PublicKey>, authorized_pubkeys: Vec<PublicKey>,
) -> N34Result<IssueStatus> { ) -> N34Result<IssueStatus> {
self.fetch_events( self.fetch_event(
Filter::new() Filter::new()
.event(issue_id) .event(issue_id)
.kinds([ .kinds(IssueStatus::all_kinds())
Kind::GitStatusOpen,
Kind::GitStatusApplied,
Kind::GitStatusClosed,
])
.authors(utils::dedup(authorized_pubkeys.into_iter())), .authors(utils::dedup(authorized_pubkeys.into_iter())),
) )
.await? .await?
.into_iter()
.max_by_key(|e| e.created_at)
.map(|status| IssueStatus::try_from(status.kind)) .map(|status| IssueStatus::try_from(status.kind))
.unwrap_or_else(|| Ok(IssueStatus::Open)) .unwrap_or_else(|| Ok(IssueStatus::Open))
} }
@@ -314,15 +304,13 @@ impl NostrClient {
pr_id: EventId, pr_id: EventId,
authorized_pubkeys: Vec<PublicKey>, authorized_pubkeys: Vec<PublicKey>,
) -> N34Result<PatchPrStatus> { ) -> N34Result<PatchPrStatus> {
self.fetch_events( self.fetch_event(
Filter::new() Filter::new()
.event(pr_id) .event(pr_id)
.kinds(PatchPrStatus::all_kinds()) .kinds(PatchPrStatus::all_kinds())
.authors(utils::dedup(authorized_pubkeys.into_iter())), .authors(utils::dedup(authorized_pubkeys.into_iter())),
) )
.await? .await?
.into_iter()
.max_by_key(|e| e.created_at)
.map(|status| PatchPrStatus::try_from(status.kind)) .map(|status| PatchPrStatus::try_from(status.kind))
.unwrap_or_else(|| Ok(PatchPrStatus::Open)) .unwrap_or_else(|| Ok(PatchPrStatus::Open))
} }
@@ -338,15 +326,13 @@ impl NostrClient {
authorized_pubkeys: Vec<PublicKey>, authorized_pubkeys: Vec<PublicKey>,
) -> N34Result<PatchPrStatus> { ) -> N34Result<PatchPrStatus> {
let (root_status, event_tags) = self let (root_status, event_tags) = self
.fetch_events( .fetch_event(
Filter::new() Filter::new()
.event(root_patch) .event(root_patch)
.kinds(PatchPrStatus::all_kinds()) .kinds(PatchPrStatus::all_kinds())
.authors(utils::dedup(authorized_pubkeys.into_iter())), .authors(utils::dedup(authorized_pubkeys.into_iter())),
) )
.await? .await?
.into_iter()
.max_by_key(|e| e.created_at)
.map(|status| N34Result::Ok((PatchPrStatus::try_from(status.kind)?, status.tags))) .map(|status| N34Result::Ok((PatchPrStatus::try_from(status.kind)?, status.tags)))
.unwrap_or_else(|| Ok((PatchPrStatus::Open, Tags::new())))?; .unwrap_or_else(|| Ok((PatchPrStatus::Open, Tags::new())))?;
@@ -375,7 +361,6 @@ impl NostrClient {
.event(root_patch_id), .event(root_patch_id),
) )
.await? .await?
.into_iter()
.filter(|e| { .filter(|e| {
e.tags.iter().any(|t| { e.tags.iter().any(|t| {
t.is_root() && t.content().is_some_and(|c| c == root_patch_id.to_hex()) 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 /// Returns the read relays of the given users if found, otherwise empty
/// vector /// vector
pub async fn read_relays_from_users(&self, users: &[PublicKey]) -> Vec<RelayUrl> { pub async fn read_relays_from_users(&self, users: &[PublicKey]) -> Vec<RelayUrl> {
self.fetch_events( if let Ok(iterator) = self
Filter::new() .fetch_events(
.kind(nostr::event::Kind::RelayList) Filter::new()
.authors(utils::dedup(users.iter().copied())), .kind(nostr::event::Kind::RelayList)
) .authors(utils::dedup(users.iter().copied())),
.await )
.unwrap_or_default() .await
.into_iter() {
.flat_map(|e| utils::add_read_relays(Some(&e))) iterator
.collect() .flat_map(|e| utils::add_read_relays(Some(&e)))
.collect()
} else {
Vec::new()
}
} }
/// Parse the given content and returns the details that inside it /// Parse the given content and returns the details that inside it