Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
edit_history: Refactor to use FormattedEditHistoryEvent type.
Tim Abbott85222b790d62
diff --git a/tools/linter_lib/custom_check.py b/tools/linter_lib/custom_check.pyindex 7a6f82491f1..9eef8b72155 100644--- a/tools/linter_lib/custom_check.py+++ b/tools/linter_lib/custom_check.py@@ -14,6 +14,7 @@ FILES_WITH_LEGACY_SUBJECT = {# This basically requires a big DB migration:"zerver/lib/topic.py","zerver/lib/types.py",+ "zerver/views/message_edit.py",# This is for backward compatibility."zerver/tests/test_legacy_subject.py",# Other migration-related changes require extreme care.diff --git a/zerver/lib/types.py b/zerver/lib/types.pyindex 16eae69fb71..60ba026fca6 100644--- a/zerver/lib/types.py+++ b/zerver/lib/types.py@@ -126,3 +126,21 @@ class EditHistoryEvent(TypedDict, total=False):prev_content: strprev_rendered_content: Optional[str]prev_rendered_content_version: Optional[int]+++class FormattedEditHistoryEvent(TypedDict, total=False):+ """+ Extended format used in the edit history endpoint.+ """++ user_id: Optional[int]+ timestamp: int+ prev_stream: int+ stream: int+ prev_topic: str+ topic: str+ prev_content: str+ content: str+ prev_rendered_content: Optional[str]+ rendered_content: Optional[str]+ content_html_diff: strdiff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex 85445fc3571..6795c8cb218 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -4995,6 +4995,12 @@ paths:type: stringdescription: |the topic for the message before being edited.+ prev_stream:+ type: integer+ description: |+ Only present if message's stream was edited.++ The stream ID of the message prior to being edited.content:type: stringdescription: |diff --git a/zerver/views/message_edit.py b/zerver/views/message_edit.pyindex 0ad0f03621c..9944d9fa0a3 100644--- a/zerver/views/message_edit.py+++ b/zerver/views/message_edit.py@@ -1,5 +1,5 @@import datetime-from typing import Any, Dict, List, Optional, Union+from typing import List, Optional, Unionimport orjsonfrom django.contrib.auth.models import AnonymousUser@@ -17,18 +17,19 @@ from zerver.lib.request import REQ, RequestNotes, has_request_variablesfrom zerver.lib.response import json_successfrom zerver.lib.timestamp import datetime_to_timestampfrom zerver.lib.topic import LEGACY_PREV_TOPIC, REQ_topic+from zerver.lib.types import EditHistoryEvent, FormattedEditHistoryEventfrom zerver.lib.validator import check_bool, check_string_in, to_non_negative_intfrom zerver.models import Message, UserProfile-def fill_edit_history_entries(message_history: List[Dict[str, Any]], message: Message) -> None:- """This fills out the message edit history entries from the database,- which are designed to have the minimum data possible, to instead- have the current topic + content as of that time, plus data on- whatever changed. This makes it much simpler to do future+def fill_edit_history_entries(+ raw_edit_history: List[EditHistoryEvent], message: Message+) -> List[FormattedEditHistoryEvent]:+ """+ This fills out the message edit history entries from the database+ to have the current topic + content as of that time, plus data on+ whatever changed. This makes it much simpler to do futureprocessing.-- Note that this mutates what is passed to it, which is sorta a bad pattern."""prev_content = message.contentprev_rendered_content = message.rendered_content@@ -36,37 +37,55 @@ def fill_edit_history_entries(message_history: List[Dict[str, Any]], message: Me# Make sure that the latest entry in the history corresponds to the# message's last edit time- if len(message_history) > 0:+ if len(raw_edit_history) > 0:assert message.last_edit_time is not None- assert datetime_to_timestamp(message.last_edit_time) == message_history[0]["timestamp"]-- for entry in message_history:- entry["topic"] = prev_topic- if LEGACY_PREV_TOPIC in entry:- prev_topic = entry[LEGACY_PREV_TOPIC]- entry["prev_topic"] = prev_topic- del entry[LEGACY_PREV_TOPIC]-- entry["content"] = prev_content- entry["rendered_content"] = prev_rendered_content- if "prev_content" in entry:- del entry["prev_rendered_content_version"]- prev_content = entry["prev_content"]- prev_rendered_content = entry["prev_rendered_content"]+ assert datetime_to_timestamp(message.last_edit_time) == raw_edit_history[0]["timestamp"]++ formatted_edit_history: List[FormattedEditHistoryEvent] = []+ for edit_history_event in raw_edit_history:+ formatted_entry: FormattedEditHistoryEvent = {+ "content": prev_content,+ "rendered_content": prev_rendered_content,+ "timestamp": edit_history_event["timestamp"],+ "topic": prev_topic,+ "user_id": edit_history_event["user_id"],+ }++ # Add current topic, map LEGACY_PREV_TOPIC => "prev_topic".+ if LEGACY_PREV_TOPIC in edit_history_event:+ prev_topic = edit_history_event["prev_subject"]+ formatted_entry["prev_topic"] = prev_topic++ # Fill current values for content/rendered_content.+ if "prev_content" in edit_history_event:+ formatted_entry["prev_content"] = edit_history_event["prev_content"]+ prev_content = formatted_entry["prev_content"]+ formatted_entry["prev_rendered_content"] = edit_history_event["prev_rendered_content"]+ prev_rendered_content = formatted_entry["prev_rendered_content"]assert prev_rendered_content is not None- entry["content_html_diff"] = highlight_html_differences(- prev_rendered_content, entry["rendered_content"], message.id+ rendered_content = formatted_entry["rendered_content"]+ assert rendered_content is not None+ formatted_entry["content_html_diff"] = highlight_html_differences(+ prev_rendered_content, rendered_content, message.id)- message_history.append(- dict(- topic=prev_topic,- content=prev_content,- rendered_content=prev_rendered_content,- timestamp=datetime_to_timestamp(message.date_sent),- user_id=message.sender_id,- )- )+ if "prev_stream" in edit_history_event:+ formatted_entry["prev_stream"] = edit_history_event["prev_stream"]+ # TODO: Include current stream here.++ formatted_edit_history.append(formatted_entry)++ initial_message_history: FormattedEditHistoryEvent = {+ "content": prev_content,+ "rendered_content": prev_rendered_content,+ "timestamp": datetime_to_timestamp(message.date_sent),+ "topic": prev_topic,+ "user_id": message.sender_id,+ }++ formatted_edit_history.append(initial_message_history)++ return formatted_edit_history@has_request_variables@@ -81,12 +100,12 @@ def get_message_edit_history(# Extract the message edit history from the messageif message.edit_history is not None:- message_edit_history = orjson.loads(message.edit_history)+ raw_edit_history = orjson.loads(message.edit_history)else:- message_edit_history = []+ raw_edit_history = []# Fill in all the extra data that will make it usable- fill_edit_history_entries(message_edit_history, message)+ message_edit_history = fill_edit_history_entries(raw_edit_history, message)return json_success(request, data={"message_history": list(reversed(message_edit_history))})