Skip to content

Commit 01ebb2c

Browse files
showelltimabbott
authored andcommitted
refactor: Pass realm to bulk_remove_subscriptions.
We made a very similar change to bulk_add_subscriptions earlier in the year.
1 parent ebbd5f1 commit 01ebb2c

File tree

8 files changed

+35
-23
lines changed

8 files changed

+35
-23
lines changed

zerver/lib/actions.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4184,6 +4184,7 @@ def notify_subscriptions_removed(
41844184

41854185

41864186
def bulk_remove_subscriptions(
4187+
realm: Realm,
41874188
users: Iterable[UserProfile],
41884189
streams: Iterable[Stream],
41894190
*,
@@ -4193,6 +4194,13 @@ def bulk_remove_subscriptions(
41934194
users = list(users)
41944195
streams = list(streams)
41954196

4197+
# Sanity check our callers
4198+
for stream in streams:
4199+
assert stream.realm_id == realm.id
4200+
4201+
for user in users:
4202+
assert user.realm_id == realm.id
4203+
41964204
stream_dict = {stream.id: stream for stream in streams}
41974205

41984206
existing_subs_by_user = get_bulk_stream_subscriber_info(users, streams)
@@ -4226,16 +4234,14 @@ def get_non_subscribed_subs() -> List[Tuple[UserProfile, Stream]]:
42264234
subs_to_deactivate.append(sub_info)
42274235
sub_ids_to_deactivate.append(sub_info.sub.id)
42284236

4229-
our_realm = users[0].realm
4230-
42314237
# We do all the database changes in a transaction to ensure
42324238
# RealmAuditLog entries are atomically created when making changes.
42334239
with transaction.atomic():
4234-
occupied_streams_before = list(get_occupied_streams(our_realm))
4240+
occupied_streams_before = list(get_occupied_streams(realm))
42354241
Subscription.objects.filter(
42364242
id__in=sub_ids_to_deactivate,
42374243
).update(active=False)
4238-
occupied_streams_after = list(get_occupied_streams(our_realm))
4244+
occupied_streams_after = list(get_occupied_streams(realm))
42394245

42404246
# Log subscription activities in RealmAuditLog
42414247
event_time = timezone_now()
@@ -4266,7 +4272,7 @@ def get_non_subscribed_subs() -> List[Tuple[UserProfile, Stream]]:
42664272
for user_profile in users:
42674273
if len(streams_by_user[user_profile.id]) == 0:
42684274
continue
4269-
notify_subscriptions_removed(our_realm, user_profile, streams_by_user[user_profile.id])
4275+
notify_subscriptions_removed(realm, user_profile, streams_by_user[user_profile.id])
42704276

42714277
event = {
42724278
"type": "mark_stream_messages_as_read",
@@ -4276,7 +4282,7 @@ def get_non_subscribed_subs() -> List[Tuple[UserProfile, Stream]]:
42764282
queue_json_publish("deferred_work", event)
42774283

42784284
send_peer_remove_events(
4279-
realm=our_realm,
4285+
realm=realm,
42804286
streams=streams,
42814287
altered_user_dict=altered_user_dict,
42824288
)

zerver/lib/test_classes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,8 +1076,9 @@ def subscribe(self, user_profile: UserProfile, stream_name: str) -> Stream:
10761076
return stream
10771077

10781078
def unsubscribe(self, user_profile: UserProfile, stream_name: str) -> None:
1079+
realm = user_profile.realm
10791080
stream = get_stream(stream_name, user_profile.realm)
1080-
bulk_remove_subscriptions([user_profile], [stream], acting_user=None)
1081+
bulk_remove_subscriptions(realm, [user_profile], [stream], acting_user=None)
10811082

10821083
# Subscribe to a stream by making an API request
10831084
def common_subscribe_to_streams(

zerver/management/commands/merge_streams.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def handle(self, *args: Any, **options: str) -> None:
7474
if len(subs_to_deactivate) > 0:
7575
print(f"Deactivating {len(subs_to_deactivate)} subscriptions")
7676
bulk_remove_subscriptions(
77+
realm,
7778
[sub.user_profile for sub in subs_to_deactivate],
7879
[stream_to_destroy],
7980
acting_user=None,

zerver/management/commands/remove_users_from_stream.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def handle(self, *args: Any, **options: Any) -> None:
2525
stream_name = options["stream"].strip()
2626
stream = get_stream(stream_name, realm)
2727

28-
result = bulk_remove_subscriptions(user_profiles, [stream], acting_user=None)
28+
result = bulk_remove_subscriptions(realm, user_profiles, [stream], acting_user=None)
2929
not_subscribed = result[1]
3030
not_subscribed_users = {tup[0] for tup in not_subscribed}
3131

zerver/tests/test_audit_log.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ def test_subscriptions(self) -> None:
277277
now = timezone_now()
278278

279279
user = self.example_user("hamlet")
280+
realm = user.realm
280281
stream = self.make_stream("test_stream")
281282
acting_user = self.example_user("iago")
282283
bulk_add_subscriptions(user.realm, [stream], [user], acting_user=acting_user)
@@ -293,7 +294,7 @@ def test_subscriptions(self) -> None:
293294
self.assertEqual(modified_stream.id, stream.id)
294295
self.assertEqual(subscription_creation_logs[0].modified_user, user)
295296

296-
bulk_remove_subscriptions([user], [stream], acting_user=acting_user)
297+
bulk_remove_subscriptions(realm, [user], [stream], acting_user=acting_user)
297298
subscription_deactivation_logs = RealmAuditLog.objects.filter(
298299
event_type=RealmAuditLog.SUBSCRIPTION_DEACTIVATED,
299300
event_time__gte=now,

zerver/tests/test_events.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,12 +1920,12 @@ def test_subscribe_other_user_never_subscribed(self) -> None:
19201920
check_subscription_peer_add("events[1]", events[1])
19211921

19221922
def test_remove_other_user_never_subscribed(self) -> None:
1923-
self.subscribe(self.example_user("othello"), "test_stream")
1923+
othello = self.example_user("othello")
1924+
realm = othello.realm
1925+
self.subscribe(othello, "test_stream")
19241926
stream = get_stream("test_stream", self.user_profile.realm)
19251927

1926-
action = lambda: bulk_remove_subscriptions(
1927-
[self.example_user("othello")], [stream], acting_user=None
1928-
)
1928+
action = lambda: bulk_remove_subscriptions(realm, [othello], [stream], acting_user=None)
19291929
events = self.verify_action(action)
19301930
check_subscription_peer_remove("events[0]", events[0])
19311931

@@ -2476,13 +2476,15 @@ def do_test_subscribe_events(self, include_subscribers: bool) -> None:
24762476
)
24772477
check_subscription_peer_add("events[0]", events[0])
24782478

2479+
hamlet = self.example_user("hamlet")
2480+
iago = self.example_user("iago")
2481+
othello = self.example_user("othello")
2482+
realm = othello.realm
24792483
stream = get_stream("test_stream", self.user_profile.realm)
24802484

24812485
# Now remove the first user, to test the normal unsubscribe flow and
24822486
# 'peer_remove' event for subscribed streams.
2483-
action = lambda: bulk_remove_subscriptions(
2484-
[self.example_user("othello")], [stream], acting_user=None
2485-
)
2487+
action = lambda: bulk_remove_subscriptions(realm, [othello], [stream], acting_user=None)
24862488
events = self.verify_action(
24872489
action,
24882490
include_subscribers=include_subscribers,
@@ -2491,9 +2493,7 @@ def do_test_subscribe_events(self, include_subscribers: bool) -> None:
24912493
check_subscription_peer_remove("events[0]", events[0])
24922494

24932495
# Now remove the user himself, to test the 'remove' event flow
2494-
action = lambda: bulk_remove_subscriptions(
2495-
[self.example_user("hamlet")], [stream], acting_user=None
2496-
)
2496+
action = lambda: bulk_remove_subscriptions(realm, [hamlet], [stream], acting_user=None)
24972497
events = self.verify_action(
24982498
action, include_subscribers=include_subscribers, include_streams=False, num_events=2
24992499
)
@@ -2515,9 +2515,7 @@ def do_test_subscribe_events(self, include_subscribers: bool) -> None:
25152515
check_subscription_peer_add("events[0]", events[0])
25162516

25172517
# Remove the user to test 'peer_remove' event flow for unsubscribed stream.
2518-
action = lambda: bulk_remove_subscriptions(
2519-
[self.example_user("iago")], [stream], acting_user=None
2520-
)
2518+
action = lambda: bulk_remove_subscriptions(realm, [iago], [stream], acting_user=None)
25212519
events = self.verify_action(
25222520
action,
25232521
include_subscribers=include_subscribers,

zerver/tests/test_subs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3976,6 +3976,8 @@ def test_users_getting_remove_peer_event(self) -> None:
39763976
user5 = self.example_user("AARON")
39773977
guest = self.example_user("polonius")
39783978

3979+
realm = user1.realm
3980+
39793981
stream1 = self.make_stream("stream1")
39803982
stream2 = self.make_stream("stream2")
39813983
stream3 = self.make_stream("stream3")
@@ -4000,6 +4002,7 @@ def test_users_getting_remove_peer_event(self) -> None:
40004002
with queries_captured() as query_count:
40014003
with cache_tries_captured() as cache_count:
40024004
bulk_remove_subscriptions(
4005+
realm,
40034006
[user1, user2],
40044007
[stream1, stream2, stream3, private],
40054008
acting_user=None,
@@ -4067,6 +4070,7 @@ def test_bulk_subscribe_MIT(self) -> None:
40674070

40684071
with self.tornado_redirected_to_list(events, expected_num_events=0):
40694072
bulk_remove_subscriptions(
4073+
realm,
40704074
users=[mit_user],
40714075
streams=streams,
40724076
acting_user=None,

zerver/views/streams.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ def remove_subscriptions_backend(
405405
),
406406
) -> HttpResponse:
407407

408+
realm = user_profile.realm
408409
removing_someone_else = check_if_removing_someone_else(user_profile, principals)
409410

410411
streams_as_dict: List[StreamDict] = []
@@ -424,7 +425,7 @@ def remove_subscriptions_backend(
424425

425426
result: Dict[str, List[str]] = dict(removed=[], not_removed=[])
426427
(removed, not_subscribed) = bulk_remove_subscriptions(
427-
people_to_unsub, streams, acting_user=user_profile
428+
realm, people_to_unsub, streams, acting_user=user_profile
428429
)
429430

430431
for (subscriber, removed_stream) in removed:

0 commit comments

Comments
 (0)