lemoncurry/wellknowns/views/nostr.py

28 lines
833 B
Python
Raw Permalink Normal View History

2023-08-10 04:05:46 -04:00
from django.http import JsonResponse
from users.models import User
aliases = {"dani": ("_", "00dani")}
2023-08-10 04:05:46 -04:00
unaliases = {alias: name for (name, aliases) in aliases.items() for alias in aliases}
def nostr_json(request) -> JsonResponse:
users = User.objects.filter(nostr_key__isnull=False)
if "name" in request.GET:
name = request.GET["name"]
if name in unaliases:
name = unaliases[name]
users = users.filter(username=name)
names = {u.username: u.nostr_key for u in users}
for name in list(names.keys()):
for alias in aliases.get(name, []):
names[alias] = names[name]
relays = {u.nostr_key: u.nostr_relays for u in users if u.nostr_relays}
response = {"names": names}
if relays:
response["relays"] = relays
return JsonResponse(response)