summaryrefslogtreecommitdiffstats
path: root/users/shortcuts.py
blob: 9aadb6638f672a2b9bbe0ce5a9d9fe1e20de8c98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from django.http import Http404

from users.models import Domain, Identity


def by_handle_or_404(request, handle, local=True, fetch=False) -> Identity:
    """
    Retrieves an Identity by its long or short handle.
    Domain-sensitive, so it will understand short handles on alternate domains.
    """
    if "@" not in handle:
        if "host" not in request.headers:
            raise Http404("No hostname available")
        username = handle
        domain_instance = Domain.get_domain(request.headers["host"])
        if domain_instance is None:
            raise Http404("No matching domains found")
        domain = domain_instance.domain
    else:
        username, domain = handle.split("@", 1)
        # Resolve the domain to the display domain
        domain_instance = Domain.get_domain(domain)
        if domain_instance is None:
            domain_instance = Domain.get_remote_domain(domain)
        domain = domain_instance.domain
    identity = Identity.by_username_and_domain(
        username,
        domain,
        local=local,
        fetch=fetch,
    )
    if identity is None:
        raise Http404(f"No identity for handle {handle}")
    if identity.blocked:
        raise Http404("Blocked user")
    return identity