From 0cc5c59a737f24cd16c100719f11ccd994fc81d6 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 23 Feb 2023 09:34:14 +0200 Subject: [PATCH 1/6] chore: initial commit --- .gitignore | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..10a11d5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +.DS_Store +._* + +__pycache__ +*.py[cod] +*$py.class +.mypy_cache +.vscode +*-lock.json + +*.egg +*.egg-info +.coverage +.pytest_cache +.webassets-cache +htmlcov +test-reports +tests/data/*.sqlite3 + +*.swo +*.swp +*.pyo +*.pyc +*.env \ No newline at end of file From c97fcec6e916f9db1264e6699511fd679f8060a4 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 23 Feb 2023 11:26:42 +0200 Subject: [PATCH 2/6] refactor: extract `init_filters` and `event_getter` --- views_api.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/views_api.py b/views_api.py index 6ecb02a..4a12998 100644 --- a/views_api.py +++ b/views_api.py @@ -2,13 +2,15 @@ from http import HTTPStatus import asyncio import ssl import json -from fastapi import Request +from typing import List +from fastapi import Request, WebSocket from fastapi.param_functions import Query from fastapi.params import Depends from fastapi.responses import JSONResponse from starlette.exceptions import HTTPException from sse_starlette.sse import EventSourceResponse +from loguru import logger from . import nostrclient_ext @@ -93,8 +95,17 @@ async def api_post_event(event: Event): @nostrclient_ext.post("/api/v1/filters") async def api_subscribe(filters: Filters): + nostr_filters = init_filters(filters.__root__) + + return EventSourceResponse( + event_getter(nostr_filters), + ping=20, + media_type="text/event-stream", + ) + +def init_filters(filters: List[Filter]): filter_list = [] - for filter in filters.__root__: + for filter in filters: filter_list.append( NostrFilter( event_ids=filter.ids, @@ -116,15 +127,11 @@ async def api_subscribe(filters: Filters): request.extend(nostr_filters.to_json_array()) message = json.dumps(request) client.relay_manager.publish_message(message) + return nostr_filters - async def event_getter(): - while True: - event = await received_event_queue.get() - if nostr_filters.match(event): - yield event.to_message() - return EventSourceResponse( - event_getter(), - ping=20, - media_type="text/event-stream", - ) +async def event_getter(nostr_filters): + while True: + event = await received_event_queue.get() + if nostr_filters.match(event): + yield event.to_message() \ No newline at end of file From f601ca38f69759a24edb48f4f9c5a63b3b1b3817 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 23 Feb 2023 11:27:08 +0200 Subject: [PATCH 3/6] feat: add `ws_filter_subscribe` --- views_api.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/views_api.py b/views_api.py index 4a12998..d7de7e8 100644 --- a/views_api.py +++ b/views_api.py @@ -103,6 +103,25 @@ async def api_subscribe(filters: Filters): media_type="text/event-stream", ) + +@nostrclient_ext.websocket("/api/v1/filters") +async def ws_filter_subscribe(websocket: WebSocket): + await websocket.accept() + while True: + json_data = await websocket.receive_text() + print('### nostrclient', json_data) + try: + data = json.loads(json_data) + filters = data if isinstance(data, list) else [data] + filters = [Filter.parse_obj(f) for f in filters] + nostr_filters = init_filters(filters) + async for message in event_getter(nostr_filters): + await websocket.send_text(message) + + except Exception as e: + logger.warning(e) + + def init_filters(filters: List[Filter]): filter_list = [] for filter in filters: From 752fad2e3c98214cb9c1984d7c0446d6bdc5112a Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 23 Feb 2023 11:58:56 +0200 Subject: [PATCH 4/6] fix: allow `ws://` relays --- templates/nostrclient/index.html | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/templates/nostrclient/index.html b/templates/nostrclient/index.html index 1edaed9..79ec7ed 100644 --- a/templates/nostrclient/index.html +++ b/templates/nostrclient/index.html @@ -73,6 +73,7 @@ icon="cancel" color="pink" > + {% endraw %} @@ -203,14 +204,17 @@ }) }, addRelay() { - if (!this.relayToAdd.startsWith("wss://")) { - this.relayToAdd = "" + if ( + !this.relayToAdd.startsWith('wss://') && + !this.relayToAdd.startsWith('ws://') + ) { + this.relayToAdd = '' this.$q.notify({ - timeout: 5000, - type: 'warning', - message: `Invalid relay URL.`, - caption: "Should start with wss://" - }) + timeout: 5000, + type: 'warning', + message: `Invalid relay URL.`, + caption: "Should start with 'wss://'' or 'ws://'" + }) return } console.log("ADD RELAY " + this.relayToAdd) From a24762932cb17c45bf510c5ffd8a546964c3cb9e Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 23 Feb 2023 12:00:49 +0200 Subject: [PATCH 5/6] chore: remove `print()` --- views_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views_api.py b/views_api.py index d7de7e8..86c834e 100644 --- a/views_api.py +++ b/views_api.py @@ -109,7 +109,7 @@ async def ws_filter_subscribe(websocket: WebSocket): await websocket.accept() while True: json_data = await websocket.receive_text() - print('### nostrclient', json_data) + # print('### nostrclient', json_data) try: data = json.loads(json_data) filters = data if isinstance(data, list) else [data] From 380747a72b445a8afc6971fac920caf961e0e6fe Mon Sep 17 00:00:00 2001 From: calle <93376500+callebtc@users.noreply.github.com> Date: Thu, 23 Feb 2023 12:56:56 +0100 Subject: [PATCH 6/6] Update views_api.py --- views_api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/views_api.py b/views_api.py index 86c834e..d4d8601 100644 --- a/views_api.py +++ b/views_api.py @@ -109,7 +109,6 @@ async def ws_filter_subscribe(websocket: WebSocket): await websocket.accept() while True: json_data = await websocket.receive_text() - # print('### nostrclient', json_data) try: data = json.loads(json_data) filters = data if isinstance(data, list) else [data]