Skip to content

Commit

Permalink
Fix #642: Race condition searching for unseen users
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgodwin committed Sep 15, 2023
1 parent dd532e4 commit 6c83d7b
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions users/models/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,39 +394,41 @@ def by_username_and_domain(
domain = domain.domain
else:
domain = domain.lower()
try:
if local:
return cls.objects.get(
username__iexact=username,
domain_id=domain,
local=True,
)
else:
return cls.objects.get(
username__iexact=username,
domain_id=domain,
)
except cls.DoesNotExist:
if fetch and not local:
actor_uri, handle = cls.fetch_webfinger(f"{username}@{domain}")
if handle is None:
return None
# See if this actually does match an existing actor
try:
return cls.objects.get(actor_uri=actor_uri)
except cls.DoesNotExist:
pass
# OK, make one
username, domain = handle.split("@")
if not domain_instance:
domain_instance = Domain.get_remote_domain(domain)
return cls.objects.create(
actor_uri=actor_uri,
username=username,
domain_id=domain_instance,
local=False,
)
return None

with transaction.atomic():
try:
if local:
return cls.objects.get(
username__iexact=username,
domain_id=domain,
local=True,
)
else:
return cls.objects.get(
username__iexact=username,
domain_id=domain,
)
except cls.DoesNotExist:
if fetch and not local:
actor_uri, handle = cls.fetch_webfinger(f"{username}@{domain}")
if handle is None:
return None
# See if this actually does match an existing actor
try:
return cls.objects.get(actor_uri=actor_uri)
except cls.DoesNotExist:
pass
# OK, make one
username, domain = handle.split("@")
if not domain_instance:
domain_instance = Domain.get_remote_domain(domain)
return cls.objects.create(
actor_uri=actor_uri,
username=username,
domain_id=domain_instance,
local=False,
)
return None

@classmethod
def by_actor_uri(cls, uri, create=False, transient=False) -> "Identity":
Expand Down

0 comments on commit 6c83d7b

Please sign in to comment.