message_edit: Update stream active status when moving messages.

Update the active status of new stream where the messages are moved
into, if appropriate.

Tested by deleting all messages in a stream. Running the command
to update stream status to mark it inactive. Then moving messages
into the stream to check if the status is updated correctly to active.
This commit is contained in:
Aman Agrawal
2024-07-30 02:40:23 +00:00
committed by Tim Abbott
parent 50256f4831
commit 81c345483e
2 changed files with 55 additions and 1 deletions

View File

@@ -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_bot
from 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_result

View File

@@ -11,6 +11,7 @@ from zerver.actions.streams import do_change_stream_post_policy
from zerver.actions.user_groups import check_add_user_group
from zerver.actions.users import do_change_user_role
from zerver.lib.message import has_message_access
from zerver.lib.streams import check_update_all_streams_active_status
from zerver.lib.test_classes import ZulipTestCase, get_topic_messages
from zerver.lib.test_helpers import queries_captured
from 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)