Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
do_update_messages: Delete user_message if guest user loses access.
Aman Agrawalb7e5b7f1933b
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex 489a7936d6c..44af288349a 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -4522,6 +4522,29 @@ def do_update_message(user_profile: UserProfile, message: Message,event["new_stream_id"] = new_stream.idevent["propagate_mode"] = propagate_mode+ # When messages are moved from one stream to another, some+ # users may lose access to those messages, including guest+ # users and users not subscribed to the new stream (if it is a+ # private stream). For those users, their experience is as+ # though the messages were deleted, and we should send a+ # delete_message event to them instead.++ subscribers = get_active_subscriptions_for_stream_id(+ stream_id).select_related("user_profile")+ subs_to_new_stream = list(get_active_subscriptions_for_stream_id(+ new_stream.id).select_related("user_profile"))++ new_stream_sub_ids = [user.user_profile_id for user in subs_to_new_stream]++ # Get guest users who aren't subscribed to the new_stream.+ guest_subs_losing_access = [+ sub for sub in subscribers+ if sub.user_profile.is_guest+ and sub.user_profile_id not in new_stream_sub_ids+ ]+ ums = ums.exclude(user_profile_id__in=[+ sub.user_profile_id for sub in guest_subs_losing_access])+if topic_name is not None:topic_name = truncate_topic(topic_name)message.set_topic_name(topic_name)@@ -4543,6 +4566,29 @@ def do_update_message(user_profile: UserProfile, message: Message,)changed_messages += messages_list+ if new_stream is not None:+ assert stream_being_edited is not None++ message_ids = [msg.id for msg in changed_messages]+ # Delete UserMessage objects from guest users who will no+ # longer have access to these messages. Note: This could be+ # very expensive, since it's N guest users x M messages.+ UserMessage.objects.filter(+ user_profile_id__in=[sub.user_profile_id for sub in+ guest_subs_losing_access],+ message_id__in=message_ids,+ ).delete()++ delete_event: DeleteMessagesEvent = {+ 'type': 'delete_message',+ 'message_ids': message_ids,+ 'message_type': 'stream',+ 'stream_id': stream_being_edited.id,+ 'topic': orig_topic_name,+ }+ delete_event_notify_user_ids = [sub.user_profile_id for sub in guest_subs_losing_access]+ send_event(user_profile.realm, delete_event, delete_event_notify_user_ids)+if message.edit_history is not None:edit_history = ujson.loads(message.edit_history)edit_history.insert(0, edit_history_event)@@ -4587,8 +4633,31 @@ def do_update_message(user_profile: UserProfile, message: Message,# Remove duplicates by excluding the id of users already in users_to_be_notified list.# This is the case where a user both has a UserMessage row and is a current Subscribersubscribers = subscribers.exclude(user_profile_id__in=[um.user_profile_id for um in ums])++ if new_stream is not None:+ assert guest_subs_losing_access is not None+ # Exclude guest users who are not subscribed to the new stream from receing this event.+ subscribers = subscribers.exclude(user_profile_id__in=[sub.user_profile_id for sub in guest_subs_losing_access])+# All users that are subscribed to the stream must be notified when a message is editedsubscribers_ids = [user.user_profile_id for user in subscribers]++ if new_stream is not None:+ # TODO: Guest users don't see the new moved topic unless breadcrumb message for+ # new stream is enabled. Excluding these users from receiving this event helps+ # us avoid a error trackeback for our clients. We should figure out a way to+ # inform the guest users of this new topic if sending a 'message' event for these messages+ # is not an option.+ # Don't send this event to guest subs who are not subscrbied to the old stream but+ # are subscribed to the new stream+ old_stream_unsubed_guests = [+ sub for sub in subs_to_new_stream+ if sub.user_profile.is_guest+ and sub.user_profile_id not in subscribers_ids+ ]+ subscribers = subscribers.exclude(user_profile_id__in=[sub.user_profile_id for sub in old_stream_unsubed_guests])+ subscribers_ids = [user.user_profile_id for user in subscribers]+users_to_be_notified += list(map(subscriber_info, subscribers_ids))send_event(user_profile.realm, event, users_to_be_notified)diff --git a/zerver/tests/test_messages.py b/zerver/tests/test_messages.pyindex d21df7441b7..8d45f4d7f20 100644--- a/zerver/tests/test_messages.py+++ b/zerver/tests/test_messages.py@@ -56,6 +56,7 @@ from zerver.lib.message import (get_first_visible_message_id,get_raw_unread_data,get_recent_private_conversations,+ has_message_access,maybe_update_first_visible_message_id,messages_for_ids,render_markdown,@@ -3559,7 +3560,7 @@ class EditMessageTest(ZulipTestCase):'propagate_mode': 'change_all','topic': 'new topic',})- self.assertEqual(len(queries), 49)+ self.assertEqual(len(queries), 52)messages = get_topic_messages(user_profile, old_stream, "test")self.assertEqual(len(messages), 1)@@ -3570,6 +3571,29 @@ class EditMessageTest(ZulipTestCase):self.assertEqual(messages[3].content, f"This topic was moved here from #**test move stream>test** by @_**Iago|{user_profile.id}**")self.assert_json_success(result)+ def test_inaccessible_msg_after_stream_change(self) -> None:+ """Simulates the case where message is moved to a stream where user is not a subscribed"""+ (user_profile, old_stream, new_stream, msg_id, msg_id_lt) = self.prepare_move_topics(+ "iago", "test move stream", "new stream", "test")++ guest_user = self.example_user('polonius')+ self.subscribe(guest_user, old_stream.name)+ msg_id_to_test_acesss = self.send_stream_message(user_profile, old_stream.name,+ topic_name='test', content="fourth")++ self.assertEqual(has_message_access(guest_user, Message.objects.get(id=msg_id_to_test_acesss), None), True)++ result = self.client_patch("/json/messages/" + str(msg_id), {+ 'message_id': msg_id,+ 'stream_id': new_stream.id,+ 'propagate_mode': 'change_all',+ 'topic': 'new topic'+ })+ self.assert_json_success(result)++ self.assertEqual(has_message_access(guest_user, Message.objects.get(id=msg_id_to_test_acesss), None), False)+ self.assertEqual(has_message_access(self.example_user('iago'), Message.objects.get(id=msg_id_to_test_acesss), None), True)+def test_no_notify_move_message_to_stream(self) -> None:(user_profile, old_stream, new_stream, msg_id, msg_id_lt) = self.prepare_move_topics("iago", "test move stream", "new stream", "test")