Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

edit_history: Add support for empty topic name.

Prakhar Pratyush293db85f6756
diff --git a/api_docs/changelog.md b/api_docs/changelog.md
index 2a9c682fcb2..893210c589a 100644
--- a/api_docs/changelog.md
+++ b/api_docs/changelog.md
@@ -49,6 +49,10 @@ format used by the Zulip server that they are interacting with.
boolean parameter to decide whether the topic names in the fetched messages
can be empty strings.
+* [`GET /messages/{message_id}/history`](/api/get-message-history):
+ Added `allow_empty_topic_name` boolean parameter to decide whether the
+ topic names in the fetched message history objects can be empty strings.
+
**Feature level 333**
* [Message formatting](/api/message-formatting): System groups can now
diff --git a/zerver/lib/topic.py b/zerver/lib/topic.py
index 6e48c1af98d..3e0a9ec971d 100644
--- a/zerver/lib/topic.py
+++ b/zerver/lib/topic.py
@@ -327,3 +327,11 @@ def maybe_rename_general_chat_to_empty_topic(topic_name: str) -> str:
if topic_name == Message.EMPTY_TOPIC_FALLBACK_NAME:
topic_name = ""
return topic_name
+
+
+def maybe_rename_empty_topic_to_general_chat(
+ topic_name: str, is_channel_message: bool, allow_empty_topic_name: bool
+) -> str:
+ if is_channel_message and topic_name == "" and not allow_empty_topic_name:
+ return Message.EMPTY_TOPIC_FALLBACK_NAME
+ return topic_name
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index 0a86a378fea..c55602510aa 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -7310,6 +7310,21 @@ paths:
[edit-settings]: /help/view-a-messages-edit-history
parameters:
- $ref: "#/components/parameters/MessageId"
+ - name: allow_empty_topic_name
+ in: query
+ description: |
+ Whether the topic names i.e. `topic` and `prev_topic` fields in
+ the `message_history` objects returned can be empty string.
+
+ If `false`, the value of `realm_empty_topic_display_name`
+ found in the [`POST /register`](/api/register-queue) response is
+ returned replacing the empty string as the topic name.
+
+ **Changes**: New in Zulip 10.0 (feature level 334).
+ schema:
+ type: boolean
+ default: false
+ example: true
x-response-description: |
Please note that the original message's snapshot only contains the fields
`topic`, `content`, `rendered_content`, `timestamp` and `user_id`. This
diff --git a/zerver/tests/test_message_topics.py b/zerver/tests/test_message_topics.py
index a0da63316dd..577c49d6689 100644
--- a/zerver/tests/test_message_topics.py
+++ b/zerver/tests/test_message_topics.py
@@ -686,3 +686,44 @@ class EmptyTopicNameTest(ZulipTestCase):
)
data = self.assert_json_success(result)
self.assertEqual(data["message"]["edit_history"][0]["topic"], "")
+
+ def test_get_message_edit_history(self) -> None:
+ hamlet = self.example_user("hamlet")
+ self.login_user(hamlet)
+
+ message_id = self.send_stream_message(hamlet, "Denmark", topic_name="")
+ message_id_2 = self.send_stream_message(
+ hamlet, "Denmark", topic_name=Message.EMPTY_TOPIC_FALLBACK_NAME
+ )
+
+ params = {"topic": "new topic name"}
+ result = self.client_patch(f"/json/messages/{message_id}", params)
+ self.assert_json_success(result)
+ result = self.client_patch(f"/json/messages/{message_id_2}", params)
+ self.assert_json_success(result)
+
+ params = {"allow_empty_topic_name": "false"}
+ result = self.client_get(f"/json/messages/{message_id}/history", params)
+ data = self.assert_json_success(result)
+ self.assertEqual(data["message_history"][0]["topic"], Message.EMPTY_TOPIC_FALLBACK_NAME)
+ self.assertEqual(
+ data["message_history"][1]["prev_topic"], Message.EMPTY_TOPIC_FALLBACK_NAME
+ )
+
+ result = self.client_get(f"/json/messages/{message_id_2}/history", params)
+ data = self.assert_json_success(result)
+ self.assertEqual(data["message_history"][0]["topic"], Message.EMPTY_TOPIC_FALLBACK_NAME)
+ self.assertEqual(
+ data["message_history"][1]["prev_topic"], Message.EMPTY_TOPIC_FALLBACK_NAME
+ )
+
+ params = {"allow_empty_topic_name": "true"}
+ result = self.client_get(f"/json/messages/{message_id}/history", params)
+ data = self.assert_json_success(result)
+ self.assertEqual(data["message_history"][0]["topic"], "")
+ self.assertEqual(data["message_history"][1]["prev_topic"], "")
+
+ result = self.client_get(f"/json/messages/{message_id_2}/history", params)
+ data = self.assert_json_success(result)
+ self.assertEqual(data["message_history"][0]["topic"], "")
+ self.assertEqual(data["message_history"][1]["prev_topic"], "")
diff --git a/zerver/views/message_edit.py b/zerver/views/message_edit.py
index 11fb482fa66..46470d97ab7 100644
--- a/zerver/views/message_edit.py
+++ b/zerver/views/message_edit.py
@@ -23,13 +23,16 @@ from zerver.lib.message import (
from zerver.lib.request import RequestNotes
from zerver.lib.response import json_success
from zerver.lib.timestamp import datetime_to_timestamp
+from zerver.lib.topic import maybe_rename_empty_topic_to_general_chat
from zerver.lib.typed_endpoint import OptionalTopic, PathOnly, typed_endpoint
from zerver.lib.types import EditHistoryEvent, FormattedEditHistoryEvent
from zerver.models import Message, UserProfile
def fill_edit_history_entries(
- raw_edit_history: list[EditHistoryEvent], message: Message
+ raw_edit_history: list[EditHistoryEvent],
+ message: Message,
+ allow_empty_topic_name: bool,
) -> list[FormattedEditHistoryEvent]:
"""
This fills out the message edit history entries from the database
@@ -39,7 +42,10 @@ def fill_edit_history_entries(
"""
prev_content = message.content
prev_rendered_content = message.rendered_content
- prev_topic_name = message.topic_name()
+ is_channel_message = message.is_stream_message()
+ prev_topic_name = maybe_rename_empty_topic_to_general_chat(
+ message.topic_name(), is_channel_message, allow_empty_topic_name
+ )
# Make sure that the latest entry in the history corresponds to the
# message's last edit time
@@ -58,7 +64,9 @@ def fill_edit_history_entries(
}
if "prev_topic" in edit_history_event:
- prev_topic_name = edit_history_event["prev_topic"]
+ prev_topic_name = maybe_rename_empty_topic_to_general_chat(
+ edit_history_event["prev_topic"], is_channel_message, allow_empty_topic_name
+ )
formatted_entry["prev_topic"] = prev_topic_name
# Fill current values for content/rendered_content.
@@ -99,6 +107,7 @@ def get_message_edit_history(
user_profile: UserProfile,
*,
message_id: PathOnly[NonNegativeInt],
+ allow_empty_topic_name: Json[bool] = False,
) -> HttpResponse:
if not user_profile.realm.allow_edit_history:
raise JsonableError(_("Message edit history is disabled in this organization"))
@@ -111,7 +120,9 @@ def get_message_edit_history(
raw_edit_history = []
# Fill in all the extra data that will make it usable
- message_edit_history = fill_edit_history_entries(raw_edit_history, message)
+ message_edit_history = fill_edit_history_entries(
+ raw_edit_history, message, allow_empty_topic_name
+ )
return json_success(request, data={"message_history": list(reversed(message_edit_history))})