Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

create_user: Use transaction.atomic as decorator with do_reactivate_user.

Sahil Batra6cc468f6d4b3
diff --git a/zerver/actions/create_user.py b/zerver/actions/create_user.py
index fac4db6f666..f1f722f5ff7 100644
--- a/zerver/actions/create_user.py
+++ b/zerver/actions/create_user.py
@@ -315,12 +315,20 @@ def notify_created_user(user_profile: UserProfile) -> None:
if user_ids_with_real_email_access:
person["delivery_email"] = user_profile.delivery_email
event: Dict[str, Any] = dict(type="realm_user", op="add", person=person)
- send_event(user_profile.realm, event, user_ids_with_real_email_access)
+ transaction.on_commit(
+ lambda event=event: send_event(
+ user_profile.realm, event, user_ids_with_real_email_access
+ )
+ )
if user_ids_without_real_email_access:
del person["delivery_email"]
event = dict(type="realm_user", op="add", person=person)
- send_event(user_profile.realm, event, user_ids_without_real_email_access)
+ transaction.on_commit(
+ lambda event=event: send_event(
+ user_profile.realm, event, user_ids_without_real_email_access
+ )
+ )
def created_bot_event(user_profile: UserProfile) -> Dict[str, Any]:
@@ -357,7 +365,9 @@ def created_bot_event(user_profile: UserProfile) -> Dict[str, Any]:
def notify_created_bot(user_profile: UserProfile) -> None:
event = created_bot_event(user_profile)
- send_event(user_profile.realm, event, bot_owner_user_ids(user_profile))
+ transaction.on_commit(
+ lambda: send_event(user_profile.realm, event, bot_owner_user_ids(user_profile))
+ )
def do_create_user(
@@ -535,32 +545,32 @@ def do_activate_mirror_dummy_user(
notify_created_user(user_profile)
[email protected](savepoint=False)
def do_reactivate_user(user_profile: UserProfile, *, acting_user: Optional[UserProfile]) -> None:
"""Reactivate a user that had previously been deactivated"""
- with transaction.atomic():
- change_user_is_active(user_profile, True)
+ change_user_is_active(user_profile, True)
- event_time = timezone_now()
- RealmAuditLog.objects.create(
- realm=user_profile.realm,
- modified_user=user_profile,
- acting_user=acting_user,
- event_type=RealmAuditLog.USER_REACTIVATED,
- event_time=event_time,
- extra_data=orjson.dumps(
- {
- RealmAuditLog.ROLE_COUNT: realm_user_count_by_role(user_profile.realm),
- }
- ).decode(),
- )
- do_increment_logging_stat(
- user_profile.realm,
- COUNT_STATS["active_users_log:is_bot:day"],
- user_profile.is_bot,
- event_time,
- )
- if settings.BILLING_ENABLED:
- update_license_ledger_if_needed(user_profile.realm, event_time)
+ event_time = timezone_now()
+ RealmAuditLog.objects.create(
+ realm=user_profile.realm,
+ modified_user=user_profile,
+ acting_user=acting_user,
+ event_type=RealmAuditLog.USER_REACTIVATED,
+ event_time=event_time,
+ extra_data=orjson.dumps(
+ {
+ RealmAuditLog.ROLE_COUNT: realm_user_count_by_role(user_profile.realm),
+ }
+ ).decode(),
+ )
+ do_increment_logging_stat(
+ user_profile.realm,
+ COUNT_STATS["active_users_log:is_bot:day"],
+ user_profile.is_bot,
+ event_time,
+ )
+ if settings.BILLING_ENABLED:
+ update_license_ledger_if_needed(user_profile.realm, event_time)
notify_created_user(user_profile)
diff --git a/zerver/actions/streams.py b/zerver/actions/streams.py
index 9832492b52d..361333e9f53 100644
--- a/zerver/actions/streams.py
+++ b/zerver/actions/streams.py
@@ -399,7 +399,11 @@ def send_peer_subscriber_events(
stream_ids=[stream_id],
user_ids=sorted(altered_user_ids),
)
- send_event(realm, event, peer_user_ids)
+ transaction.on_commit(
+ lambda event=event, peer_user_ids=peer_user_ids: send_event(
+ realm, event, peer_user_ids
+ )
+ )
public_stream_ids = [
stream_id
@@ -437,7 +441,11 @@ def send_peer_subscriber_events(
stream_ids=[stream_id],
user_ids=sorted(altered_user_ids),
)
- send_event(realm, event, peer_user_ids)
+ transaction.on_commit(
+ lambda event=event, peer_user_ids=peer_user_ids: send_event(
+ realm, event, peer_user_ids
+ )
+ )
for user_id, stream_ids in user_streams.items():
peer_user_ids = public_peer_ids - {user_id}
@@ -447,7 +455,11 @@ def send_peer_subscriber_events(
stream_ids=sorted(stream_ids),
user_ids=[user_id],
)
- send_event(realm, event, peer_user_ids)
+ transaction.on_commit(
+ lambda event=event, peer_user_ids=peer_user_ids: send_event(
+ realm, event, peer_user_ids
+ )
+ )
SubT = Tuple[List[SubInfo], List[SubInfo]]
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index 415c9d5d1db..df730f6557a 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -904,7 +904,7 @@ class NormalActionsTest(BaseAction):
num_events=7,
)
- check_invites_changed("events[3]", events[3])
+ check_invites_changed("events[1]", events[1])
def test_typing_events(self) -> None:
events = self.verify_action(
@@ -1122,20 +1122,20 @@ class NormalActionsTest(BaseAction):
events = self.verify_action(lambda: self.register("[email protected]", "test1"), num_events=5)
self.assert_length(events, 5)
- check_realm_user_add("events[0]", events[0])
+ check_realm_user_add("events[1]", events[1])
new_user_profile = get_user_by_delivery_email("[email protected]", self.user_profile.realm)
self.assertEqual(new_user_profile.delivery_email, "[email protected]")
- check_subscription_peer_add("events[1]", events[1])
+ check_subscription_peer_add("events[4]", events[4])
- check_message("events[2]", events[2])
+ check_message("events[0]", events[0])
self.assertIn(
f'data-user-id="{new_user_profile.id}">test1_zulip.com</span> just signed up for Zulip',
- events[2]["message"]["content"],
+ events[0]["message"]["content"],
)
+ check_user_group_add_members("events[2]", events[2])
check_user_group_add_members("events[3]", events[3])
- check_user_group_add_members("events[4]", events[4])
def test_register_events_email_address_visibility(self) -> None:
do_set_realm_property(
@@ -1147,20 +1147,20 @@ class NormalActionsTest(BaseAction):
events = self.verify_action(lambda: self.register("[email protected]", "test1"), num_events=5)
self.assert_length(events, 5)
- check_realm_user_add("events[0]", events[0])
+ check_realm_user_add("events[1]", events[1])
new_user_profile = get_user_by_delivery_email("[email protected]", self.user_profile.realm)
self.assertEqual(new_user_profile.email, f"user{new_user_profile.id}@zulip.testserver")
- check_subscription_peer_add("events[1]", events[1])
+ check_subscription_peer_add("events[4]", events[4])
- check_message("events[2]", events[2])
+ check_message("events[0]", events[0])
self.assertIn(
f'data-user-id="{new_user_profile.id}">test1_zulip.com</span> just signed up for Zulip',
- events[2]["message"]["content"],
+ events[0]["message"]["content"],
)
+ check_user_group_add_members("events[2]", events[2])
check_user_group_add_members("events[3]", events[3])
- check_user_group_add_members("events[4]", events[4])
def test_alert_words_events(self) -> None:
events = self.verify_action(lambda: do_add_alert_words(self.user_profile, ["alert_word"]))
@@ -2024,7 +2024,7 @@ class NormalActionsTest(BaseAction):
def test_create_bot(self) -> None:
action = lambda: self.create_bot("test")
events = self.verify_action(action, num_events=4)
- check_realm_bot_add("events[1]", events[1])
+ check_realm_bot_add("events[3]", events[3])
action = lambda: self.create_bot(
"test_outgoing_webhook",
@@ -2036,7 +2036,7 @@ class NormalActionsTest(BaseAction):
events = self.verify_action(action, num_events=4)
# The third event is the second call of notify_created_bot, which contains additional
# data for services (in contrast to the first call).
- check_realm_bot_add("events[1]", events[1])
+ check_realm_bot_add("events[3]", events[3])
action = lambda: self.create_bot(
"test_embedded",
@@ -2046,7 +2046,7 @@ class NormalActionsTest(BaseAction):
bot_type=UserProfile.EMBEDDED_BOT,
)
events = self.verify_action(action, num_events=4)
- check_realm_bot_add("events[1]", events[1])
+ check_realm_bot_add("events[3]", events[3])
def test_change_bot_full_name(self) -> None:
bot = self.create_bot("test")