Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
initial_state_data: Add support for empty topic name.
Prakhar Pratyush398b8b11db3f
diff --git a/api_docs/changelog.md b/api_docs/changelog.mdindex 114323d88a6..2be3e2dfca0 100644--- a/api_docs/changelog.md+++ b/api_docs/changelog.md@@ -54,6 +54,12 @@ format used by the Zulip server that they are interacting with.Added `allow_empty_topic_name` boolean parameter to decide whether thetopic names in the fetched message history objects can be empty strings.+* [`POST /register`](/api/register-queue): For clients that don't support+ the `empty_topic_name` [client capability](/api/register-queue#parameter-client_capabilities),+ the `topic` field in the `unread_msgs` object will have the value+ of `realm_empty_topic_display_name` field replacing the empty string+ for channel messages.+**Feature level 333*** [Message formatting](/api/message-formatting): System groups can nowdiff --git a/zerver/lib/events.py b/zerver/lib/events.pyindex f8a46412ca7..8632f642369 100644--- a/zerver/lib/events.py+++ b/zerver/lib/events.py@@ -1840,7 +1840,12 @@ def do_events_register(include_deactivated_groups=include_deactivated_groups,)- post_process_state(user_profile, ret, notification_settings_null=False)+ post_process_state(+ user_profile,+ ret,+ notification_settings_null=False,+ allow_empty_topic_name=empty_topic_name,+ )return ret# Fill up the UserMessage rows if a soft-deactivated user has returned@@ -1909,7 +1914,9 @@ def do_events_register(include_deactivated_groups=include_deactivated_groups,)- post_process_state(user_profile, ret, notification_settings_null)+ post_process_state(+ user_profile, ret, notification_settings_null, allow_empty_topic_name=empty_topic_name+ )if len(events) > 0:ret["last_event_id"] = events[-1]["id"]@@ -1919,7 +1926,10 @@ def do_events_register(def post_process_state(- user_profile: UserProfile | None, ret: dict[str, Any], notification_settings_null: bool+ user_profile: UserProfile | None,+ ret: dict[str, Any],+ notification_settings_null: bool,+ allow_empty_topic_name: bool,) -> None:"""NOTE:@@ -1933,7 +1943,7 @@ def post_process_state(for client."""if "raw_unread_msgs" in ret:- ret["unread_msgs"] = aggregate_unread_data(ret["raw_unread_msgs"])+ ret["unread_msgs"] = aggregate_unread_data(ret["raw_unread_msgs"], allow_empty_topic_name)del ret["raw_unread_msgs"]"""diff --git a/zerver/lib/message.py b/zerver/lib/message.pyindex 4d94de2b333..b4ac6a8f66c 100644--- a/zerver/lib/message.py+++ b/zerver/lib/message.py@@ -28,7 +28,12 @@ from zerver.lib.stream_subscription import (num_subscribers_for_stream_id,)from zerver.lib.streams import can_access_stream_history, get_web_public_streams_queryset-from zerver.lib.topic import MESSAGE__TOPIC, TOPIC_NAME, messages_for_topic+from zerver.lib.topic import (+ MESSAGE__TOPIC,+ TOPIC_NAME,+ maybe_rename_general_chat_to_empty_topic,+ messages_for_topic,+)from zerver.lib.types import UserDisplayRecipientfrom zerver.lib.user_groups import user_has_permission_for_group_settingfrom zerver.lib.user_topics import build_get_topic_visibility_policy, get_topic_visibility_policy@@ -825,11 +830,15 @@ def extract_unread_data_from_um_rows(return raw_unread_messages-def aggregate_streams(*, input_dict: dict[int, RawUnreadStreamDict]) -> list[UnreadStreamInfo]:+def aggregate_streams(+ *, input_dict: dict[int, RawUnreadStreamDict], allow_empty_topic_name: bool+) -> list[UnreadStreamInfo]:lookup_dict: dict[tuple[int, str], UnreadStreamInfo] = {}for message_id, attribute_dict in input_dict.items():stream_id = attribute_dict["stream_id"]topic_name = attribute_dict["topic"]+ if topic_name == "" and not allow_empty_topic_name:+ topic_name = Message.EMPTY_TOPIC_FALLBACK_NAMElookup_key = (stream_id, topic_name.lower())if lookup_key not in lookup_dict:obj = UnreadStreamInfo(@@ -902,7 +911,9 @@ def aggregate_direct_message_groups(return [lookup_dict[k] for k in sorted_keys]-def aggregate_unread_data(raw_data: RawUnreadMessagesResult) -> UnreadMessagesResult:+def aggregate_unread_data(+ raw_data: RawUnreadMessagesResult, allow_empty_topic_name: bool+) -> UnreadMessagesResult:pm_dict = raw_data["pm_dict"]stream_dict = raw_data["stream_dict"]unmuted_stream_msgs = raw_data["unmuted_stream_msgs"]@@ -912,7 +923,9 @@ def aggregate_unread_data(raw_data: RawUnreadMessagesResult) -> UnreadMessagesRecount = len(pm_dict) + len(unmuted_stream_msgs) + len(direct_message_group_dict)pm_objects = aggregate_pms(input_dict=pm_dict)- stream_objects = aggregate_streams(input_dict=stream_dict)+ stream_objects = aggregate_streams(+ input_dict=stream_dict, allow_empty_topic_name=allow_empty_topic_name+ )direct_message_groups = aggregate_direct_message_groups(input_dict=direct_message_group_dict)result: UnreadMessagesResult = dict(@@ -954,7 +967,9 @@ def apply_unread_message_event()stream_muted = stream_id in state["muted_stream_ids"]- visibility_policy = get_topic_visibility_policy(user_profile, stream_id, topic_name)+ visibility_policy = get_topic_visibility_policy(+ user_profile, stream_id, topic_name=maybe_rename_general_chat_to_empty_topic(topic_name)+ )# A stream message is unmuted if it belongs to:# * a not muted topic in a normal stream# * an unmuted or followed topic in a muted streamdiff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex 621e2a719d4..25c21219b35 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -15140,6 +15140,16 @@ paths:type: stringdescription: |The topic under which the messages were sent.++ Note that the empty string topic may have been rewritten by the server+ to the value of `realm_empty_topic_display_name` found in the+ [`POST /register`](/api/register-queue) response depending on the value+ of the `empty_topic_name` [client capability][client-capabilities].++ **Changes**: The `empty_topic_name` client capability is new in+ Zulip 10.0 (feature level 334).++ [client-capabilities]: /api/register-queue#parameter-client_capabilitiesstream_id:type: integerdescription: |diff --git a/zerver/tests/test_event_system.py b/zerver/tests/test_event_system.pyindex 058752219a7..0219f2d94d3 100644--- a/zerver/tests/test_event_system.py+++ b/zerver/tests/test_event_system.py@@ -845,7 +845,7 @@ class FetchInitialStateDataTest(ZulipTestCase):user_profile=reader,realm=reader.realm,)- post_process_state(reader, result, False)+ post_process_state(reader, result, False, True)self.assert_length(result["unread_msgs"]["streams"], 1)self.assertEqual(result["unread_msgs"]["streams"][0]["topic"], "case DOES not MATTER")self.assert_length(result["unread_msgs"]["streams"][0]["unread_message_ids"], 2)diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex 4f528bd39b0..6021a27be19 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -306,6 +306,7 @@ class BaseAction(ZulipTestCase):client_is_old: bool = False,include_deactivated_groups: bool = False,archived_channels: bool = False,+ allow_empty_topic_name: bool = True,) -> Iterator[list[dict[str, Any]]]:"""Make sure we have a clean slate of client descriptors for these tests.@@ -384,7 +385,9 @@ class BaseAction(ZulipTestCase):validate_against_openapi_schema(content, "/events", "get", "200")self.assert_length(events, num_events)initial_state = copy.deepcopy(hybrid_state)- post_process_state(self.user_profile, initial_state, notification_settings_null)+ post_process_state(+ self.user_profile, initial_state, notification_settings_null, allow_empty_topic_name+ )before = orjson.dumps(initial_state)apply_events(self.user_profile,@@ -399,7 +402,9 @@ class BaseAction(ZulipTestCase):include_deactivated_groups=include_deactivated_groups,archived_channels=archived_channels,)- post_process_state(self.user_profile, hybrid_state, notification_settings_null)+ post_process_state(+ self.user_profile, hybrid_state, notification_settings_null, allow_empty_topic_name+ )after = orjson.dumps(hybrid_state)if state_change_expected:@@ -430,7 +435,9 @@ class BaseAction(ZulipTestCase):include_deactivated_groups=include_deactivated_groups,archived_channels=archived_channels,)- post_process_state(self.user_profile, normal_state, notification_settings_null)+ post_process_state(+ self.user_profile, normal_state, notification_settings_null, allow_empty_topic_name+ )self.match_states(hybrid_state, normal_state, events)def match_states(diff --git a/zerver/tests/test_message_flags.py b/zerver/tests/test_message_flags.pyindex dfb54722eee..bacbe6357b4 100644--- a/zerver/tests/test_message_flags.py+++ b/zerver/tests/test_message_flags.py@@ -1224,7 +1224,7 @@ class GetUnreadMsgsTest(ZulipTestCase):def get_unread_data() -> UnreadMessagesResult:raw_unread_data = get_raw_unread_data(user_profile)- aggregated_data = aggregate_unread_data(raw_unread_data)+ aggregated_data = aggregate_unread_data(raw_unread_data, allow_empty_topic_name=True)return aggregated_datawith mock.patch("zerver.lib.message.MAX_UNREAD_MESSAGES", 5):diff --git a/zerver/tests/test_message_topics.py b/zerver/tests/test_message_topics.pyindex 44fe62beb88..21b0f1d9871 100644--- a/zerver/tests/test_message_topics.py+++ b/zerver/tests/test_message_topics.py@@ -6,6 +6,7 @@ from django.utils.timezone import now as timezone_nowfrom zerver.actions.streams import do_change_stream_permissionfrom zerver.actions.user_topics import do_set_user_topic_visibility_policy+from zerver.lib.events import ClientCapabilities, do_events_registerfrom zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.user_topics import set_topic_visibility_policy, topic_has_visibility_policyfrom zerver.models import Message, UserMessage, UserTopic@@ -785,3 +786,39 @@ class EmptyTopicNameTest(ZulipTestCase):data = self.assert_json_success(result)self.assertEqual(data["message_history"][0]["topic"], "")self.assertEqual(data["message_history"][1]["prev_topic"], "")++ def test_initial_state_data(self) -> None:+ hamlet = self.example_user("hamlet")+ iago = self.example_user("iago")+ self.login_user(hamlet)++ self.send_stream_message(hamlet, "Denmark", topic_name="")+ self.send_stream_message(hamlet, "Verona", topic_name=Message.EMPTY_TOPIC_FALLBACK_NAME)++ with mock.patch("zerver.lib.events.request_event_queue", return_value=1):+ state_data = do_events_register(+ iago,+ iago.realm,+ get_client("website"),+ client_capabilities=ClientCapabilities(+ empty_topic_name=True, notification_settings_null=False+ ),+ )+ self.assertEqual(state_data["unread_msgs"]["streams"][0]["topic"], "")+ self.assertEqual(state_data["unread_msgs"]["streams"][1]["topic"], "")++ with mock.patch("zerver.lib.events.request_event_queue", return_value=1):+ state_data = do_events_register(+ iago,+ iago.realm,+ get_client("website"),+ client_capabilities=ClientCapabilities(+ empty_topic_name=False, notification_settings_null=False+ ),+ )+ self.assertEqual(+ state_data["unread_msgs"]["streams"][0]["topic"], Message.EMPTY_TOPIC_FALLBACK_NAME+ )+ self.assertEqual(+ state_data["unread_msgs"]["streams"][1]["topic"], Message.EMPTY_TOPIC_FALLBACK_NAME+ )diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex e55a49c4c90..d242110025f 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -528,7 +528,7 @@ class TestCreateStreams(ZulipTestCase):def get_unread_stream_data(user: UserProfile) -> list[UnreadStreamInfo]:raw_unread_data = get_raw_unread_data(user)- aggregated_data = aggregate_unread_data(raw_unread_data)+ aggregated_data = aggregate_unread_data(raw_unread_data, allow_empty_topic_name=True)return aggregated_data["streams"]stream_id = Stream.objects.get(name="brand new stream").id@@ -5806,7 +5806,7 @@ class SubscriptionAPITest(ZulipTestCase):def get_unread_stream_data() -> list[UnreadStreamInfo]:raw_unread_data = get_raw_unread_data(user)- aggregated_data = aggregate_unread_data(raw_unread_data)+ aggregated_data = aggregate_unread_data(raw_unread_data, allow_empty_topic_name=True)return aggregated_data["streams"]result = get_unread_stream_data()