mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-23 19:54:18 +01:00
add nostr
This commit is contained in:
43
nostr/relay_manager.py
Normal file
43
nostr/relay_manager.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import threading
|
||||
from .filter import Filters
|
||||
from .message_pool import MessagePool
|
||||
from .relay import Relay, RelayPolicy
|
||||
|
||||
class RelayManager:
|
||||
def __init__(self) -> None:
|
||||
self.relays: dict[str, Relay] = {}
|
||||
self.message_pool = MessagePool()
|
||||
|
||||
def add_relay(self, url: str, read: bool=True, write: bool=True, subscriptions={}):
|
||||
policy = RelayPolicy(read, write)
|
||||
relay = Relay(url, policy, self.message_pool, subscriptions)
|
||||
self.relays[url] = relay
|
||||
|
||||
def remove_relay(self, url: str):
|
||||
self.relays.pop(url)
|
||||
|
||||
def add_subscription(self, id: str, filters: Filters):
|
||||
for relay in self.relays.values():
|
||||
relay.add_subscription(id, filters)
|
||||
|
||||
def close_subscription(self, id: str):
|
||||
for relay in self.relays.values():
|
||||
relay.close_subscription(id)
|
||||
|
||||
def open_connections(self, ssl_options: dict=None):
|
||||
for relay in self.relays.values():
|
||||
threading.Thread(
|
||||
target=relay.connect,
|
||||
args=(ssl_options,),
|
||||
name=f"{relay.url}-thread"
|
||||
).start()
|
||||
|
||||
def close_connections(self):
|
||||
for relay in self.relays.values():
|
||||
relay.close()
|
||||
|
||||
def publish_message(self, message: str):
|
||||
for relay in self.relays.values():
|
||||
if relay.policy.should_write:
|
||||
relay.publish(message)
|
||||
|
||||
Reference in New Issue
Block a user