Skip to content

Commit

Permalink
Expose subscribe Client methods
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 16, 2025
1 parent ba3d399 commit 854b690
Show file tree
Hide file tree
Showing 11 changed files with 2,285 additions and 333 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* Expose `EventBuilder::private_msg` ([Yuki Kishimoto])
* Expose `UnwrappedGift` ([Yuki Kishimoto])
* Expose `Client::handle_notifications` ([Yuki Kishimoto])
* Expose subscribe `Client` methods ([Yuki Kishimoto])

### Fixed

Expand Down
56 changes: 56 additions & 0 deletions lib/src/rust/api/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'protocol/event.dart';
import 'protocol/event/builder.dart';
import 'protocol/signer.dart';
import 'protocol/types/filter.dart';
import 'relay/options.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 @@ -136,6 +137,61 @@ abstract class Client implements RustOpaqueInterface {
/// Rise error if it not set.
Future<NostrSigner> signer();

/// Subscribe to filters
///
/// This method creates a new subscription. None of the previous subscriptions will be edited/closed when you call this!
/// So remember to unsubscribe when you no longer need it.
///
/// 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).
///
/// # Auto-closing subscription
///
/// It's possible to automatically close a subscription by configuring the [SubscribeAutoCloseOptions].
///
/// Note: auto-closing subscriptions aren't saved in subscriptions map!
Future<String> subscribe(
{required List<Filter> filters, SubscribeAutoCloseOptions? opts});

/// Subscribe to filters to specific relays
///
/// This method creates a new subscription. None of the previous subscriptions will be edited/closed when you call this!
/// So remember to unsubscribe when you no longer need it.
///
/// ### Auto-closing subscription
///
/// It's possible to automatically close a subscription by configuring the [SubscribeAutoCloseOptions].
Future<String> subscribeTo(
{required List<String> urls,
required List<Filter> filters,
SubscribeAutoCloseOptions? opts});

/// Subscribe to filters with custom [SubscriptionId]
///
/// 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).
///
/// # Auto-closing subscription
///
/// It's possible to automatically close a subscription by configuring the [SubscribeAutoCloseOptions].
///
/// Note: auto-closing subscriptions aren't saved in subscriptions map!
Future<void> subscribeWithId(
{required String id,
required List<Filter> filters,
SubscribeAutoCloseOptions? opts});

/// Subscribe to filters with custom [SubscriptionId] to specific relays
///
/// ### Auto-closing subscription
///
/// It's possible to automatically close a subscription by configuring the [SubscribeAutoCloseOptions].
Future<void> subscribeWithIdTo(
{required List<String> urls,
required String id,
required List<Filter> filters,
SubscribeAutoCloseOptions? opts});

/// Unset nostr signer
Future<void> unsetSigner();
}
35 changes: 34 additions & 1 deletion lib/src/rust/api/relay/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@ import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'package:freezed_annotation/freezed_annotation.dart' hide protected;
part 'options.freezed.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`, `try_from`
// These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `deref`, `from`, `try_from`, `try_from`

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<_SubscribeAutoCloseOptions>>
abstract class SubscribeAutoCloseOptions implements RustOpaqueInterface {
/// Close subscription when the policy is satisfied
SubscribeAutoCloseOptions exitPolicy({required ReqExitPolicy policy});

/// Automatically close subscription if no notifications/events are received within the duration.
SubscribeAutoCloseOptions idleTimeout({Duration? timeout});

factory SubscribeAutoCloseOptions() =>
NostrSdk.instance.api.crateApiRelayOptionsSubscribeAutoCloseOptionsNew();

/// Automatically close subscription after duration.
SubscribeAutoCloseOptions timeout({Duration? timeout});
}

@freezed
sealed class ConnectionMode with _$ConnectionMode {
Expand All @@ -35,3 +50,21 @@ sealed class ConnectionMode with _$ConnectionMode {
String? customPath,
}) = ConnectionMode_Tor;
}

@freezed
sealed class ReqExitPolicy with _$ReqExitPolicy {
const ReqExitPolicy._();

/// Exit on EOSE
const factory ReqExitPolicy.exitOnEose() = ReqExitPolicy_ExitOnEOSE;

/// After EOSE is received, keep listening for N more events that match the filter.
const factory ReqExitPolicy.waitForEventsAfterEose(
int field0,
) = ReqExitPolicy_WaitForEventsAfterEOSE;

/// After EOSE is received, keep listening for matching events for `Duration` more time.
const factory ReqExitPolicy.waitDurationAfterEose(
Duration field0,
) = ReqExitPolicy_WaitDurationAfterEOSE;
}
Loading

0 comments on commit 854b690

Please sign in to comment.