edit_history: Add supoort for empty topic name.

This commit is contained in:
Prakhar Pratyush 2024-11-15 15:41:54 +05:30
parent 95742c8f03
commit a1c3920df3
3 changed files with 38 additions and 4 deletions

View File

@ -327,3 +327,11 @@ def maybe_rename_general_chat_to_empty_topic(topic_name: str) -> str:
if topic_name == "general chat": if topic_name == "general chat":
topic_name = "" topic_name = ""
return 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 "general chat"
return topic_name

View File

@ -1630,6 +1630,19 @@ def process_stream_typing_notification_event(event: Mapping[str, Any], users: It
client.add_event(event) client.add_event(event)
def process_mark_message_unread_event(event: Mapping[str, Any], users: Iterable[int]) -> None:
for user_profile_id in users:
for client in get_client_descriptors_for_user(user_profile_id):
if not client.accepts_event(event):
continue
for message_id, message_detail in event["message_details"].items():
if message_detail["type"] == "stream" and message_detail.get("topic") == "" and not client.empty_topic_name:
event["message_details"][message_id]["topic"] = "general chat"
client.add_event(event)
def process_notification(notice: Mapping[str, Any]) -> None: def process_notification(notice: Mapping[str, Any]) -> None:
event: Mapping[str, Any] = notice["event"] event: Mapping[str, Any] = notice["event"]
users: list[int] | list[Mapping[str, Any]] = notice["users"] users: list[int] | list[Mapping[str, Any]] = notice["users"]
@ -1662,6 +1675,8 @@ def process_notification(notice: Mapping[str, Any]) -> None:
process_user_topic_event(event, cast(list[int], users)) process_user_topic_event(event, cast(list[int], users))
elif event["type"] == "typing" and event["message_type"] == "stream": elif event["type"] == "typing" and event["message_type"] == "stream":
process_stream_typing_notification_event(event, cast(list[int], users)) process_stream_typing_notification_event(event, cast(list[int], users))
elif event["type"] == "update_message_flags" and event["op"] == "remove" and event["flag"] == "read":
process_mark_message_unread_event(event, cast(list[int], users))
elif event["type"] == "cleanup_queue": elif event["type"] == "cleanup_queue":
# cleanup_event_queue may generate this event to forward cleanup # cleanup_event_queue may generate this event to forward cleanup
# requests to the right shard. # requests to the right shard.

View File

@ -23,13 +23,16 @@ from zerver.lib.message import (
from zerver.lib.request import RequestNotes from zerver.lib.request import RequestNotes
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.timestamp import datetime_to_timestamp 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.typed_endpoint import OptionalTopic, PathOnly, typed_endpoint
from zerver.lib.types import EditHistoryEvent, FormattedEditHistoryEvent from zerver.lib.types import EditHistoryEvent, FormattedEditHistoryEvent
from zerver.models import Message, UserProfile from zerver.models import Message, UserProfile
def fill_edit_history_entries( 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]: ) -> list[FormattedEditHistoryEvent]:
""" """
This fills out the message edit history entries from the database 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_content = message.content
prev_rendered_content = message.rendered_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 # Make sure that the latest entry in the history corresponds to the
# message's last edit time # message's last edit time
@ -58,7 +64,9 @@ def fill_edit_history_entries(
} }
if "prev_topic" in edit_history_event: 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 formatted_entry["prev_topic"] = prev_topic_name
# Fill current values for content/rendered_content. # Fill current values for content/rendered_content.
@ -99,6 +107,7 @@ def get_message_edit_history(
user_profile: UserProfile, user_profile: UserProfile,
*, *,
message_id: PathOnly[NonNegativeInt], message_id: PathOnly[NonNegativeInt],
allow_empty_topic_name: Json[bool] = False,
) -> HttpResponse: ) -> HttpResponse:
if not user_profile.realm.allow_edit_history: if not user_profile.realm.allow_edit_history:
raise JsonableError(_("Message edit history is disabled in this organization")) raise JsonableError(_("Message edit history is disabled in this organization"))
@ -111,7 +120,9 @@ def get_message_edit_history(
raw_edit_history = [] raw_edit_history = []
# Fill in all the extra data that will make it usable # 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))}) return json_success(request, data={"message_history": list(reversed(message_edit_history))})