Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_edit: Update stream active status when moving messages.
Aman Agrawal81c345483ee9
diff --git a/zerver/actions/message_edit.py b/zerver/actions/message_edit.pyindex 5336c12baa1..62ab01ee7c3 100644--- a/zerver/actions/message_edit.py+++ b/zerver/actions/message_edit.py@@ -83,7 +83,7 @@ from zerver.models import (UserTopic,)from zerver.models.streams import get_stream_by_id_in_realm-from zerver.models.users import get_system_bot+from zerver.models.users import active_user_ids, get_system_botfrom zerver.tornado.django_api import send_event_on_commit@@ -1404,4 +1404,31 @@ def check_update_message(}queue_json_publish("embed_links", event_data)+ # Update stream active status after we have successfully moved the+ # messages. We only update the new stream here and let the daily+ # cron job handle updating the old stream. User might still want+ # to interact with the old stream and keeping it placed in the same+ # position in the left sidebar might help user.+ if stream_id is not None and new_stream is not None and not new_stream.is_recently_active:+ date_days_ago = timezone_now() - timedelta(days=Stream.LAST_ACTIVITY_DAYS_BEFORE_FOR_ACTIVE)+ is_stream_active = Message.objects.filter(+ date_sent__gte=date_days_ago,+ recipient__type=Recipient.STREAM,+ realm=user_profile.realm,+ recipient__type_id=stream_id,+ ).exists()++ if is_stream_active != new_stream.is_recently_active:+ new_stream.is_recently_active = is_stream_active+ new_stream.save(update_fields=["is_recently_active"])+ event = dict(+ type="stream",+ op="update",+ property="is_recently_active",+ value=is_stream_active,+ stream_id=stream_id,+ name=new_stream.name,+ )+ send_event_on_commit(user_profile.realm, event, active_user_ids(user_profile.realm_id))+return updated_message_resultdiff --git a/zerver/tests/test_message_move_stream.py b/zerver/tests/test_message_move_stream.pyindex 6b4fd867976..a23709776e5 100644--- a/zerver/tests/test_message_move_stream.py+++ b/zerver/tests/test_message_move_stream.py@@ -11,6 +11,7 @@ from zerver.actions.streams import do_change_stream_post_policyfrom zerver.actions.user_groups import check_add_user_groupfrom zerver.actions.users import do_change_user_rolefrom zerver.lib.message import has_message_access+from zerver.lib.streams import check_update_all_streams_active_statusfrom zerver.lib.test_classes import ZulipTestCase, get_topic_messagesfrom zerver.lib.test_helpers import queries_capturedfrom zerver.lib.url_encoding import near_stream_message_url@@ -1906,3 +1907,29 @@ class MessageMoveStreamTest(ZulipTestCase):),True,)++ def test_move_message_update_stream_active_status(self) -> None:+ (user_profile, old_stream, new_stream, msg_id, msg_id_later) = self.prepare_move_topics(+ "iago", "test move stream", "new stream", "test"+ )++ # Delete all messages in new stream and mark it as inactive.+ Message.objects.filter(recipient__type_id=new_stream.id, realm=user_profile.realm).delete()++ check_update_all_streams_active_status()+ new_stream.refresh_from_db()+ self.assertFalse(new_stream.is_recently_active)++ # Move the message to new stream should make active again.+ result = self.client_patch(+ f"/json/messages/{msg_id_later}",+ {+ "stream_id": new_stream.id,+ "propagate_mode": "change_later",+ "send_notification_to_new_thread": "false",+ },+ )+ self.assert_json_success(result)++ new_stream.refresh_from_db()+ self.assertTrue(new_stream.is_recently_active)