2022-11-06 12:48:04 -08:00
|
|
|
from django.http import Http404
|
2022-11-05 13:17:27 -07:00
|
|
|
|
2022-11-06 12:48:04 -08:00
|
|
|
from users.models import Domain, Identity
|
2022-11-05 13:17:27 -07:00
|
|
|
|
|
|
|
|
2022-11-09 21:29:33 -08:00
|
|
|
def by_handle_or_404(request, handle, local=True, fetch=False) -> Identity:
|
2022-11-05 13:17:27 -07:00
|
|
|
"""
|
|
|
|
Retrieves an Identity by its long or short handle.
|
|
|
|
Domain-sensitive, so it will understand short handles on alternate domains.
|
|
|
|
"""
|
|
|
|
if "@" not in handle:
|
2022-12-05 13:44:50 -08:00
|
|
|
if "host" not in request.headers:
|
2022-11-06 12:48:04 -08:00
|
|
|
raise Http404("No hostname available")
|
|
|
|
username = handle
|
2022-12-05 13:44:50 -08:00
|
|
|
domain_instance = Domain.get_domain(request.headers["host"])
|
2022-11-06 12:48:04 -08:00
|
|
|
if domain_instance is None:
|
|
|
|
raise Http404("No matching domains found")
|
|
|
|
domain = domain_instance.domain
|
|
|
|
else:
|
|
|
|
username, domain = handle.split("@", 1)
|
2023-12-13 01:04:41 -08:00
|
|
|
if not Domain.is_valid_domain(domain):
|
|
|
|
raise Http404("Invalid domain")
|
2022-11-06 23:19:00 -08:00
|
|
|
# Resolve the domain to the display domain
|
2022-11-10 22:42:43 -08:00
|
|
|
domain_instance = Domain.get_domain(domain)
|
|
|
|
if domain_instance is None:
|
|
|
|
domain_instance = Domain.get_remote_domain(domain)
|
|
|
|
domain = domain_instance.domain
|
2022-11-06 23:19:00 -08:00
|
|
|
identity = Identity.by_username_and_domain(
|
|
|
|
username,
|
2022-12-28 09:52:39 -08:00
|
|
|
domain_instance,
|
2022-11-06 23:19:00 -08:00
|
|
|
local=local,
|
|
|
|
fetch=fetch,
|
|
|
|
)
|
2022-11-06 20:30:07 -08:00
|
|
|
if identity is None:
|
|
|
|
raise Http404(f"No identity for handle {handle}")
|
2022-12-16 18:42:48 -08:00
|
|
|
if identity.blocked:
|
|
|
|
raise Http404("Blocked user")
|
2022-11-06 20:30:07 -08:00
|
|
|
return identity
|
2023-05-03 21:42:37 -07:00
|
|
|
|
|
|
|
|
|
|
|
def by_handle_for_user_or_404(request, handle):
|
|
|
|
"""
|
|
|
|
Retrieves an identity the local user can control via their handle, or
|
|
|
|
raises a 404.
|
|
|
|
"""
|
|
|
|
identity = by_handle_or_404(request, handle, local=True, fetch=False)
|
|
|
|
if not identity.users.filter(id=request.user.id).exists():
|
|
|
|
raise Http404("Current user does not own identity")
|
|
|
|
return identity
|