diff --git a/services.py b/services.py index ab65dae..e03ad1d 100644 --- a/services.py +++ b/services.py @@ -45,7 +45,6 @@ class NostrRouter: except WebSocketDisconnect: self.connected = False break - # print(json_str) # registers a subscription if the input was a REQ request subscription_id, json_str_rewritten = await self._add_nostr_subscription( @@ -81,7 +80,7 @@ class NostrRouter: } # this reconstructs the original response from the relay - # reconstruct oriiginal subscription id + # reconstruct original subscription id s_original = s[len(f"{self.subscription_id_rewrite}_") :] event_to_forward = ["EVENT", s_original, event_json] # print(json.dumps(event_to_forward)) @@ -104,6 +103,7 @@ class NostrRouter: async def stop(self): for t in self.tasks: t.cancel() + self.connected = False def _marshall_nostr_filters(self, data: Union[dict, list]): filters = data if isinstance(data, list) else [data] diff --git a/tasks.py b/tasks.py index 1566f65..790337c 100644 --- a/tasks.py +++ b/tasks.py @@ -1,5 +1,6 @@ import asyncio import ssl +import json import threading from .crud import get_relays @@ -15,6 +16,13 @@ from .services import ( async def init_relays(): + # we save any subscriptions teporarily to re-add them after reinitializing the client + subscriptions = {} + for relay in nostr.client.relay_manager.relays.values(): + # relay.add_subscription(id, filters) + for subscription_id, filters in relay.subscriptions.items(): + subscriptions[subscription_id] = filters + # reinitialize the entire client nostr.__init__() # get relays from db @@ -22,6 +30,16 @@ async def init_relays(): # set relays and connect to them nostr.client.relays = list(set([r.url for r in relays.__root__ if r.url])) nostr.client.connect() + + await asyncio.sleep(2) + # re-add subscriptions + for subscription_id, subscription in subscriptions.items(): + nostr.client.relay_manager.add_subscription( + subscription_id, subscription.filters + ) + s = subscription.to_json_object() + json_str = json.dumps(["REQ", s["id"], s["filters"][0]]) + nostr.client.relay_manager.publish_message(json_str) return