alert_words: Update add_alert_words codepath to send event on commit.

Earlier, we were using 'send_event' in 'do_add_alert_words' which
can lead to a situation, if any db operation is added after the
'send_event' in future, where we enqueue events but the action
function fails at a later stage.

Events should not be sent until we know we're not rolling back.

Fixes part of #30489.
This commit is contained in:
Prakhar Pratyush
2024-07-30 18:13:40 +05:30
committed by Tim Abbott
parent eab78d996d
commit ba80266b7e
2 changed files with 6 additions and 3 deletions

View File

@@ -1,15 +1,18 @@
from collections.abc import Iterable, Sequence
from django.db import transaction
from zerver.lib.alert_words import add_user_alert_words, remove_user_alert_words
from zerver.models import UserProfile
from zerver.tornado.django_api import send_event
from zerver.tornado.django_api import send_event_on_commit
def notify_alert_words(user_profile: UserProfile, words: Sequence[str]) -> None:
event = dict(type="alert_words", alert_words=words)
send_event(user_profile.realm, event, [user_profile.id])
send_event_on_commit(user_profile.realm, event, [user_profile.id])
@transaction.atomic(savepoint=False)
def do_add_alert_words(user_profile: UserProfile, alert_words: Iterable[str]) -> None:
words = add_user_alert_words(user_profile, alert_words)
notify_alert_words(user_profile, words)