emoji: Use a transaction, rather than create and delete-on-fail.

This commit is contained in:
Alex Vandiver 2024-07-12 01:24:38 +00:00 committed by Tim Abbott
parent 2b3da0e70f
commit 6110ac7339
1 changed files with 6 additions and 9 deletions

View File

@ -22,6 +22,7 @@ def notify_realm_emoji(realm: Realm, realm_emoji: Dict[str, EmojiInfo]) -> None:
send_event_on_commit(realm, event, active_user_ids(realm.id))
@transaction.atomic(durable=True)
def check_add_realm_emoji(
realm: Realm, name: str, author: UserProfile, image_file: IO[bytes], content_type: str
) -> RealmEmoji:
@ -30,7 +31,10 @@ def check_add_realm_emoji(
realm_emoji.full_clean()
realm_emoji.save()
except (IntegrityError, ValidationError):
# Match the string in upload_emoji.
# This exists to handle races; upload_emoji checked prior to
# calling, and also hand-validated the name, but we're now in
# a transaction. The error string should match the one in
# upload_emoji.
raise JsonableError(_("A custom emoji with this name already exists."))
# This mirrors the check in upload_emoji_image because we want to
@ -40,15 +44,8 @@ def check_add_realm_emoji(
raise BadImageError(_("Invalid image format"))
emoji_file_name = get_emoji_file_name(content_type, realm_emoji.id)
emoji_uploaded_successfully = False
is_animated = False
try:
is_animated = upload_emoji_image(image_file, emoji_file_name, author, content_type)
emoji_uploaded_successfully = True
finally:
if not emoji_uploaded_successfully:
realm_emoji.delete()
realm_emoji.file_name = emoji_file_name
realm_emoji.is_animated = is_animated
realm_emoji.save(update_fields=["file_name", "is_animated"])