Skip to content

Commit 854b690

Browse files
committed
Expose subscribe Client methods
1 parent ba3d399 commit 854b690

File tree

11 files changed

+2285
-333
lines changed

11 files changed

+2285
-333
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* Expose `EventBuilder::private_msg` ([Yuki Kishimoto])
4141
* Expose `UnwrappedGift` ([Yuki Kishimoto])
4242
* Expose `Client::handle_notifications` ([Yuki Kishimoto])
43+
* Expose subscribe `Client` methods ([Yuki Kishimoto])
4344

4445
### Fixed
4546

lib/src/rust/api/client.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'protocol/event.dart';
1212
import 'protocol/event/builder.dart';
1313
import 'protocol/signer.dart';
1414
import 'protocol/types/filter.dart';
15+
import 'relay/options.dart';
1516

1617
// 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`
1718

@@ -136,6 +137,61 @@ abstract class Client implements RustOpaqueInterface {
136137
/// Rise error if it not set.
137138
Future<NostrSigner> signer();
138139

140+
/// Subscribe to filters
141+
///
142+
/// This method creates a new subscription. None of the previous subscriptions will be edited/closed when you call this!
143+
/// So remember to unsubscribe when you no longer need it.
144+
///
145+
/// If `gossip` is enabled (see [`Options::gossip`]) the events will be requested also to
146+
/// NIP65 relays (automatically discovered) of public keys included in filters (if any).
147+
///
148+
/// # Auto-closing subscription
149+
///
150+
/// It's possible to automatically close a subscription by configuring the [SubscribeAutoCloseOptions].
151+
///
152+
/// Note: auto-closing subscriptions aren't saved in subscriptions map!
153+
Future<String> subscribe(
154+
{required List<Filter> filters, SubscribeAutoCloseOptions? opts});
155+
156+
/// Subscribe to filters to specific relays
157+
///
158+
/// This method creates a new subscription. None of the previous subscriptions will be edited/closed when you call this!
159+
/// So remember to unsubscribe when you no longer need it.
160+
///
161+
/// ### Auto-closing subscription
162+
///
163+
/// It's possible to automatically close a subscription by configuring the [SubscribeAutoCloseOptions].
164+
Future<String> subscribeTo(
165+
{required List<String> urls,
166+
required List<Filter> filters,
167+
SubscribeAutoCloseOptions? opts});
168+
169+
/// Subscribe to filters with custom [SubscriptionId]
170+
///
171+
/// If `gossip` is enabled (see [`Options::gossip`]) the events will be requested also to
172+
/// NIP65 relays (automatically discovered) of public keys included in filters (if any).
173+
///
174+
/// # Auto-closing subscription
175+
///
176+
/// It's possible to automatically close a subscription by configuring the [SubscribeAutoCloseOptions].
177+
///
178+
/// Note: auto-closing subscriptions aren't saved in subscriptions map!
179+
Future<void> subscribeWithId(
180+
{required String id,
181+
required List<Filter> filters,
182+
SubscribeAutoCloseOptions? opts});
183+
184+
/// Subscribe to filters with custom [SubscriptionId] to specific relays
185+
///
186+
/// ### Auto-closing subscription
187+
///
188+
/// It's possible to automatically close a subscription by configuring the [SubscribeAutoCloseOptions].
189+
Future<void> subscribeWithIdTo(
190+
{required List<String> urls,
191+
required String id,
192+
required List<Filter> filters,
193+
SubscribeAutoCloseOptions? opts});
194+
139195
/// Unset nostr signer
140196
Future<void> unsetSigner();
141197
}

lib/src/rust/api/relay/options.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@ import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
88
import 'package:freezed_annotation/freezed_annotation.dart' hide protected;
99
part 'options.freezed.dart';
1010

11-
// 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`
11+
// 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`
12+
13+
// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<_SubscribeAutoCloseOptions>>
14+
abstract class SubscribeAutoCloseOptions implements RustOpaqueInterface {
15+
/// Close subscription when the policy is satisfied
16+
SubscribeAutoCloseOptions exitPolicy({required ReqExitPolicy policy});
17+
18+
/// Automatically close subscription if no notifications/events are received within the duration.
19+
SubscribeAutoCloseOptions idleTimeout({Duration? timeout});
20+
21+
factory SubscribeAutoCloseOptions() =>
22+
NostrSdk.instance.api.crateApiRelayOptionsSubscribeAutoCloseOptionsNew();
23+
24+
/// Automatically close subscription after duration.
25+
SubscribeAutoCloseOptions timeout({Duration? timeout});
26+
}
1227

1328
@freezed
1429
sealed class ConnectionMode with _$ConnectionMode {
@@ -35,3 +50,21 @@ sealed class ConnectionMode with _$ConnectionMode {
3550
String? customPath,
3651
}) = ConnectionMode_Tor;
3752
}
53+
54+
@freezed
55+
sealed class ReqExitPolicy with _$ReqExitPolicy {
56+
const ReqExitPolicy._();
57+
58+
/// Exit on EOSE
59+
const factory ReqExitPolicy.exitOnEose() = ReqExitPolicy_ExitOnEOSE;
60+
61+
/// After EOSE is received, keep listening for N more events that match the filter.
62+
const factory ReqExitPolicy.waitForEventsAfterEose(
63+
int field0,
64+
) = ReqExitPolicy_WaitForEventsAfterEOSE;
65+
66+
/// After EOSE is received, keep listening for matching events for `Duration` more time.
67+
const factory ReqExitPolicy.waitDurationAfterEose(
68+
Duration field0,
69+
) = ReqExitPolicy_WaitDurationAfterEOSE;
70+
}

0 commit comments

Comments
 (0)