Merge pull request #9 from lnbits/resusbscribe_on_add
Resusbscribe on add
This commit is contained in:
commit
37b48b7c0c
2 changed files with 20 additions and 2 deletions
|
|
@ -45,7 +45,6 @@ class NostrRouter:
|
||||||
except WebSocketDisconnect:
|
except WebSocketDisconnect:
|
||||||
self.connected = False
|
self.connected = False
|
||||||
break
|
break
|
||||||
# print(json_str)
|
|
||||||
|
|
||||||
# registers a subscription if the input was a REQ request
|
# registers a subscription if the input was a REQ request
|
||||||
subscription_id, json_str_rewritten = await self._add_nostr_subscription(
|
subscription_id, json_str_rewritten = await self._add_nostr_subscription(
|
||||||
|
|
@ -81,7 +80,7 @@ class NostrRouter:
|
||||||
}
|
}
|
||||||
|
|
||||||
# this reconstructs the original response from the relay
|
# 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}_") :]
|
s_original = s[len(f"{self.subscription_id_rewrite}_") :]
|
||||||
event_to_forward = ["EVENT", s_original, event_json]
|
event_to_forward = ["EVENT", s_original, event_json]
|
||||||
# print(json.dumps(event_to_forward))
|
# print(json.dumps(event_to_forward))
|
||||||
|
|
@ -104,6 +103,7 @@ class NostrRouter:
|
||||||
async def stop(self):
|
async def stop(self):
|
||||||
for t in self.tasks:
|
for t in self.tasks:
|
||||||
t.cancel()
|
t.cancel()
|
||||||
|
self.connected = False
|
||||||
|
|
||||||
def _marshall_nostr_filters(self, data: Union[dict, list]):
|
def _marshall_nostr_filters(self, data: Union[dict, list]):
|
||||||
filters = data if isinstance(data, list) else [data]
|
filters = data if isinstance(data, list) else [data]
|
||||||
|
|
|
||||||
18
tasks.py
18
tasks.py
|
|
@ -1,5 +1,6 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import ssl
|
import ssl
|
||||||
|
import json
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from .crud import get_relays
|
from .crud import get_relays
|
||||||
|
|
@ -15,6 +16,13 @@ from .services import (
|
||||||
|
|
||||||
|
|
||||||
async def init_relays():
|
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
|
# reinitialize the entire client
|
||||||
nostr.__init__()
|
nostr.__init__()
|
||||||
# get relays from db
|
# get relays from db
|
||||||
|
|
@ -22,6 +30,16 @@ async def init_relays():
|
||||||
# set relays and connect to them
|
# set relays and connect to them
|
||||||
nostr.client.relays = list(set([r.url for r in relays.__root__ if r.url]))
|
nostr.client.relays = list(set([r.url for r in relays.__root__ if r.url]))
|
||||||
nostr.client.connect()
|
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
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue