diff --git a/lib/domain_layer/usecases/engines/network_engine.dart b/lib/domain_layer/usecases/engines/network_engine.dart index 5fd0bea9c..7b2186f72 100644 --- a/lib/domain_layer/usecases/engines/network_engine.dart +++ b/lib/domain_layer/usecases/engines/network_engine.dart @@ -12,8 +12,6 @@ abstract class NetworkEngine { Iterable? specificRelays, }); - // todo: - // response obj? - // implement in both engines - // Future handleCloseSubscription(String subId); + /// closes the nostr network subscription + Future closeSubscription(String subId); } diff --git a/lib/domain_layer/usecases/jit_engine.dart b/lib/domain_layer/usecases/jit_engine.dart index 992ee6394..6a7710ab5 100644 --- a/lib/domain_layer/usecases/jit_engine.dart +++ b/lib/domain_layer/usecases/jit_engine.dart @@ -1,5 +1,7 @@ import 'dart:async'; +import 'package:ndk/shared/nips/nip01/client_msg.dart'; + import '../../shared/helpers/relay_helper.dart'; import '../../shared/logger/logger.dart'; import '../entities/broadcast_response.dart'; @@ -176,11 +178,16 @@ class JitEngine with Logger implements NetworkEngine { return NdkBroadcastResponse(publishedEvent: nostrEvent); } - // close a relay subscription, the relay connection will be kept open and closed automatically (garbage collected) - //todo: this could be moved to the request object - handleCloseSubscription(String id) async { - await seedRelaysConnected; - throw UnimplementedError(); + /// close a relay subscription, the relay connection will be kept open and closed automatically (garbage collected) + @override + closeSubscription(String id) async { + //await seedRelaysConnected; + for (var relay in globalState.connectedRelays) { + if (relay.activeSubscriptions.containsKey(id)) { + await relay.closeSubscription(id); + relay.activeSubscriptions.remove(id); + } + } } static doesRelayCoverPubkey( diff --git a/lib/domain_layer/usecases/relay_jit_manager/relay_jit.dart b/lib/domain_layer/usecases/relay_jit_manager/relay_jit.dart index fefb82da5..cee085deb 100644 --- a/lib/domain_layer/usecases/relay_jit_manager/relay_jit.dart +++ b/lib/domain_layer/usecases/relay_jit_manager/relay_jit.dart @@ -151,7 +151,7 @@ class RelayJit extends Relay with Logger { return; } activeSubscriptions.remove(id); - ClientMsg closeMsg = ClientMsg("CLOSE", id: id, filters: []); + ClientMsg closeMsg = ClientMsg("CLOSE", id: id); await send(closeMsg); } diff --git a/lib/domain_layer/usecases/relay_sets_engine.dart b/lib/domain_layer/usecases/relay_sets_engine.dart index 6e945744f..2d00b98da 100644 --- a/lib/domain_layer/usecases/relay_sets_engine.dart +++ b/lib/domain_layer/usecases/relay_sets_engine.dart @@ -289,4 +289,9 @@ class RelaySetsEngine implements NetworkEngine { return NdkBroadcastResponse(publishedEvent: nostrEvent); } + + @override + Future closeSubscription(String subId) async { + _relayManager.closeSubscription(subId); + } } diff --git a/lib/domain_layer/usecases/requests/requests.dart b/lib/domain_layer/usecases/requests/requests.dart index 0861d0f5d..3a3d82a7e 100644 --- a/lib/domain_layer/usecases/requests/requests.dart +++ b/lib/domain_layer/usecases/requests/requests.dart @@ -116,6 +116,11 @@ class Requests { )); } + /// Closes a Nostr network subscription + Future closeSubscription(String subId) { + return _engine.closeSubscription(subId); + } + /// Performs a low-level Nostr event request /// /// This method should be used only if the prebuilt use cases and