add option for multiple users in search, add first version of search ui

This commit is contained in:
Believethehype
2024-01-08 16:41:27 +01:00
parent db3e49f384
commit 7f928ae767
5 changed files with 248 additions and 13 deletions

View File

@@ -47,6 +47,7 @@ class AdvancedSearch(DVMTaskInterface):
# default values
user = ""
users = []
since_days = 800 # days ago
until_days = 0 # days ago
search = ""
@@ -61,7 +62,9 @@ class AdvancedSearch(DVMTaskInterface):
param = tag.as_vec()[1]
if param == "user": # check for param type
user = tag.as_vec()[2]
if param == "since": # check for param type
elif param == "users": # check for param type
user = json.loads(tag.as_vec()[2])
elif param == "since": # check for param type
since_days = int(tag.as_vec()[2])
elif param == "until": # check for param type
until_days = int(tag.as_vec()[2])
@@ -71,6 +74,7 @@ class AdvancedSearch(DVMTaskInterface):
options = {
"search": search,
"user": user,
"users": users,
"since": since_days,
"until": until_days,
"max_results": max_results
@@ -98,16 +102,37 @@ class AdvancedSearch(DVMTaskInterface):
search_until_seconds = int(options["until"]) * 24 * 60 * 60
dif = Timestamp.now().as_secs() - search_until_seconds
search_until = Timestamp.from_secs(dif)
userkeys = []
for user in options["users"]:
user = user.as_json()[1]
user = str(user).lstrip("@")
if str(user).startswith('npub'):
userkey = PublicKey.from_bech32(user)
elif str(user).startswith("nostr:npub"):
userkey = PublicKey.from_nostr_uri(user)
else:
userkey = PublicKey.from_hex(user)
if options["user"] == "":
userkeys.append(userkey)
if not options["users"] and options["user"] == "":
notes_filter = Filter().kind(1).search(options["search"]).since(search_since).until(search_until).limit(
options["max_results"])
elif options["search"] == "":
notes_filter = Filter().kind(1).author(PublicKey.from_hex(options["user"])).since(search_since).until(
search_until).limit(options["max_results"])
if options["users"]:
notes_filter = Filter().kind(1).authors(userkeys).since(search_since).until(
search_until).limit(options["max_results"])
else:
notes_filter = Filter().kind(1).authors([PublicKey.from_hex(options["user"])]).since(search_since).until(
search_until).limit(options["max_results"])
else:
notes_filter = Filter().kind(1).author(PublicKey.from_hex(options["user"])).search(options["search"]).since(
search_since).until(search_until).limit(options["max_results"])
if options["users"]:
notes_filter = Filter().kind(1).authors(userkeys).search(options["search"]).since(
search_since).until(search_until).limit(options["max_results"])
else:
notes_filter = Filter().kind(1).authors([PublicKey.from_hex(options["user"])]).search(options["search"]).since(
search_since).until(search_until).limit(options["max_results"])
events = cli.get_events_of([notes_filter], timedelta(seconds=5))

View File

@@ -217,7 +217,7 @@ def fetch_user_metadata(npub, client):
pk = PublicKey.from_hex(npub)
print(f"\nGetting profile metadata for {pk.to_bech32()}...")
profile_filter = Filter().kind(0).author(pk).limit(1)
events = client.get_events_of([profile_filter], timedelta(seconds=5))
events = client.get_events_of([profile_filter], timedelta(seconds=1))
if len(events) > 0:
latest_entry = events[0]
latest_time = 0

View File

@@ -2,6 +2,7 @@ import json
import os
from datetime import timedelta
from pathlib import Path
from typing import List
import dotenv
from nostr_sdk import Filter, Client, Alphabet, EventId, Event, PublicKey, Tag, Keys, nip04_decrypt, Metadata, Options, \
@@ -36,6 +37,15 @@ def get_event_by_id(event_id: str, client: Client, config=None) -> Event | None:
return None
def get_events_by_id(event_ids: list, client: Client, config=None) -> list[Event] | None:
id_filter = Filter().ids(event_ids)
events = client.get_events_of([id_filter], timedelta(seconds=config.RELAY_TIMEOUT))
if len(events) > 0:
return events
else:
return None
def get_referenced_event_by_id(event_id, client, dvm_config, kinds) -> Event | None:
if kinds is None:
kinds = []
@@ -161,7 +171,6 @@ def update_profile(dvm_config, client, lud16=""):
about = nip89content.get("about")
image = nip89content.get("image")
# Set metadata
metadata = Metadata() \
.set_name(name) \
@@ -170,7 +179,7 @@ def update_profile(dvm_config, client, lud16=""):
.set_picture(image) \
.set_lud16(lud16) \
.set_nip05(lud16)
# .set_banner("https://example.com/banner.png") \
# .set_banner("https://example.com/banner.png") \
print(f"Setting profile metadata for {keys.public_key().to_bech32()}...")
print(metadata.as_json())
@@ -192,4 +201,3 @@ def add_pk_to_env_file(dtag, oskey):
print(f'loading environment from {env_path.resolve()}')
dotenv.load_dotenv(env_path, verbose=True, override=True)
dotenv.set_key(env_path, dtag, oskey)