chore: Improve event fetchers functions

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb
2025-05-09 14:49:51 +00:00
parent 4298ca5d2d
commit 1af731ef67

View File

@@ -121,6 +121,16 @@ impl NostrClient {
Ok(result.success.into_iter().collect()) Ok(result.success.into_iter().collect())
} }
/// 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, CLIENT_TIMEOUT)
.await?
.first_owned())
}
/// Try to fetch a repository and returns it /// Try to fetch a repository and returns it
pub async fn fetch_repo( pub async fn fetch_repo(
&self, &self,
@@ -130,39 +140,25 @@ impl NostrClient {
.author(repo_naddr.public_key) .author(repo_naddr.public_key)
.kind(Kind::GitRepoAnnouncement) .kind(Kind::GitRepoAnnouncement)
.identifier(&repo_naddr.identifier); .identifier(&repo_naddr.identifier);
let events = self
.client
.fetch_events(filter, CLIENT_TIMEOUT)
.await
.map_err(|_| N34Error::NotFoundRepo)?;
self.fetch_event(filter)
Ok(utils::event_into_repo( .await?
events.first_owned().ok_or(N34Error::NotFoundRepo)?, .map(|e| utils::event_into_repo(e, &repo_naddr.identifier))
&repo_naddr.identifier, .ok_or(N34Error::NotFoundRepo)
))
} }
/// Fetches the relay list (kind 10002) for the given user. Returns None if /// Fetches the relay list (kind 10002) for the given user. Returns None if
/// no relays are found. /// no relays are found.
pub async fn user_relays_list(&self, user: PublicKey) -> N34Result<Option<Event>> { pub async fn user_relays_list(&self, user: PublicKey) -> N34Result<Option<Event>> {
Ok(self self.fetch_event(Filter::new().author(user).kind(Kind::RelayList))
.client .await
.fetch_events(
Filter::new().author(user).kind(Kind::RelayList),
CLIENT_TIMEOUT,
)
.await?
.first_owned())
} }
/// Gets the author of the specified event, if found. /// Gets the author of the specified event, if found.
pub async fn event_author(&self, event_id: EventId) -> N34Result<Option<PublicKey>> { pub async fn event_author(&self, event_id: EventId) -> N34Result<Option<PublicKey>> {
Ok(self Ok(self
.client .fetch_event(Filter::new().id(event_id))
.fetch_events(Filter::new().id(event_id), CLIENT_TIMEOUT)
.await? .await?
.first_owned()
.map(|e| e.pubkey)) .map(|e| e.pubkey))
} }
} }