refactor: mark delete using query builder
This commit is contained in:
parent
f01ea6a237
commit
57197b981d
3 changed files with 29 additions and 15 deletions
|
|
@ -4,7 +4,7 @@ from typing import Any, Callable, List
|
||||||
from fastapi import WebSocket
|
from fastapi import WebSocket
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from .crud import create_event, mark_events_deleted, get_event, get_events
|
from .crud import create_event, get_event, get_events, mark_events_deleted
|
||||||
from .models import NostrEvent, NostrEventType, NostrFilter
|
from .models import NostrEvent, NostrEventType, NostrFilter
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
9
crud.py
9
crud.py
|
|
@ -54,11 +54,11 @@ async def get_event(relay_id: str, id: str) -> Optional[NostrEvent]:
|
||||||
return event
|
return event
|
||||||
|
|
||||||
async def mark_events_deleted(relay_id: str, filter: NostrFilter):
|
async def mark_events_deleted(relay_id: str, filter: NostrFilter):
|
||||||
if len(filter.ids) == 0:
|
if filter.is_empty():
|
||||||
return None
|
return None
|
||||||
ids = ",".join(["?"] * len(filter.ids))
|
_, where, values = build_where_clause(relay_id, filter)
|
||||||
values = [relay_id] + filter.ids
|
|
||||||
await db.execute(f"UPDATE nostrrelay.events SET deleted=true WHERE relay_id = ? AND id IN ({ids})", tuple(values))
|
await db.execute(f"""UPDATE nostrrelay.events SET deleted=true WHERE {" AND ".join(where)}""", tuple(values))
|
||||||
|
|
||||||
|
|
||||||
async def create_event_tags(
|
async def create_event_tags(
|
||||||
|
|
@ -109,6 +109,7 @@ def build_select_events_query(relay_id:str, filter:NostrFilter):
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# todo: check range
|
||||||
if filter.limit and filter.limit > 0:
|
if filter.limit and filter.limit > 0:
|
||||||
query += f" LIMIT {filter.limit}"
|
query += f" LIMIT {filter.limit}"
|
||||||
|
|
||||||
|
|
|
||||||
33
models.py
33
models.py
|
|
@ -20,14 +20,15 @@ class NostrRelay(BaseModel):
|
||||||
def from_row(cls, row: Row) -> "NostrRelay":
|
def from_row(cls, row: Row) -> "NostrRelay":
|
||||||
return cls(**dict(row))
|
return cls(**dict(row))
|
||||||
|
|
||||||
|
|
||||||
class NostrRelayInfo(BaseModel):
|
class NostrRelayInfo(BaseModel):
|
||||||
name: Optional[str]
|
name: Optional[str]
|
||||||
description: Optional[str]
|
description: Optional[str]
|
||||||
pubkey: Optional[str]
|
pubkey: Optional[str]
|
||||||
contact: Optional[str] = "https://t.me/lnbits"
|
contact: Optional[str] = "https://t.me/lnbits"
|
||||||
supported_nips: List[str] = ["NIP01", "NIP09", "NIP11", "NIP15", "NIP20"]
|
supported_nips: List[str] = ["NIP01", "NIP09", "NIP11", "NIP15", "NIP20"]
|
||||||
software: Optional[str] = "LNbist"
|
software: Optional[str] = "LNbist"
|
||||||
version: Optional[str]
|
version: Optional[str]
|
||||||
|
|
||||||
|
|
||||||
class NostrEventType(str, Enum):
|
class NostrEventType(str, Enum):
|
||||||
|
|
@ -80,7 +81,6 @@ class NostrEvent(BaseModel):
|
||||||
if not valid_signature:
|
if not valid_signature:
|
||||||
raise ValueError(f"Invalid signature: '{self.sig}' for event '{self.id}'")
|
raise ValueError(f"Invalid signature: '{self.sig}' for event '{self.id}'")
|
||||||
|
|
||||||
|
|
||||||
def serialize_response(self, subscription_id):
|
def serialize_response(self, subscription_id):
|
||||||
return [NostrEventType.EVENT, subscription_id, dict(self)]
|
return [NostrEventType.EVENT, subscription_id, dict(self)]
|
||||||
|
|
||||||
|
|
@ -128,8 +128,21 @@ class NostrFilter(BaseModel):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
event_tag_values = [t[1] for t in event_tags if t[0] == tag_name]
|
event_tag_values = [t[1] for t in event_tags if t[0] == tag_name]
|
||||||
|
|
||||||
common_tags = [event_tag for event_tag in event_tag_values if event_tag in filter_tags]
|
common_tags = [
|
||||||
|
event_tag for event_tag in event_tag_values if event_tag in filter_tags
|
||||||
|
]
|
||||||
if len(common_tags) == 0:
|
if len(common_tags) == 0:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def is_empty(self):
|
||||||
|
return (
|
||||||
|
len(self.ids) == 0
|
||||||
|
and len(self.authors) == 0
|
||||||
|
and len(self.kinds) == 0
|
||||||
|
and len(self.e) == 0
|
||||||
|
and len(self.p) == 0
|
||||||
|
and (not self.since)
|
||||||
|
and (not self.until)
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue