From f6fba19ac4e4f4fec65e47f4cf8a51ca441b3aab Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 31 Jul 2023 17:09:52 -0700 Subject: [PATCH] Only trigger add_status_task when status is first created I think the reason I didn't do this initially was so that related users and books, which are added necessarily after the model instance is crated, will be part of the object when the task runs, but I have investigated this and because of the transaction.atomic statement in the to_model method in bookwyrm/activitypub/base_activity.py and in the status view (added in this commit), this is not an issue. --- bookwyrm/activitystreams.py | 7 +++---- bookwyrm/views/status.py | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/bookwyrm/activitystreams.py b/bookwyrm/activitystreams.py index 3e1432a4b..71f9e42d7 100644 --- a/bookwyrm/activitystreams.py +++ b/bookwyrm/activitystreams.py @@ -329,10 +329,9 @@ def add_status_on_create(sender, instance, created, *args, **kwargs): remove_status_task.delay(instance.id) return - # To avoid creating a zillion unnecessary tasks caused by re-saving the model, - # check if it's actually ready to send before we go. We're trusting this was - # set correctly by the inbox or view - if not instance.ready: + # We don't want to create multiple add_status_tasks for each status, and because + # the transactions are atomic, on_commit won't run until the status is ready to add. + if not created: return # when creating new things, gotta wait on the transaction diff --git a/bookwyrm/views/status.py b/bookwyrm/views/status.py index e3a7481f8..5285b9a0a 100644 --- a/bookwyrm/views/status.py +++ b/bookwyrm/views/status.py @@ -6,6 +6,7 @@ from urllib.parse import urlparse from django.contrib.auth.decorators import login_required from django.core.validators import URLValidator from django.core.exceptions import ValidationError +from django.db import transaction from django.db.models import Q from django.http import HttpResponse, HttpResponseBadRequest, Http404 from django.shortcuts import get_object_or_404, redirect @@ -56,6 +57,7 @@ class CreateStatus(View): return TemplateResponse(request, "compose.html", data) # pylint: disable=too-many-branches + @transaction.atomic def post(self, request, status_type, existing_status_id=None): """create status of whatever type""" created = not existing_status_id @@ -83,7 +85,6 @@ class CreateStatus(View): return redirect_to_referer(request) status = form.save(request, commit=False) - status.ready = False # save the plain, unformatted version of the status for future editing status.raw_content = status.content if hasattr(status, "quote"): @@ -123,7 +124,6 @@ class CreateStatus(View): if hasattr(status, "quote"): status.quote = to_markdown(status.quote) - status.ready = True status.save(created=created) # update a readthrough, if needed