From 13374917f31333217e5d4b079cc8ee351f18f88e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Sun, 19 Nov 2023 18:09:22 -0300 Subject: [PATCH] Make get_representative() atomic --- bookwyrm/activitypub/base_activity.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py index aa4b5b687..fbbc18f73 100644 --- a/bookwyrm/activitypub/base_activity.py +++ b/bookwyrm/activitypub/base_activity.py @@ -236,7 +236,7 @@ class ActivityObject: omit = kwargs.get("omit", ()) data = self.__dict__.copy() # recursively serialize - for (k, v) in data.items(): + for k, v in data.items(): try: if issubclass(type(v), ActivityObject): data[k] = v.serialize() @@ -397,18 +397,14 @@ def resolve_remote_id( def get_representative(): """Get or create an actor representing the instance to sign outgoing HTTP GET requests""" - username = f"{INSTANCE_ACTOR_USERNAME}@{DOMAIN}" - email = "bookwyrm@localhost" - try: - user = models.User.objects.get(username=username) - except models.User.DoesNotExist: - user = models.User.objects.create_user( - username=username, - email=email, + return models.User.objects.get_or_create( + username=f"{INSTANCE_ACTOR_USERNAME}@{DOMAIN}", + defaults=dict( + email="bookwyrm@localhost", local=True, localname=INSTANCE_ACTOR_USERNAME, - ) - return user + ), + )[0] def get_activitypub_data(url):