update client to main

This commit is contained in:
callebtc
2023-02-02 17:05:22 +01:00
parent b1b4e711b2
commit 4a56c00ba0
5 changed files with 39 additions and 319 deletions

View File

@@ -56,8 +56,8 @@ class NostrClient:
def post(self, message: str):
event = Event(self.public_key.hex(), message, kind=EventKind.TEXT_NOTE)
event.sign(self.private_key.hex())
message = json.dumps([ClientMessageType.EVENT, event.to_json_object()])
event.signature = self.private_key.sign_event(event)
message = json.dumps([ClientMessageType.EVENT, event.to_message()])
# print("Publishing message:")
# print(message)
self.relay_manager.publish_message(message)
@@ -101,8 +101,8 @@ class NostrClient:
tags=[["p", to_pubkey.hex()]],
kind=EventKind.ENCRYPTED_DIRECT_MESSAGE,
)
event.sign(self.private_key.hex())
event_message = json.dumps([ClientMessageType.EVENT, event.to_json_object()])
event.signature = self.private_key.sign_event(event)
event_message = json.dumps([ClientMessageType.EVENT, event.to_message()])
# print("DM message:")
# print(event_message)
@@ -114,7 +114,7 @@ class NostrClient:
[
Filter(
kinds=[EventKind.ENCRYPTED_DIRECT_MESSAGE],
tags={"#p": [sender_publickey.hex()]},
pubkey_refs={"#p": [sender_publickey.hex()]},
)
]
)

View File

@@ -4,8 +4,6 @@ from typing import List
from .event import Event, EventKind
class Filter:
"""
NIP-01 filtering.
@@ -17,34 +15,27 @@ class Filter:
added. For example:
# arbitrary tag
filter.add_arbitrary_tag('t', [hashtags])
# promoted to explicit support
Filter(hashtag_refs=[hashtags])
"""
def __init__(
<<<<<<< HEAD
self,
ids: "list[str]" = None,
kinds: "list[int]" = None,
authors: "list[str]" = None,
event_ids: List[str] = None,
kinds: List[EventKind] = None,
authors: List[str] = None,
since: int = None,
until: int = None,
tags: "dict[str, list[str]]" = None,
event_refs: List[
str
] = None, # the "#e" attr; list of event ids referenced in an "e" tag
pubkey_refs: List[
str
] = None, # The "#p" attr; list of pubkeys referenced in a "p" tag
limit: int = None,
) -> None:
self.IDs = ids
=======
self,
event_ids: List[str] = None,
kinds: List[EventKind] = None,
authors: List[str] = None,
since: int = None,
until: int = None,
event_refs: List[str] = None, # the "#e" attr; list of event ids referenced in an "e" tag
pubkey_refs: List[str] = None, # The "#p" attr; list of pubkeys referenced in a "p" tag
limit: int = None) -> None:
self.event_ids = event_ids
>>>>>>> bda320f6d6d5087fe1afecd122831afe025f7633
self.kinds = kinds
self.authors = authors
self.since = since
@@ -55,21 +46,19 @@ class Filter:
self.tags = {}
if self.event_refs:
self.add_arbitrary_tag('e', self.event_refs)
self.add_arbitrary_tag("e", self.event_refs)
if self.pubkey_refs:
self.add_arbitrary_tag('p', self.pubkey_refs)
self.add_arbitrary_tag("p", self.pubkey_refs)
def add_arbitrary_tag(self, tag: str, values: list):
"""
Filter on any arbitrary tag with explicit handling for NIP-01 and NIP-12
single-letter tags.
Filter on any arbitrary tag with explicit handling for NIP-01 and NIP-12
single-letter tags.
"""
# NIP-01 'e' and 'p' tags and any NIP-12 single-letter tags must be prefixed with "#"
# NIP-01 'e' and 'p' tags and any NIP-12 single-letter tags must be prefixed with "#"
tag_key = tag if len(tag) > 1 else f"#{tag}"
self.tags[tag_key] = values
def matches(self, event: Event) -> bool:
if self.event_ids is not None and event.id not in self.event_ids:
return False
@@ -81,16 +70,13 @@ class Filter:
return False
if self.until is not None and event.created_at > self.until:
return False
if (self.event_refs is not None or self.pubkey_refs is not None) and len(event.tags) == 0:
if (self.event_refs is not None or self.pubkey_refs is not None) and len(
event.tags
) == 0:
return False
<<<<<<< HEAD
if self.tags != None:
e_tag_identifiers = [e_tag[0] for e_tag in event.tags]
=======
if self.tags:
e_tag_identifiers = set([e_tag[0] for e_tag in event.tags])
>>>>>>> bda320f6d6d5087fe1afecd122831afe025f7633
for f_tag, f_tag_values in self.tags.items():
# Omit any NIP-01 or NIP-12 "#" chars on single-letter tags
f_tag = f_tag.replace("#", "")
@@ -98,38 +84,26 @@ class Filter:
if f_tag not in e_tag_identifiers:
# Event is missing a tag type that we're looking for
return False
# Multiple values within f_tag_values are treated as OR search; an Event
# needs to match only one.
# Note: an Event could have multiple entries of the same tag type
# (e.g. a reply to multiple people) so we have to check all of them.
match_found = False
for e_tag in event.tags:
<<<<<<< HEAD
if e_tag[1] not in f_tag_values:
return False
=======
if e_tag[0] == f_tag and e_tag[1] in f_tag_values:
match_found = True
break
if not match_found:
return False
>>>>>>> bda320f6d6d5087fe1afecd122831afe025f7633
return True
def to_json_object(self) -> dict:
res = {}
<<<<<<< HEAD
if self.IDs != None:
res["ids"] = self.IDs
if self.kinds != None:
=======
if self.event_ids is not None:
res["ids"] = self.event_ids
if self.kinds is not None:
>>>>>>> bda320f6d6d5087fe1afecd122831afe025f7633
if self.kinds is not None:
res["kinds"] = self.kinds
if self.authors is not None:
res["authors"] = self.authors
@@ -145,10 +119,6 @@ class Filter:
return res
<<<<<<< HEAD
=======
>>>>>>> bda320f6d6d5087fe1afecd122831afe025f7633
class Filters(UserList):
def __init__(self, initlist: "list[Filter]" = []) -> None:
super().__init__(initlist)
@@ -161,8 +131,4 @@ class Filters(UserList):
return False
def to_json_array(self) -> list:
<<<<<<< HEAD
return [filter.to_json_object() for filter in self.data]
=======
return [filter.to_json_object() for filter in self.data]
>>>>>>> bda320f6d6d5087fe1afecd122831afe025f7633