2023-11-19 19:45:19 +01:00
|
|
|
from datetime import timedelta
|
2023-08-17 01:46:00 +02:00
|
|
|
from typing import List, Literal, Optional, Union
|
2020-06-22 22:57:01 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2021-09-20 10:34:10 +02:00
|
|
|
from django.contrib.auth.models import AnonymousUser
|
2021-06-03 15:46:13 +02:00
|
|
|
from django.db import IntegrityError, transaction
|
2020-06-22 22:57:01 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.timezone import now as timezone_now
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2023-08-17 01:46:00 +02:00
|
|
|
from pydantic import Json, NonNegativeInt
|
2020-06-22 22:57:01 +02:00
|
|
|
|
2022-07-17 13:00:21 +02:00
|
|
|
from zerver.actions.message_delete import do_delete_messages
|
|
|
|
from zerver.actions.message_edit import check_update_message
|
2021-09-20 10:34:10 +02:00
|
|
|
from zerver.context_processors import get_valid_realm_from_request
|
2020-06-22 22:57:01 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
|
|
from zerver.lib.html_diff import highlight_html_differences
|
2024-04-11 22:48:10 +02:00
|
|
|
from zerver.lib.message import (
|
|
|
|
access_message,
|
|
|
|
access_message_and_usermessage,
|
|
|
|
access_web_public_message,
|
|
|
|
messages_for_ids,
|
|
|
|
)
|
2023-08-17 01:46:00 +02:00
|
|
|
from zerver.lib.request import RequestNotes
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
2020-06-22 22:57:01 +02:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
2023-08-17 01:46:00 +02:00
|
|
|
from zerver.lib.typed_endpoint import OptionalTopic, PathOnly, typed_endpoint
|
2022-03-02 01:36:17 +01:00
|
|
|
from zerver.lib.types import EditHistoryEvent, FormattedEditHistoryEvent
|
2021-06-21 18:52:51 +02:00
|
|
|
from zerver.models import Message, UserProfile
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
|
2022-03-02 01:36:17 +01:00
|
|
|
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 future
|
2020-06-22 22:57:01 +02:00
|
|
|
processing.
|
|
|
|
"""
|
|
|
|
prev_content = message.content
|
|
|
|
prev_rendered_content = message.rendered_content
|
2024-01-13 14:02:59 +01:00
|
|
|
prev_topic_name = message.topic_name()
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
# Make sure that the latest entry in the history corresponds to the
|
|
|
|
# message's last edit time
|
2022-03-02 01:36:17 +01:00
|
|
|
if len(raw_edit_history) > 0:
|
2020-06-22 22:57:01 +02:00
|
|
|
assert message.last_edit_time is not None
|
2022-03-02 01:36:17 +01:00
|
|
|
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"],
|
2024-01-13 14:02:59 +01:00
|
|
|
"topic": prev_topic_name,
|
2022-03-02 01:36:17 +01:00
|
|
|
"user_id": edit_history_event["user_id"],
|
|
|
|
}
|
|
|
|
|
2022-03-02 02:09:41 +01:00
|
|
|
if "prev_topic" in edit_history_event:
|
2024-01-13 14:02:59 +01:00
|
|
|
prev_topic_name = edit_history_event["prev_topic"]
|
|
|
|
formatted_entry["prev_topic"] = prev_topic_name
|
2022-03-02 01:36:17 +01:00
|
|
|
|
|
|
|
# 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"]
|
2020-06-22 22:57:01 +02:00
|
|
|
assert prev_rendered_content is not None
|
2022-03-02 01:36:17 +01:00
|
|
|
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
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
|
2022-03-02 01:36:17 +01:00
|
|
|
if "prev_stream" in edit_history_event:
|
|
|
|
formatted_entry["prev_stream"] = edit_history_event["prev_stream"]
|
2022-02-14 17:04:39 +01:00
|
|
|
formatted_entry["stream"] = edit_history_event["stream"]
|
2022-03-02 01:36:17 +01:00
|
|
|
|
|
|
|
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),
|
2024-01-13 14:02:59 +01:00
|
|
|
"topic": prev_topic_name,
|
2022-03-02 01:36:17 +01:00
|
|
|
"user_id": message.sender_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
formatted_edit_history.append(initial_message_history)
|
|
|
|
|
|
|
|
return formatted_edit_history
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-06-22 22:57:01 +02:00
|
|
|
|
2023-08-17 01:46:00 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def get_message_edit_history(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-17 01:46:00 +02:00
|
|
|
*,
|
|
|
|
message_id: PathOnly[NonNegativeInt],
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2020-06-22 22:57:01 +02:00
|
|
|
if not user_profile.realm.allow_edit_history:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Message edit history is disabled in this organization"))
|
2024-03-22 06:45:17 +01:00
|
|
|
message = access_message(user_profile, message_id)
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
# Extract the message edit history from the message
|
|
|
|
if message.edit_history is not None:
|
2022-03-02 01:36:17 +01:00
|
|
|
raw_edit_history = orjson.loads(message.edit_history)
|
2020-06-22 22:57:01 +02:00
|
|
|
else:
|
2022-03-02 01:36:17 +01:00
|
|
|
raw_edit_history = []
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
# Fill in all the extra data that will make it usable
|
2022-03-02 01:36:17 +01:00
|
|
|
message_edit_history = fill_edit_history_entries(raw_edit_history, message)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"message_history": list(reversed(message_edit_history))})
|
2020-06-22 22:57:01 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-08-17 01:46:00 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def update_message_backend(
|
|
|
|
request: HttpRequest,
|
2021-04-01 15:50:03 +02:00
|
|
|
user_profile: UserProfile,
|
2023-08-17 01:46:00 +02:00
|
|
|
*,
|
|
|
|
message_id: PathOnly[NonNegativeInt],
|
|
|
|
stream_id: Optional[Json[NonNegativeInt]] = None,
|
|
|
|
topic_name: OptionalTopic = None,
|
|
|
|
propagate_mode: Literal["change_later", "change_one", "change_all"] = "change_one",
|
|
|
|
send_notification_to_old_thread: Json[bool] = False,
|
|
|
|
send_notification_to_new_thread: Json[bool] = True,
|
|
|
|
content: Optional[str] = None,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2021-05-09 17:24:40 +02:00
|
|
|
number_changed = check_update_message(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile,
|
2021-05-09 17:24:40 +02:00
|
|
|
message_id,
|
|
|
|
stream_id,
|
2021-02-12 08:19:30 +01:00
|
|
|
topic_name,
|
|
|
|
propagate_mode,
|
|
|
|
send_notification_to_old_thread,
|
|
|
|
send_notification_to_new_thread,
|
|
|
|
content,
|
|
|
|
)
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
# Include the number of messages changed in the logs
|
2021-08-21 19:24:20 +02:00
|
|
|
log_data = RequestNotes.get_notes(request).log_data
|
2021-07-09 10:06:04 +02:00
|
|
|
assert log_data is not None
|
|
|
|
log_data["extra"] = f"[{number_changed}]"
|
2021-05-09 17:24:40 +02:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def validate_can_delete_message(user_profile: UserProfile, message: Message) -> None:
|
|
|
|
if user_profile.is_realm_admin:
|
|
|
|
# Admin can delete any message, any time.
|
|
|
|
return
|
2023-06-14 19:40:41 +02:00
|
|
|
if message.sender != user_profile and message.sender.bot_owner_id != user_profile.id:
|
|
|
|
# Users can only delete messages sent by them or by their bots.
|
2020-06-22 22:57:01 +02:00
|
|
|
raise JsonableError(_("You don't have permission to delete this message"))
|
2021-06-21 18:52:51 +02:00
|
|
|
if not user_profile.can_delete_own_message():
|
|
|
|
# Only user with roles as allowed by delete_own_message_policy can delete message.
|
2020-06-22 22:57:01 +02:00
|
|
|
raise JsonableError(_("You don't have permission to delete this message"))
|
|
|
|
|
2021-06-14 18:49:28 +02:00
|
|
|
deadline_seconds: Optional[int] = user_profile.realm.message_content_delete_limit_seconds
|
|
|
|
if deadline_seconds is None:
|
|
|
|
# None means no time limit to delete message
|
2020-06-22 22:57:01 +02:00
|
|
|
return
|
2023-11-19 19:45:19 +01:00
|
|
|
if (timezone_now() - message.date_sent) > timedelta(seconds=deadline_seconds):
|
2023-10-09 21:28:43 +02:00
|
|
|
# User cannot delete message after deadline time of realm
|
2020-06-22 22:57:01 +02:00
|
|
|
raise JsonableError(_("The time limit for deleting this message has passed"))
|
|
|
|
return
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-06-03 15:46:13 +02:00
|
|
|
@transaction.atomic
|
2023-08-17 01:46:00 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def delete_message_backend(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-17 01:46:00 +02:00
|
|
|
*,
|
|
|
|
message_id: PathOnly[NonNegativeInt],
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2021-06-03 15:46:13 +02:00
|
|
|
# We lock the `Message` object to ensure that any transactions modifying the `Message` object
|
|
|
|
# concurrently are serialized properly with deleting the message; this prevents a deadlock
|
|
|
|
# that would otherwise happen because of the other transaction holding a lock on the `Message`
|
|
|
|
# row.
|
2024-03-22 06:45:17 +01:00
|
|
|
message = access_message(user_profile, message_id, lock_message=True)
|
2020-06-22 22:57:01 +02:00
|
|
|
validate_can_delete_message(user_profile, message)
|
|
|
|
try:
|
|
|
|
do_delete_messages(user_profile.realm, [message])
|
|
|
|
except (Message.DoesNotExist, IntegrityError):
|
|
|
|
raise JsonableError(_("Message already deleted"))
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2020-06-22 22:57:01 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-08-17 01:46:00 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def json_fetch_raw_message(
|
|
|
|
request: HttpRequest,
|
2021-09-20 10:34:10 +02:00
|
|
|
maybe_user_profile: Union[UserProfile, AnonymousUser],
|
2023-08-17 01:46:00 +02:00
|
|
|
*,
|
|
|
|
message_id: PathOnly[NonNegativeInt],
|
|
|
|
apply_markdown: Json[bool] = True,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2021-09-20 10:34:10 +02:00
|
|
|
if not maybe_user_profile.is_authenticated:
|
|
|
|
realm = get_valid_realm_from_request(request)
|
|
|
|
message = access_web_public_message(realm, message_id)
|
2023-11-08 04:53:05 +01:00
|
|
|
user_profile = None
|
2021-09-20 10:34:10 +02:00
|
|
|
else:
|
2024-04-11 22:48:10 +02:00
|
|
|
(message, user_message) = access_message_and_usermessage(maybe_user_profile, message_id)
|
2023-11-08 04:53:05 +01:00
|
|
|
user_profile = maybe_user_profile
|
2021-09-20 10:34:10 +02:00
|
|
|
|
2022-03-02 05:33:20 +01:00
|
|
|
flags = ["read"]
|
|
|
|
if not maybe_user_profile.is_authenticated:
|
|
|
|
allow_edit_history = realm.allow_edit_history
|
|
|
|
else:
|
|
|
|
if user_message:
|
|
|
|
flags = user_message.flags_list()
|
2022-04-02 02:52:29 +02:00
|
|
|
else:
|
|
|
|
flags = ["read", "historical"]
|
2022-03-02 05:33:20 +01:00
|
|
|
allow_edit_history = maybe_user_profile.realm.allow_edit_history
|
|
|
|
|
|
|
|
# Security note: It's important that we call this only with a
|
|
|
|
# message already fetched via `access_message` type methods,
|
|
|
|
# as we do above.
|
|
|
|
message_dict_list = messages_for_ids(
|
|
|
|
message_ids=[message.id],
|
|
|
|
user_message_flags={message_id: flags},
|
|
|
|
search_fields={},
|
|
|
|
apply_markdown=apply_markdown,
|
|
|
|
client_gravatar=True,
|
|
|
|
allow_edit_history=allow_edit_history,
|
2023-11-08 04:53:05 +01:00
|
|
|
user_profile=user_profile,
|
|
|
|
realm=message.realm,
|
2022-03-02 05:33:20 +01:00
|
|
|
)
|
|
|
|
response = dict(
|
|
|
|
message=message_dict_list[0],
|
|
|
|
# raw_content is deprecated; we will need to wait until
|
|
|
|
# clients have been fully migrated to using the modern API
|
|
|
|
# before removing this, probably in 2023.
|
|
|
|
raw_content=message.content,
|
|
|
|
)
|
|
|
|
return json_success(request, response)
|