Skip to content

Commit

Permalink
move settings to ndk config
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-lox committed Mar 3, 2025
1 parent b0a8979 commit d9a8642
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
23 changes: 16 additions & 7 deletions packages/ndk/lib/domain_layer/usecases/broadcast/broadcast.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:ndk/ndk.dart';

import '../../../config/broadcast_defaults.dart';
import '../../../shared/nips/nip09/deletion.dart';
import '../../../shared/nips/nip25/reactions.dart';
import '../../entities/broadcast_state.dart';
Expand All @@ -14,6 +13,8 @@ class Broadcast {
final Accounts _accounts;
final CacheManager _cacheManager;
final GlobalState _globalState;
final double _considerDonePercent;
final Duration _timeout;

/// creates a new [Broadcast] instance
///
Expand All @@ -22,10 +23,14 @@ class Broadcast {
required CacheManager cacheManager,
required NetworkEngine networkEngine,
required Accounts accounts,
required double considerDonePercent,
required Duration timeout,
}) : _accounts = accounts,
_cacheManager = cacheManager,
_engine = networkEngine,
_globalState = globalState;
_globalState = globalState,
_considerDonePercent = considerDonePercent,
_timeout = timeout;

/// [throws] if the default signer and the custom signer are null \
/// [returns] the signer that is not null, if both are provided returns [customSigner]
Expand All @@ -39,18 +44,22 @@ class Broadcast {
/// low level nostr broadcast using inbox/outbox (gossip) \
/// [specificRelays] disables inbox/outbox (gossip) and broadcasts to the relays specified. Useful for NostrWalletConnect \
/// [customSigner] if you want to use a different signer than the one from currently logged in user in [Accounts] \
/// [considerDonePercent] the percentage (0.0, 1.0) of relays that need to respond with "OK" for the broadcast to be considered done \
/// [considerDonePercent] the percentage (0.0, 1.0) of relays that need to respond with "OK" for the broadcast to be considered done (overrides the default value) \
/// [timeout] the timeout for the broadcast (overrides the default timeout) \
/// [returns] a [NdkBroadcastResponse] object containing the result => success per relay
NdkBroadcastResponse broadcast({
required Nip01Event nostrEvent,
Iterable<String>? specificRelays,
EventSigner? customSigner,
double considerDonePercent = BroadcastDefaults.CONSIDER_DONE_PERCENT,
Duration timeout = BroadcastDefaults.TIMEOUT,
double? considerDonePercent,
Duration? timeout,
}) {
final myConsiderDonePercent = considerDonePercent ?? _considerDonePercent;
final myTimeout = timeout ?? _timeout;

final broadcastState = BroadcastState(
considerDonePercent: considerDonePercent,
timeout: timeout,
considerDonePercent: myConsiderDonePercent,
timeout: myTimeout,
);
// register broadcast state
_globalState.inFlightBroadcasts[nostrEvent.id] = broadcastState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ class UserRelayLists {
required String relayUrl,
required ReadWriteMarker marker,
required Iterable<String> broadcastRelays,
double? considerDonePercent,
Duration? timeout,
}) async {
UserRelayList? userRelayList = await _ensureUpToDateUserRelayList();
if (userRelayList == null) {
Expand Down Expand Up @@ -221,8 +223,10 @@ class UserRelayLists {
/// [newUserRelayList] the new user relay list
/// [returns] the new user relay list
Future<UserRelayList> setInitialUserRelayList(
UserRelayList newUserRelayList,
) async {
UserRelayList newUserRelayList, {
double? considerDonePercent,
Duration? timeout,
}) async {
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
// set created at and refreshed timestamp
newUserRelayList.refreshedTimestamp = now;
Expand All @@ -245,6 +249,8 @@ class UserRelayLists {
Future<UserRelayList?> broadcastRemoveNip65Relay({
required String relayUrl,
required Iterable<String> broadcastRelays,
double? considerDonePercent,
Duration? timeout,
}) async {
UserRelayList? userRelayList = await _ensureUpToDateUserRelayList();

Expand Down
2 changes: 2 additions & 0 deletions packages/ndk/lib/presentation_layer/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ class Initialization {
networkEngine: engine,
cacheManager: _ndkConfig.cache,
accounts: accounts,
considerDonePercent: _ndkConfig.defaultBroadcastConsiderDonePercent,
timeout: _ndkConfig.defaultBroadcastTimeout,
);

follows = Follows(
Expand Down
14 changes: 12 additions & 2 deletions packages/ndk/lib/presentation_layer/ndk_config.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:ndk/config/broadcast_defaults.dart';

import '../config/bootstrap_relays.dart';
import '../config/logger_defaults.dart';
import '../config/request_defaults.dart';
Expand Down Expand Up @@ -38,6 +40,13 @@ class NdkConfig {
/// this value is used if no individual timeout is set for a query
Duration defaultQueryTimeout;

/// timeout for broadcasts
Duration defaultBroadcastTimeout;

/// percentage of relays that need to respond with "OK" for the broadcast to be considered done \
/// value between 0.0 and 1.0
double defaultBroadcastConsiderDonePercent;

/// log level
lib_logger.Level logLevel;

Expand All @@ -59,8 +68,9 @@ class NdkConfig {
this.bootstrapRelays = DEFAULT_BOOTSTRAP_RELAYS,
this.eventOutFilters = const [],
this.defaultQueryTimeout = RequestDefaults.DEFAULT_QUERY_TIMEOUT,

/// test
this.defaultBroadcastTimeout = BroadcastDefaults.TIMEOUT,
this.defaultBroadcastConsiderDonePercent =
BroadcastDefaults.CONSIDER_DONE_PERCENT,
this.logLevel = defaultLogLevel,
});
}
Expand Down

0 comments on commit d9a8642

Please sign in to comment.