mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-08 07:18:47 +00:00
Merge pull request #809 from PR0M3TH3AN/codex/refactor-eventhandler-to-use-nostr_sdk
Use nostr_sdk Event and update handler tests
This commit is contained in:
@@ -3,14 +3,7 @@
|
|||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
try:
|
from nostr_sdk import Event
|
||||||
from monstr.event.event import Event
|
|
||||||
except ImportError: # pragma: no cover - optional dependency
|
|
||||||
|
|
||||||
class Event: # minimal placeholder for type hints when monstr is absent
|
|
||||||
id: str
|
|
||||||
created_at: int
|
|
||||||
content: str
|
|
||||||
|
|
||||||
|
|
||||||
# Instantiate the logger
|
# Instantiate the logger
|
||||||
@@ -26,26 +19,15 @@ class EventHandler:
|
|||||||
pass # Initialize if needed
|
pass # Initialize if needed
|
||||||
|
|
||||||
def handle_new_event(self, evt: Event):
|
def handle_new_event(self, evt: Event):
|
||||||
"""
|
"""Process and log details from a Nostr event."""
|
||||||
Processes incoming events by logging their details.
|
|
||||||
|
|
||||||
:param evt: The received Event object.
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
# Assuming evt.created_at is always an integer Unix timestamp
|
created_at = evt.created_at().as_secs()
|
||||||
if isinstance(evt.created_at, int):
|
created_at_str = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(created_at))
|
||||||
created_at_str = time.strftime(
|
event_id = evt.id().to_hex()
|
||||||
"%Y-%m-%d %H:%M:%S", time.gmtime(evt.created_at)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# Handle unexpected types gracefully
|
|
||||||
created_at_str = str(evt.created_at)
|
|
||||||
|
|
||||||
# Log the event details without extra newlines
|
|
||||||
logger.info(
|
logger.info(
|
||||||
f"[New Event] ID: {evt.id} | Created At: {created_at_str} | Content: {evt.content}"
|
f"[New Event] ID: {event_id} | Created At: {created_at_str} | Content: {evt.content()}"
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error handling new event: {e}", exc_info=True)
|
logger.error(f"Error handling new event: {e}", exc_info=True)
|
||||||
# Optionally, handle the exception without re-raising
|
|
||||||
# For example, continue processing other events
|
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -6,31 +7,31 @@ from pathlib import Path
|
|||||||
sys.path.append(str(Path(__file__).resolve().parents[1]))
|
sys.path.append(str(Path(__file__).resolve().parents[1]))
|
||||||
|
|
||||||
from nostr import event_handler
|
from nostr import event_handler
|
||||||
|
from nostr_sdk import EventBuilder, Keys
|
||||||
|
|
||||||
class SimpleEvent:
|
|
||||||
def __init__(self, id: str, created_at: int, content: str) -> None:
|
|
||||||
self.id = id
|
|
||||||
self.created_at = created_at
|
|
||||||
self.content = content
|
|
||||||
|
|
||||||
|
|
||||||
def test_handle_new_event_logs(caplog):
|
def test_handle_new_event_logs(caplog):
|
||||||
handler = event_handler.EventHandler()
|
handler = event_handler.EventHandler()
|
||||||
evt = SimpleEvent("1", 0, "hello")
|
keys = Keys.generate()
|
||||||
|
evt = EventBuilder.text_note("hello").sign_with_keys(keys)
|
||||||
|
|
||||||
caplog.set_level(logging.INFO, logger=event_handler.logger.name)
|
caplog.set_level(logging.INFO, logger=event_handler.logger.name)
|
||||||
handler.handle_new_event(evt)
|
handler.handle_new_event(evt)
|
||||||
|
|
||||||
|
event_id = evt.id().to_hex()
|
||||||
|
created_at = evt.created_at().as_secs()
|
||||||
|
created_at_str = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(created_at))
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
"[New Event] ID: 1 | Created At: 1970-01-01 00:00:00 | Content: hello"
|
f"[New Event] ID: {event_id} | Created At: {created_at_str} | Content: hello"
|
||||||
in caplog.text
|
in caplog.text
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_handle_new_event_error(monkeypatch, caplog):
|
def test_handle_new_event_error(monkeypatch, caplog):
|
||||||
handler = event_handler.EventHandler()
|
handler = event_handler.EventHandler()
|
||||||
evt = SimpleEvent("2", 0, "boom")
|
keys = Keys.generate()
|
||||||
|
evt = EventBuilder.text_note("boom").sign_with_keys(keys)
|
||||||
|
|
||||||
def raise_info(*args, **kwargs):
|
def raise_info(*args, **kwargs):
|
||||||
raise RuntimeError("fail")
|
raise RuntimeError("fail")
|
||||||
|
Reference in New Issue
Block a user