Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
performance: Fix do_mark_stream_messages_as_read.
Steve Howell31eb97ddde2c
diff --git a/analytics/tests/test_counts.py b/analytics/tests/test_counts.pyindex 17b13be61cd..a12cfcb482b 100644--- a/analytics/tests/test_counts.py+++ b/analytics/tests/test_counts.py@@ -1178,7 +1178,7 @@ class TestLoggingCountStats(AnalyticsTestCase):self.send_stream_message(user1, stream.name)self.send_stream_message(user1, stream.name)- do_mark_stream_messages_as_read(user2, client, stream)+ do_mark_stream_messages_as_read(user2, stream.recipient_id)self.assertEqual(3, UserCount.objects.filter(property=read_count_property).aggregate(Sum('value'))['value__sum'])self.assertEqual(2, UserCount.objects.filter(property=interactions_property)diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex fd098ac93bb..8a6d1979258 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -3098,7 +3098,6 @@ def bulk_remove_subscriptions(users: Iterable[UserProfile],notify_subscriptions_removed(user_profile, streams_by_user[user_profile.id])event = {'type': 'mark_stream_messages_as_read',- 'client_id': acting_client.id,'user_profile_id': user_profile.id,'stream_ids': [stream.id for stream in streams]}queue_json_publish("deferred_work", event)@@ -4142,18 +4141,18 @@ def do_mark_all_as_read(user_profile: UserProfile, client: Client) -> int:return count-def do_mark_stream_messages_as_read(user_profile: UserProfile,- client: Client,- stream: Stream,- topic_name: Optional[str]=None) -> int:+def do_mark_stream_messages_as_read(+ user_profile: UserProfile,+ stream_recipient_id: int,+ topic_name: Optional[str]=None+) -> int:log_statsd_event('mark_stream_as_read')msgs = UserMessage.objects.filter(user_profile=user_profile,)- recipient = stream.recipient- msgs = msgs.filter(message__recipient=recipient)+ msgs = msgs.filter(message__recipient_id=stream_recipient_id)if topic_name:msgs = filter_by_topic_name_via_message(diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.pyindex a10efd82f92..df7878d4754 100644--- a/zerver/tests/test_push_notifications.py+++ b/zerver/tests/test_push_notifications.py@@ -1998,7 +1998,7 @@ class TestClearOnRead(ZulipTestCase):UserMessage.flags.active_mobile_push_notification))with mock_queue_publish("zerver.lib.actions.queue_json_publish") as mock_publish:- do_mark_stream_messages_as_read(hamlet, self.client, stream)+ do_mark_stream_messages_as_read(hamlet, stream.recipient_id)queue_items = [c[0][1] for c in mock_publish.call_args_list]groups = [item['message_ids'] for item in queue_items]diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex 3c6394ca3e2..6c1b1c1dac1 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -1382,7 +1382,7 @@ class StreamAdminTest(ZulipTestCase):those you aren't on."""result = self.attempt_unsubscribe_of_principal(- query_count=20,+ query_count=19,target_users=[self.example_user('cordelia')],is_realm_admin=True,is_subbed=True,@@ -1398,14 +1398,14 @@ class StreamAdminTest(ZulipTestCase):If you're a realm admin, you can remove multiple users from a stream.TODO: We have too many queries for this situation--each additional- user leads to 8 more queries.+ user leads to 7 more queries."""target_users = [self.example_user(name)for name in ['cordelia', 'prospero', 'iago', 'hamlet', 'ZOE']]result = self.attempt_unsubscribe_of_principal(- query_count=52,+ query_count=47,cache_count=9,target_users=target_users,is_realm_admin=True,@@ -1423,7 +1423,7 @@ class StreamAdminTest(ZulipTestCase):are on."""result = self.attempt_unsubscribe_of_principal(- query_count=21,+ query_count=20,target_users=[self.example_user('cordelia')],is_realm_admin=True,is_subbed=True,@@ -1440,7 +1440,7 @@ class StreamAdminTest(ZulipTestCase):streams you aren't on."""result = self.attempt_unsubscribe_of_principal(- query_count=21,+ query_count=20,target_users=[self.example_user('cordelia')],is_realm_admin=True,is_subbed=False,@@ -1457,7 +1457,7 @@ class StreamAdminTest(ZulipTestCase):You can remove others from public streams you're a stream administrator of."""result = self.attempt_unsubscribe_of_principal(- query_count=20,+ query_count=19,target_users=[self.example_user('cordelia')],is_realm_admin=False,is_stream_admin=True,@@ -1478,7 +1478,7 @@ class StreamAdminTest(ZulipTestCase):for name in ['cordelia', 'prospero', 'othello', 'hamlet', 'ZOE']]result = self.attempt_unsubscribe_of_principal(- query_count=52,+ query_count=47,cache_count=9,target_users=target_users,is_realm_admin=False,@@ -1496,7 +1496,7 @@ class StreamAdminTest(ZulipTestCase):You can remove others from private streams you're a stream administrator of."""result = self.attempt_unsubscribe_of_principal(- query_count=21,+ query_count=20,target_users=[self.example_user('cordelia')],is_realm_admin=False,is_stream_admin=True,@@ -1518,7 +1518,7 @@ class StreamAdminTest(ZulipTestCase):def test_admin_remove_others_from_stream_legacy_emails(self) -> None:result = self.attempt_unsubscribe_of_principal(- query_count=20,+ query_count=19,target_users=[self.example_user('cordelia')],is_realm_admin=True,is_subbed=True,@@ -1532,7 +1532,7 @@ class StreamAdminTest(ZulipTestCase):def test_admin_remove_multiple_users_from_stream_legacy_emails(self) -> None:result = self.attempt_unsubscribe_of_principal(- query_count=28,+ query_count=26,target_users=[self.example_user('cordelia'), self.example_user('prospero')],is_realm_admin=True,is_subbed=True,@@ -3258,7 +3258,7 @@ class SubscriptionAPITest(ZulipTestCase):get_client("website"),)- self.assert_length(query_count, 54)+ self.assert_length(query_count, 52)self.assert_length(cache_count, 4)peer_events = [e for e in eventsdiff --git a/zerver/views/message_flags.py b/zerver/views/message_flags.pyindex 5913713b5cb..2a3ca48de12 100644--- a/zerver/views/message_flags.py+++ b/zerver/views/message_flags.py@@ -53,7 +53,7 @@ def mark_stream_as_read(request: HttpRequest,user_profile: UserProfile,stream_id: int=REQ(validator=check_int)) -> HttpResponse:stream, recipient, sub = access_stream_by_id(user_profile, stream_id)- count = do_mark_stream_messages_as_read(user_profile, request.client, stream)+ count = do_mark_stream_messages_as_read(user_profile, recipient.id)log_data_str = f"[{count} updated]"request._log_data["extra"] = log_data_str@@ -78,7 +78,7 @@ def mark_topic_as_read(request: HttpRequest,if not topic_exists:raise JsonableError(_('No such topic \'{}\'').format(topic_name))- count = do_mark_stream_messages_as_read(user_profile, request.client, stream, topic_name)+ count = do_mark_stream_messages_as_read(user_profile, stream.recipient_id, topic_name)log_data_str = f"[{count} updated]"request._log_data["extra"] = log_data_strdiff --git a/zerver/worker/queue_processors.py b/zerver/worker/queue_processors.pyindex 9a270d4e251..300b6a1465b 100644--- a/zerver/worker/queue_processors.py+++ b/zerver/worker/queue_processors.py@@ -89,7 +89,6 @@ from zerver.lib.streams import access_stream_by_idfrom zerver.lib.timestamp import timestamp_to_datetimefrom zerver.lib.url_preview import preview as url_previewfrom zerver.models import (- Client,Message,PreregistrationUser,Realm,@@ -771,7 +770,6 @@ class DeferredWorker(QueueProcessingWorker):def consume(self, event: Dict[str, Any]) -> None:if event['type'] == 'mark_stream_messages_as_read':user_profile = get_user_profile_by_id(event['user_profile_id'])- client = Client.objects.get(id=event['client_id'])for stream_id in event['stream_ids']:# Since the user just unsubscribed, we don't require@@ -779,7 +777,7 @@ class DeferredWorker(QueueProcessingWorker):# streams would never be accessible)(stream, recipient, sub) = access_stream_by_id(user_profile, stream_id,require_active=False)- do_mark_stream_messages_as_read(user_profile, client, stream)+ do_mark_stream_messages_as_read(user_profile, stream.recipient_id)elif event["type"] == 'mark_stream_messages_as_read_for_everyone':# This event is generated by the stream deactivation code path.batch_size = 100