From 99022f2b4a30ff1b0a2e8f8872a6d284df196820 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Fri, 23 Jun 2023 11:15:57 +0300 Subject: [PATCH] refactor: extract `nostr` to `__init__` --- __init__.py | 9 +++++++++ services.py | 10 +--------- tasks.py | 1 + views_api.py | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/__init__.py b/__init__.py index 019df68..e98d8b8 100644 --- a/__init__.py +++ b/__init__.py @@ -8,6 +8,8 @@ from lnbits.db import Database from lnbits.helpers import template_renderer from lnbits.tasks import catch_everything_and_restart +from .nostr.client.client import NostrClient as NostrClientLib + db = Database("ext_nostrclient") nostrclient_static_files = [ @@ -22,6 +24,13 @@ nostrclient_ext: APIRouter = APIRouter(prefix="/nostrclient", tags=["nostrclient scheduled_tasks: List[asyncio.Task] = [] +class NostrClient: + def __init__(self): + self.client: NostrClientLib = NostrClientLib(connect=False) + + +nostr = NostrClient() + def nostr_renderer(): return template_renderer(["lnbits/extensions/nostrclient/templates"]) diff --git a/services.py b/services.py index 347b8ea..3ee9d0a 100644 --- a/services.py +++ b/services.py @@ -7,21 +7,13 @@ from loguru import logger from lnbits.helpers import urlsafe_short_hash +from . import nostr from .models import Event, Filter -from .nostr.client.client import NostrClient as NostrClientLib from .nostr.filter import Filter as NostrFilter from .nostr.filter import Filters as NostrFilters from .nostr.message_pool import EndOfStoredEventsMessage, NoticeMessage -class NostrClient: - def __init__(self): - self.client: NostrClientLib = NostrClientLib(connect=False) - - -nostr = NostrClient() - - class NostrRouter: received_subscription_events: dict[str, list[Event]] = {} diff --git a/tasks.py b/tasks.py index 1595bfd..813a337 100644 --- a/tasks.py +++ b/tasks.py @@ -1,6 +1,7 @@ import asyncio import threading +from . import nostr from .crud import get_relays from .nostr.message_pool import EndOfStoredEventsMessage, EventMessage, NoticeMessage from .services import NostrRouter, nostr diff --git a/views_api.py b/views_api.py index 374e25e..060fcc7 100644 --- a/views_api.py +++ b/views_api.py @@ -9,7 +9,7 @@ from starlette.exceptions import HTTPException from lnbits.decorators import check_admin from lnbits.helpers import urlsafe_short_hash -from . import nostrclient_ext, scheduled_tasks +from . import nostr, nostrclient_ext, scheduled_tasks from .crud import add_relay, delete_relay, get_relays from .helpers import normalize_public_key from .models import Relay, RelayList, TestMessage, TestMessageResponse