Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_flags: Add skipped unsubscribed stream ids in the response.
sanchi-tb2bc8db6bcca
diff --git a/api_docs/changelog.md b/api_docs/changelog.mdindex 7078177fd49..e77e09de711 100644--- a/api_docs/changelog.md+++ b/api_docs/changelog.md@@ -20,6 +20,14 @@ format used by the Zulip server that they are interacting with.## Changes in Zulip 10.0+**Feature level 355**++* [`POST /messages/flags/narrow`](/api/update-message-flags-for-narrow),+ [`POST /messages/flags`](/api/update-message-flags):+ Added `ignored_because_not_subscribed_channels` field in the response, which+ is a list of the channels whose messages were skipped to mark as unread+ because the user is not subscribed to them.+**Feature level 354*** [`GET /messages`](/api/get-messages), [`GETdiff --git a/version.py b/version.pyindex 77d24806da9..fdbfcc7c2dd 100644--- a/version.py+++ b/version.py@@ -34,7 +34,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"# new level means in api_docs/changelog.md, as well as "**Changes**"# entries in the endpoint's documentation in `zulip.yaml`.-API_FEATURE_LEVEL = 354 # Last bumped for accounting groups for has_message_access.+API_FEATURE_LEVEL = 355 # Last bumped for adding 'ignored_because_not_subscribed_channels'# Bump the minor PROVISION_VERSION to indicate that folks should provision# only when going from an old version of the code to a newer version. Bumpdiff --git a/zerver/actions/message_flags.py b/zerver/actions/message_flags.pyindex 71e80e5d43f..514b99e36f1 100644--- a/zerver/actions/message_flags.py+++ b/zerver/actions/message_flags.py@@ -259,7 +259,7 @@ def do_clear_mobile_push_notifications_for_ids(def do_update_message_flags(user_profile: UserProfile, operation: str, flag: str, messages: list[int]-) -> int:+) -> tuple[int, list[int]]:valid_flags = [item for item in UserMessage.flags if item not in UserMessage.NON_API_FLAGS]if flag not in valid_flags:raise JsonableError(_("Invalid flag: '{flag}'").format(flag=flag))@@ -273,6 +273,7 @@ def do_update_message_flags(flagattr = getattr(UserMessage.flags, flag)flag_target = flagattr if is_adding else 0+ ignored_because_not_subscribed_channels = []with transaction.atomic(durable=True):if flag == "read" and not is_adding:# We have an invariant that all stream messages marked as@@ -283,12 +284,20 @@ def do_update_message_flags(# currently subscribed to.subscribed_recipient_ids = get_subscribed_stream_recipient_ids_for_user(user_profile)- message_ids_in_unsubscribed_streams = set(+ messages_in_unsubscribed_streams = set(# Uses index: zerver_message_pkeyMessage.objects.select_related("recipient").filter(id__in=messages, recipient__type=Recipient.STREAM).exclude(recipient_id__in=subscribed_recipient_ids)- .values_list("id", flat=True)+ .values_list("id", "recipient__type_id")+ )++ message_ids_in_unsubscribed_streams = {+ message[0] for message in messages_in_unsubscribed_streams+ }++ ignored_because_not_subscribed_channels = list(+ {message[1] for message in messages_in_unsubscribed_streams})messages = [@@ -395,4 +404,4 @@ def do_update_message_flags(increment=min(1, count),)- return count+ return (count, ignored_because_not_subscribed_channels)diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex 4b90d7a6b8e..304f005ac26 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -7893,8 +7893,26 @@ paths:type: integerdescription: |An array with the IDs of the modified messages.+ ignored_because_not_subscribed_channels:+ type: array+ items:+ type: integer+ description: |+ Only present if the flag is `read` and the operation is `remove`.++ Zulip has an invariant that all unread messages must be in channels+ the user is subscribed to. This field will contain a list of the+ channels whose messages were skipped to mark as unread because the+ user is not subscribed to them.++ **Changes**: New in Zulip 10.0 (feature level 355).example:- {"msg": "", "messages": [4, 18, 15], "result": "success"}+ {+ "msg": "",+ "messages": [4, 18, 15],+ "ignored_because_not_subscribed_channels": [12, 13, 9],+ "result": "success",+ }/messages/flags/narrow:post:operationId: update-message-flags-for-narrow@@ -8089,6 +8107,19 @@ paths:bulk update to decide whether to issueanother request anchored at`last_processed_id`).+ ignored_because_not_subscribed_channels:+ type: array+ items:+ type: integer+ description: |+ Only present if the flag is `read` and the operation is `remove`.++ Zulip has an invariant that all unread messages must be in channels+ the user is subscribed to. This field will contain a list of the+ channels whose messages were skipped to mark as unread because the+ user is not subscribed to them.++ **Changes**: New in Zulip 10.0 (feature level 355).example:{"result": "success",@@ -8099,6 +8130,7 @@ paths:"last_processed_id": 55,"found_oldest": false,"found_newest": true,+ "ignored_because_not_subscribed_channels": [12, 13, 9],}/messages/render:post:diff --git a/zerver/tests/test_message_flags.py b/zerver/tests/test_message_flags.pyindex be02593ce62..bc97b66a2e4 100644--- a/zerver/tests/test_message_flags.py+++ b/zerver/tests/test_message_flags.py@@ -432,6 +432,61 @@ class UnreadCountTests(ZulipTestCase):))+ # Testing the response when marking messages as unread in a+ # narrow that contains messages from unsubscribed streams+ stream_name = "Test Stream"+ stream = self.subscribe(user, stream_name)+ self.subscribe(self.example_user("cordelia"), stream_name)+ message_id = self.send_stream_message(self.example_user("cordelia"), stream_name, "hello")++ self.assert_json_success(+ self.client_post(+ "/json/mark_stream_as_read",+ {+ "stream_id": stream.id,+ },+ )+ )+ um = UserMessage.objects.get(+ user_profile_id=user.id,+ message_id=message_id,+ )+ self.assertTrue(um.flags.read)++ # Unsubscribe the user from the stream+ self.unsubscribe(user, stream_name)++ # Marking recently added message and all other+ # messages added at the start of the test as unread+ # from an interleaved public narrow+ response = self.assert_json_success(+ self.client_post(+ "/json/messages/flags/narrow",+ {+ "anchor": message_id,+ "num_before": 10,+ "num_after": 0,+ "narrow": orjson.dumps([{"operator": "streams", "operand": "public"}]).decode(),+ "op": "remove",+ "flag": "read",+ },+ )+ )++ self.assertEqual(response["processed_count"], 11)+ self.assertEqual(response["updated_count"], 5)+ self.assertEqual(response["first_processed_id"], message_ids[0])+ self.assertEqual(response["last_processed_id"], message_id)+ self.assertEqual(response["found_oldest"], False)+ self.assertEqual(response["found_newest"], False)+ self.assertEqual(response["ignored_because_not_subscribed_channels"], [stream.id])+ self.assertCountEqual(+ UserMessage.objects.filter(user_profile_id=user.id, message_id__in=message_ids)+ .extra(where=[UserMessage.where_unread()]) # noqa: S610+ .values_list("message_id", flat=True),+ message_ids,+ )+def test_update_flags_for_narrow_misuse(self) -> None:self.login("hamlet")diff --git a/zerver/views/message_flags.py b/zerver/views/message_flags.pyindex a6ffc21639f..4c86ccd8eba 100644--- a/zerver/views/message_flags.py+++ b/zerver/views/message_flags.py@@ -53,7 +53,9 @@ def update_message_flags(request_notes = RequestNotes.get_notes(request)assert request_notes.log_data is not None- count = do_update_message_flags(user_profile, operation, flag, messages)+ (count, ignored_because_not_subscribed_channels) = do_update_message_flags(+ user_profile, operation, flag, messages+ )target_count_str = str(len(messages))log_data_str = f"[{operation} {flag}/{target_count_str}] actually {count}"@@ -63,6 +65,7 @@ def update_message_flags(request,data={"messages": messages, # Useless, but included for backwards compatibility.+ "ignored_because_not_subscribed_channels": ignored_because_not_subscribed_channels,},)@@ -110,7 +113,9 @@ def update_message_flags_for_narrow()messages = [row[0] for row in query_info.rows]- updated_count = do_update_message_flags(user_profile, operation, flag, messages)+ (updated_count, ignored_because_not_subscribed_channels) = do_update_message_flags(+ user_profile, operation, flag, messages+ )return json_success(request,@@ -121,6 +126,7 @@ def update_message_flags_for_narrow("last_processed_id": messages[-1] if messages else None,"found_oldest": query_info.found_oldest,"found_newest": query_info.found_newest,+ "ignored_because_not_subscribed_channels": ignored_because_not_subscribed_channels,},)