Fix #642: Race condition searching for unseen users

This commit is contained in:
Andrew Godwin 2023-09-15 10:21:33 -06:00
parent dd532e4425
commit 6c83d7b67b
1 changed files with 35 additions and 33 deletions

View File

@ -394,39 +394,41 @@ class Identity(StatorModel):
domain = domain.domain domain = domain.domain
else: else:
domain = domain.lower() domain = domain.lower()
try:
if local: with transaction.atomic():
return cls.objects.get( try:
username__iexact=username, if local:
domain_id=domain, return cls.objects.get(
local=True, username__iexact=username,
) domain_id=domain,
else: local=True,
return cls.objects.get( )
username__iexact=username, else:
domain_id=domain, return cls.objects.get(
) username__iexact=username,
except cls.DoesNotExist: domain_id=domain,
if fetch and not local: )
actor_uri, handle = cls.fetch_webfinger(f"{username}@{domain}") except cls.DoesNotExist:
if handle is None: if fetch and not local:
return None actor_uri, handle = cls.fetch_webfinger(f"{username}@{domain}")
# See if this actually does match an existing actor if handle is None:
try: return None
return cls.objects.get(actor_uri=actor_uri) # See if this actually does match an existing actor
except cls.DoesNotExist: try:
pass return cls.objects.get(actor_uri=actor_uri)
# OK, make one except cls.DoesNotExist:
username, domain = handle.split("@") pass
if not domain_instance: # OK, make one
domain_instance = Domain.get_remote_domain(domain) username, domain = handle.split("@")
return cls.objects.create( if not domain_instance:
actor_uri=actor_uri, domain_instance = Domain.get_remote_domain(domain)
username=username, return cls.objects.create(
domain_id=domain_instance, actor_uri=actor_uri,
local=False, username=username,
) domain_id=domain_instance,
return None local=False,
)
return None
@classmethod @classmethod
def by_actor_uri(cls, uri, create=False, transient=False) -> "Identity": def by_actor_uri(cls, uri, create=False, transient=False) -> "Identity":