Skip to content

Commit

Permalink
Expose Client::fetch_events
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 2, 2025
1 parent c50a411 commit 2e63f8f
Show file tree
Hide file tree
Showing 9 changed files with 719 additions and 291 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

* Expose `EventId` ([Yuki Kishimoto])
* Expose `Alphabet`, `SingleLetterTag` and `Filter` ([Yuki Kishimoto])
* Expose `Client::fetch_events` ([Yuki Kishimoto])

### Fixed

Expand Down
8 changes: 8 additions & 0 deletions lib/src/rust/api/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'protocol/event.dart';
import 'protocol/event/builder.dart';
import 'protocol/signer.dart';
import 'protocol/types/filter.dart';

// These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `from`

Expand Down Expand Up @@ -67,6 +68,13 @@ abstract class Client implements RustOpaqueInterface {
/// Disconnect relay
Future<void> disconnectRelay({required String url});

/// Fetch events from relays
///
/// If `gossip` is enabled (see [`Options::gossip`]) the events will be requested also to
/// NIP65 relays (automatically discovered) of public keys included in filters (if any).
Future<List<Event>> fetchEvents(
{required List<Filter> filters, required Duration timeout});

/// Disconnect and force remove all relays
Future<void> forceRemoveAllRelays();

Expand Down
452 changes: 307 additions & 145 deletions lib/src/rust/frb_generated.dart

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions lib/src/rust/frb_generated.io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ abstract class NostrSdkApiImplPlatform extends BaseApiImpl<NostrSdkWire> {
dco_decode_Auto_Ref_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_UnsignedEvent(
dynamic raw);

@protected
Duration dco_decode_Chrono_Duration(dynamic raw);

@protected
Map<String, String> dco_decode_Map_String_String(dynamic raw);

Expand Down Expand Up @@ -340,11 +343,24 @@ abstract class NostrSdkApiImplPlatform extends BaseApiImpl<NostrSdkWire> {
@protected
int dco_decode_i_32(dynamic raw);

@protected
PlatformInt64 dco_decode_i_64(dynamic raw);

@protected
List<Event>
dco_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Event(
dynamic raw);

@protected
List<EventId>
dco_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_EventId(
dynamic raw);

@protected
List<Filter>
dco_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Filter(
dynamic raw);

@protected
List<PublicKey>
dco_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_PublicKey(
Expand Down Expand Up @@ -556,6 +572,9 @@ abstract class NostrSdkApiImplPlatform extends BaseApiImpl<NostrSdkWire> {
sse_decode_Auto_Ref_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_UnsignedEvent(
SseDeserializer deserializer);

@protected
Duration sse_decode_Chrono_Duration(SseDeserializer deserializer);

@protected
Map<String, String> sse_decode_Map_String_String(
SseDeserializer deserializer);
Expand Down Expand Up @@ -664,11 +683,24 @@ abstract class NostrSdkApiImplPlatform extends BaseApiImpl<NostrSdkWire> {
@protected
int sse_decode_i_32(SseDeserializer deserializer);

@protected
PlatformInt64 sse_decode_i_64(SseDeserializer deserializer);

@protected
List<Event>
sse_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Event(
SseDeserializer deserializer);

@protected
List<EventId>
sse_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_EventId(
SseDeserializer deserializer);

@protected
List<Filter>
sse_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Filter(
SseDeserializer deserializer);

@protected
List<PublicKey>
sse_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_PublicKey(
Expand Down Expand Up @@ -885,6 +917,9 @@ abstract class NostrSdkApiImplPlatform extends BaseApiImpl<NostrSdkWire> {
sse_encode_Auto_Ref_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_UnsignedEvent(
UnsignedEvent self, SseSerializer serializer);

@protected
void sse_encode_Chrono_Duration(Duration self, SseSerializer serializer);

@protected
void sse_encode_Map_String_String(
Map<String, String> self, SseSerializer serializer);
Expand Down Expand Up @@ -996,11 +1031,24 @@ abstract class NostrSdkApiImplPlatform extends BaseApiImpl<NostrSdkWire> {
@protected
void sse_encode_i_32(int self, SseSerializer serializer);

@protected
void sse_encode_i_64(PlatformInt64 self, SseSerializer serializer);

@protected
void
sse_encode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Event(
List<Event> self, SseSerializer serializer);

@protected
void
sse_encode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_EventId(
List<EventId> self, SseSerializer serializer);

@protected
void
sse_encode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Filter(
List<Filter> self, SseSerializer serializer);

@protected
void
sse_encode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_PublicKey(
Expand Down
48 changes: 48 additions & 0 deletions lib/src/rust/frb_generated.web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ abstract class NostrSdkApiImplPlatform extends BaseApiImpl<NostrSdkWire> {
dco_decode_Auto_Ref_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_UnsignedEvent(
dynamic raw);

@protected
Duration dco_decode_Chrono_Duration(dynamic raw);

@protected
Map<String, String> dco_decode_Map_String_String(dynamic raw);

Expand Down Expand Up @@ -342,11 +345,24 @@ abstract class NostrSdkApiImplPlatform extends BaseApiImpl<NostrSdkWire> {
@protected
int dco_decode_i_32(dynamic raw);

@protected
PlatformInt64 dco_decode_i_64(dynamic raw);

@protected
List<Event>
dco_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Event(
dynamic raw);

@protected
List<EventId>
dco_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_EventId(
dynamic raw);

@protected
List<Filter>
dco_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Filter(
dynamic raw);

@protected
List<PublicKey>
dco_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_PublicKey(
Expand Down Expand Up @@ -558,6 +574,9 @@ abstract class NostrSdkApiImplPlatform extends BaseApiImpl<NostrSdkWire> {
sse_decode_Auto_Ref_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_UnsignedEvent(
SseDeserializer deserializer);

@protected
Duration sse_decode_Chrono_Duration(SseDeserializer deserializer);

@protected
Map<String, String> sse_decode_Map_String_String(
SseDeserializer deserializer);
Expand Down Expand Up @@ -666,11 +685,24 @@ abstract class NostrSdkApiImplPlatform extends BaseApiImpl<NostrSdkWire> {
@protected
int sse_decode_i_32(SseDeserializer deserializer);

@protected
PlatformInt64 sse_decode_i_64(SseDeserializer deserializer);

@protected
List<Event>
sse_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Event(
SseDeserializer deserializer);

@protected
List<EventId>
sse_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_EventId(
SseDeserializer deserializer);

@protected
List<Filter>
sse_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Filter(
SseDeserializer deserializer);

@protected
List<PublicKey>
sse_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_PublicKey(
Expand Down Expand Up @@ -887,6 +919,9 @@ abstract class NostrSdkApiImplPlatform extends BaseApiImpl<NostrSdkWire> {
sse_encode_Auto_Ref_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_UnsignedEvent(
UnsignedEvent self, SseSerializer serializer);

@protected
void sse_encode_Chrono_Duration(Duration self, SseSerializer serializer);

@protected
void sse_encode_Map_String_String(
Map<String, String> self, SseSerializer serializer);
Expand Down Expand Up @@ -998,11 +1033,24 @@ abstract class NostrSdkApiImplPlatform extends BaseApiImpl<NostrSdkWire> {
@protected
void sse_encode_i_32(int self, SseSerializer serializer);

@protected
void sse_encode_i_64(PlatformInt64 self, SseSerializer serializer);

@protected
void
sse_encode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Event(
List<Event> self, SseSerializer serializer);

@protected
void
sse_encode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_EventId(
List<EventId> self, SseSerializer serializer);

@protected
void
sse_encode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_Filter(
List<Filter> self, SseSerializer serializer);

@protected
void
sse_encode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInner_PublicKey(
Expand Down
3 changes: 3 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ codegen = ["dep:flutter_rust_bridge_codegen"]

[dependencies]
anyhow = "1.0"
flutter_rust_bridge = "=2.0.0"
chrono = "0.4"
flutter_rust_bridge = { version = "=2.0.0", features = ["anyhow", "chrono"] }
flutter_rust_bridge_codegen = { version = "=2.0.0", optional = true }
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", rev = "c54427849014e8451af54a56664e633e4dc0fac1", default-features = false, features = ["all-nips", "tor"] }

Expand Down
17 changes: 17 additions & 0 deletions rust/src/api/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Distributed under the MIT software license

use anyhow::Result;
use chrono::Duration;
use flutter_rust_bridge::frb;
use nostr_sdk::prelude::*;

Expand All @@ -15,6 +16,7 @@ use self::output::SendEventOutput;
use super::protocol::event::_Event;
use super::protocol::event::builder::_EventBuilder;
use super::protocol::signer::_NostrSigner;
use super::protocol::types::filter::_Filter;

#[frb(name = "Client")]
pub struct _Client {
Expand Down Expand Up @@ -182,6 +184,21 @@ impl _Client {
Ok(self.inner.disconnect().await?)
}

/// Fetch events from relays
///
/// If `gossip` is enabled (see [`Options::gossip`]) the events will be requested also to
/// NIP65 relays (automatically discovered) of public keys included in filters (if any).
// TODO: return `Events` struct
pub async fn fetch_events(
&self,
filters: Vec<_Filter>,
timeout: Duration,
) -> Result<Vec<_Event>> {
let filters = filters.into_iter().map(|f| f.inner).collect();
let events = self.inner.fetch_events(filters, timeout.to_std()?).await?;
Ok(events.into_iter().map(|e| e.into()).collect())
}

/// Send event
///
/// Send `Event` to all relays with `WRITE` flag.
Expand Down
Loading

0 comments on commit 2e63f8f

Please sign in to comment.