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.

backend: Extract check_update_message from update_message_backend.

PIG2087150fe5dc5be
diff --git a/static/js/message_edit.js b/static/js/message_edit.js
index 5469db0dbb7..c7c2b6f3fe4 100644
--- a/static/js/message_edit.js
+++ b/static/js/message_edit.js
@@ -334,7 +334,7 @@ function edit_message(row, raw_content) {
// been able to click it at the time the mouse entered the message_row. Also
// a buffer in case their computer is slow, or stalled for a second, etc
// If you change this number also change edit_limit_buffer in
- // zerver.views.message_edit.update_message_backend
+ // zerver.lib.actions.check_update_message
const seconds_left_buffer = 5;
const editability = get_editability(message, seconds_left_buffer);
const is_editable =
@@ -450,7 +450,7 @@ function edit_message(row, raw_content) {
) {
// Give them at least 10 seconds.
// If you change this number also change edit_limit_buffer in
- // zerver.views.message_edit.update_message_backend
+ // zerver.lib.actions.check_update_message
const min_seconds_to_edit = 10;
let seconds_left =
page_params.realm_message_content_edit_limit_seconds +
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index a1bd4d03ab8..577fb1b639d 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -139,6 +139,7 @@ from zerver.lib.stream_subscription import (
)
from zerver.lib.stream_topic import StreamTopicTarget
from zerver.lib.streams import (
+ access_stream_by_id,
access_stream_for_send_message,
can_access_stream_user_ids,
check_stream_name,
@@ -182,7 +183,7 @@ from zerver.lib.users import (
)
from zerver.lib.utils import generate_api_key, log_statsd_event
from zerver.lib.validator import check_widget_content
-from zerver.lib.widget import do_widget_post_save_actions
+from zerver.lib.widget import do_widget_post_save_actions, is_widget_message
from zerver.models import (
MAX_MESSAGE_LENGTH,
Attachment,
@@ -2676,6 +2677,165 @@ def check_schedule_message(
return do_schedule_messages([send_request])[0]
+def check_update_message(
+ user_profile: UserProfile,
+ message_id: int,
+ stream_id: Optional[int] = None,
+ topic_name: Optional[str] = None,
+ propagate_mode: Optional[str] = "change_one",
+ send_notification_to_old_thread: bool = True,
+ send_notification_to_new_thread: bool = True,
+ content: Optional[str] = None,
+) -> int:
+ """This will update a message given the message id and user profile.
+ It checks whether the user profile has the permission to edit the message
+ and raises a JsonableError if otherwise.
+ It returns the number changed.
+ """
+ if not user_profile.realm.allow_message_editing:
+ raise JsonableError(_("Your organization has turned off message editing"))
+
+ if propagate_mode != "change_one" and topic_name is None and stream_id is None:
+ raise JsonableError(_("Invalid propagate_mode without topic edit"))
+
+ message, ignored_user_message = access_message(user_profile, message_id)
+ is_no_topic_msg = message.topic_name() == "(no topic)"
+
+ # You only have permission to edit a message if:
+ # you change this value also change those two parameters in message_edit.js.
+ # 1. You sent it, OR:
+ # 2. This is a topic-only edit for a (no topic) message, OR:
+ # 3. This is a topic-only edit and you are an admin, OR:
+ # 4. This is a topic-only edit and your realm allows users to edit topics.
+ if message.sender == user_profile:
+ pass
+ elif (content is None) and (
+ is_no_topic_msg
+ or user_profile.is_realm_admin
+ or user_profile.realm.allow_community_topic_editing
+ ):
+ pass
+ else:
+ raise JsonableError(_("You don't have permission to edit this message"))
+
+ # Right now, we prevent users from editing widgets.
+ if content is not None and is_widget_message(message):
+ raise JsonableError(_("Widgets cannot be edited."))
+
+ # If there is a change to the content, check that it hasn't been too long
+ # Allow an extra 20 seconds since we potentially allow editing 15 seconds
+ # past the limit, and in case there are network issues, etc. The 15 comes
+ # from (min_seconds_to_edit + seconds_left_buffer) in message_edit.js; if
+ # you change this value also change those two parameters in message_edit.js.
+ edit_limit_buffer = 20
+ if content is not None and user_profile.realm.message_content_edit_limit_seconds > 0:
+ deadline_seconds = user_profile.realm.message_content_edit_limit_seconds + edit_limit_buffer
+ if (timezone_now() - message.date_sent) > datetime.timedelta(seconds=deadline_seconds):
+ raise JsonableError(_("The time limit for editing this message has passed"))
+
+ # If there is a change to the topic, check that the user is allowed to
+ # edit it and that it has not been too long. If this is not the user who
+ # sent the message, they are not the admin, and the time limit for editing
+ # topics is passed, raise an error.
+ if (
+ content is None
+ and message.sender != user_profile
+ and not user_profile.is_realm_admin
+ and not is_no_topic_msg
+ ):
+ deadline_seconds = Realm.DEFAULT_COMMUNITY_TOPIC_EDITING_LIMIT_SECONDS + edit_limit_buffer
+ if (timezone_now() - message.date_sent) > datetime.timedelta(seconds=deadline_seconds):
+ raise JsonableError(_("The time limit for editing this message has passed"))
+
+ if topic_name is None and content is None and stream_id is None:
+ raise JsonableError(_("Nothing to change"))
+ if topic_name is not None:
+ topic_name = topic_name.strip()
+ if topic_name == "":
+ raise JsonableError(_("Topic can't be empty"))
+ rendered_content = None
+ links_for_embed: Set[str] = set()
+ prior_mention_user_ids: Set[int] = set()
+ mention_user_ids: Set[int] = set()
+ mention_data: Optional[MentionData] = None
+ if content is not None:
+ if content.rstrip() == "":
+ content = "(deleted)"
+ content = normalize_body(content)
+
+ mention_data = MentionData(
+ realm_id=user_profile.realm.id,
+ content=content,
+ )
+ user_info = get_user_info_for_message_updates(message.id)
+ prior_mention_user_ids = user_info["mention_user_ids"]
+
+ # We render the message using the current user's realm; since
+ # the cross-realm bots never edit messages, this should be
+ # always correct.
+ # Note: If rendering fails, the called code will raise a JsonableError.
+ rendered_content = render_incoming_message(
+ message,
+ content,
+ user_info["message_user_ids"],
+ user_profile.realm,
+ mention_data=mention_data,
+ )
+ links_for_embed |= message.links_for_preview
+
+ mention_user_ids = message.mentions_user_ids
+
+ new_stream = None
+ number_changed = 0
+
+ if stream_id is not None:
+ if not message.is_stream_message():
+ raise JsonableError(_("Message must be a stream message"))
+ if not user_profile.is_realm_admin:
+ raise JsonableError(_("You don't have permission to move this message"))
+ try:
+ access_stream_by_id(user_profile, message.recipient.type_id)
+ except JsonableError:
+ raise JsonableError(
+ _(
+ "You don't have permission to move this message due to missing access to its stream"
+ )
+ )
+ if content is not None:
+ raise JsonableError(_("Cannot change message content while changing stream"))
+
+ new_stream = access_stream_by_id(user_profile, stream_id, require_active=True)[0]
+
+ number_changed = do_update_message(
+ user_profile,
+ message,
+ new_stream,
+ topic_name,
+ propagate_mode,
+ send_notification_to_old_thread,
+ send_notification_to_new_thread,
+ content,
+ rendered_content,
+ prior_mention_user_ids,
+ mention_user_ids,
+ mention_data,
+ )
+
+ if links_for_embed:
+ event_data = {
+ "message_id": message.id,
+ "message_content": message.content,
+ # The choice of `user_profile.realm_id` rather than
+ # `sender.realm_id` must match the decision made in the
+ # `render_incoming_message` call earlier in this function.
+ "message_realm_id": user_profile.realm_id,
+ "urls": list(links_for_embed),
+ }
+ queue_json_publish("embed_links", event_data)
+
+ return number_changed
+
+
def check_default_stream_group_name(group_name: str) -> None:
if group_name.strip() == "":
raise JsonableError(_("Invalid default stream group name '{}'").format(group_name))
diff --git a/zerver/tests/test_link_embed.py b/zerver/tests/test_link_embed.py
index d520fae3d84..fcdc246c605 100644
--- a/zerver/tests/test_link_embed.py
+++ b/zerver/tests/test_link_embed.py
@@ -320,7 +320,7 @@ class PreviewTestCase(ZulipTestCase):
url = "http://test.org/"
mocked_response = mock.Mock(side_effect=self.create_mock_response(url))
- with mock_queue_publish("zerver.views.message_edit.queue_json_publish") as patched:
+ with mock_queue_publish("zerver.lib.actions.queue_json_publish") as patched:
result = self.client_patch(
"/json/messages/" + str(msg_id),
{
@@ -452,7 +452,7 @@ class PreviewTestCase(ZulipTestCase):
)
with mock_queue_publish(
- "zerver.views.message_edit.queue_json_publish", wraps=wrapped_queue_json_publish
+ "zerver.lib.actions.queue_json_publish", wraps=wrapped_queue_json_publish
):
result = self.client_patch(
"/json/messages/" + str(msg_id),
diff --git a/zerver/views/message_edit.py b/zerver/views/message_edit.py
index 722eb2b51bd..4242615fd62 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, Set
+from typing import Any, Dict, List, Optional
import orjson
from django.db import IntegrityError
@@ -8,24 +8,15 @@ from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
from zerver.decorator import REQ, has_request_variables
-from zerver.lib.actions import (
- do_delete_messages,
- do_update_message,
- get_user_info_for_message_updates,
- render_incoming_message,
-)
+from zerver.lib.actions import check_update_message, do_delete_messages
from zerver.lib.exceptions import JsonableError
from zerver.lib.html_diff import highlight_html_differences
-from zerver.lib.markdown import MentionData
-from zerver.lib.message import access_message, normalize_body
-from zerver.lib.queue import queue_json_publish
+from zerver.lib.message import access_message
from zerver.lib.response import json_error, json_success
-from zerver.lib.streams import access_stream_by_id
from zerver.lib.timestamp import datetime_to_timestamp
from zerver.lib.topic import LEGACY_PREV_TOPIC, REQ_topic
from zerver.lib.validator import check_bool, check_string_in, to_non_negative_int
-from zerver.lib.widget import is_widget_message
-from zerver.models import Message, Realm, UserProfile
+from zerver.models import Message, UserProfile
def fill_edit_history_entries(message_history: List[Dict[str, Any]], message: Message) -> None:
@@ -114,148 +105,20 @@ def update_message_backend(
send_notification_to_new_thread: bool = REQ(default=True, json_validator=check_bool),
content: Optional[str] = REQ(default=None),
) -> HttpResponse:
- if not user_profile.realm.allow_message_editing:
- raise JsonableError(_("Your organization has turned off message editing"))
-
- if propagate_mode != "change_one" and topic_name is None and stream_id is None:
- raise JsonableError(_("Invalid propagate_mode without topic edit"))
-
- message, ignored_user_message = access_message(user_profile, message_id)
- is_no_topic_msg = message.topic_name() == "(no topic)"
-
- # You only have permission to edit a message if:
- # you change this value also change those two parameters in message_edit.js.
- # 1. You sent it, OR:
- # 2. This is a topic-only edit for a (no topic) message, OR:
- # 3. This is a topic-only edit and you are an admin, OR:
- # 4. This is a topic-only edit and your realm allows users to edit topics.
- if message.sender == user_profile:
- pass
- elif (content is None) and (
- is_no_topic_msg
- or user_profile.is_realm_admin
- or user_profile.realm.allow_community_topic_editing
- ):
- pass
- else:
- raise JsonableError(_("You don't have permission to edit this message"))
-
- # Right now, we prevent users from editing widgets.
- if content is not None and is_widget_message(message):
- return json_error(_("Widgets cannot be edited."))
-
- # If there is a change to the content, check that it hasn't been too long
- # Allow an extra 20 seconds since we potentially allow editing 15 seconds
- # past the limit, and in case there are network issues, etc. The 15 comes
- # from (min_seconds_to_edit + seconds_left_buffer) in message_edit.js; if
- # you change this value also change those two parameters in message_edit.js.
- edit_limit_buffer = 20
- if content is not None and user_profile.realm.message_content_edit_limit_seconds > 0:
- deadline_seconds = user_profile.realm.message_content_edit_limit_seconds + edit_limit_buffer
- if (timezone_now() - message.date_sent) > datetime.timedelta(seconds=deadline_seconds):
- raise JsonableError(_("The time limit for editing this message has passed"))
-
- # If there is a change to the topic, check that the user is allowed to
- # edit it and that it has not been too long. If this is not the user who
- # sent the message, they are not the admin, and the time limit for editing
- # topics is passed, raise an error.
- if (
- content is None
- and message.sender != user_profile
- and not user_profile.is_realm_admin
- and not is_no_topic_msg
- ):
- deadline_seconds = Realm.DEFAULT_COMMUNITY_TOPIC_EDITING_LIMIT_SECONDS + edit_limit_buffer
- if (timezone_now() - message.date_sent) > datetime.timedelta(seconds=deadline_seconds):
- raise JsonableError(_("The time limit for editing this message has passed"))
-
- if topic_name is None and content is None and stream_id is None:
- raise JsonableError(_("Nothing to change"))
- if topic_name is not None:
- topic_name = topic_name.strip()
- if topic_name == "":
- raise JsonableError(_("Topic can't be empty"))
- rendered_content = None
- links_for_embed: Set[str] = set()
- prior_mention_user_ids: Set[int] = set()
- mention_user_ids: Set[int] = set()
- mention_data: Optional[MentionData] = None
- if content is not None:
- if content.rstrip() == "":
- content = "(deleted)"
- content = normalize_body(content)
-
- mention_data = MentionData(
- realm_id=user_profile.realm.id,
- content=content,
- )
- user_info = get_user_info_for_message_updates(message.id)
- prior_mention_user_ids = user_info["mention_user_ids"]
-
- # We render the message using the current user's realm; since
- # the cross-realm bots never edit messages, this should be
- # always correct.
- # Note: If rendering fails, the called code will raise a JsonableError.
- rendered_content = render_incoming_message(
- message,
- content,
- user_info["message_user_ids"],
- user_profile.realm,
- mention_data=mention_data,
- )
- links_for_embed |= message.links_for_preview
-
- mention_user_ids = message.mentions_user_ids
-
- new_stream = None
- number_changed = 0
-
- if stream_id is not None:
- if not message.is_stream_message():
- raise JsonableError(_("Message must be a stream message"))
- if not user_profile.is_realm_admin:
- raise JsonableError(_("You don't have permission to move this message"))
- try:
- access_stream_by_id(user_profile, message.recipient.type_id)
- except JsonableError:
- raise JsonableError(
- _(
- "You don't have permission to move this message due to missing access to its stream"
- )
- )
- if content is not None:
- raise JsonableError(_("Cannot change message content while changing stream"))
-
- new_stream = access_stream_by_id(user_profile, stream_id, require_active=True)[0]
-
- number_changed = do_update_message(
+ number_changed = check_update_message(
user_profile,
- message,
- new_stream,
+ message_id,
+ stream_id,
topic_name,
propagate_mode,
send_notification_to_old_thread,
send_notification_to_new_thread,
content,
- rendered_content,
- prior_mention_user_ids,
- mention_user_ids,
- mention_data,
)
# Include the number of messages changed in the logs
request._log_data["extra"] = f"[{number_changed}]"
- if links_for_embed:
- event_data = {
- "message_id": message.id,
- "message_content": message.content,
- # The choice of `user_profile.realm_id` rather than
- # `sender.realm_id` must match the decision made in the
- # `render_incoming_message` call earlier in this function.
- "message_realm_id": user_profile.realm_id,
- "urls": list(links_for_embed),
- }
- queue_json_publish("embed_links", event_data)
+
return json_success()
diff --git a/zerver/worker/queue_processors.py b/zerver/worker/queue_processors.py
index 5b4dfe98688..d406b34550e 100644
--- a/zerver/worker/queue_processors.py
+++ b/zerver/worker/queue_processors.py
@@ -740,7 +740,7 @@ class FetchLinksEmbedData(QueueProcessingWorker):
message = Message.objects.get(id=event["message_id"])
# If the message changed, we will run this task after updating the message
- # in zerver.views.message_edit.update_message_backend
+ # in zerver.lib.actions.check_update_message
if message.content != event["message_content"]:
return
if message.content is not None: