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 {
/// 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,

View File

@@ -310,7 +310,6 @@ pub async fn list_pr_patches_and_issues<const ENTITY_TYPE: u8>(
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<const IS_PR: bool>(
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()

View File

@@ -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,

View File

@@ -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<Option<Event>> {
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<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
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<PublicKey>,
) -> N34Result<IssueStatus> {
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<PublicKey>,
) -> N34Result<PatchPrStatus> {
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<PublicKey>,
) -> N34Result<PatchPrStatus> {
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<RelayUrl> {
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