Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_report: Add confirmation message.
Danish83e9a83e8503
diff --git a/starlight_help/src/content/docs/report-a-message.mdx b/starlight_help/src/content/docs/report-a-message.mdxindex f5a2479fc44..1b4f0f347c3 100644--- a/starlight_help/src/content/docs/report-a-message.mdx+++ b/starlight_help/src/content/docs/report-a-message.mdx@@ -27,7 +27,9 @@ that you report, not other DMs in the same thread.organization.1. Fill out the requested information. Moderators will see the messageyou're reporting when they review your report.- 1. Click **Submit** to report the message.+ 1. Click **Submit** to report the message. You will receive a direct+ message from Notification Bot confirming that your report was+ submitted.</FlattenedSteps></TabItem>diff --git a/zerver/lib/message_report.py b/zerver/lib/message_report.pyindex ec42ea01ac1..cce276c5e28 100644--- a/zerver/lib/message_report.py+++ b/zerver/lib/message_report.py@@ -1,7 +1,8 @@from django.conf import settingsfrom django.utils.translation import gettext as _+from django.utils.translation import override as override_language-from zerver.actions.message_send import internal_send_stream_message+from zerver.actions.message_send import internal_send_private_message, internal_send_stream_messagefrom zerver.lib.display_recipient import get_display_recipientfrom zerver.lib.markdown.fenced_code import get_unused_fencefrom zerver.lib.mention import silent_mention_syntax_for_user@@ -13,7 +14,7 @@ from zerver.lib.message import ()from zerver.lib.timestamp import datetime_to_global_timefrom zerver.lib.topic_link_util import get_message_link_syntax-from zerver.lib.url_encoding import pm_message_url+from zerver.lib.url_encoding import pm_message_url, stream_message_urlfrom zerver.models import Message, Realm, UserProfilefrom zerver.models.recipients import Recipientfrom zerver.models.streams import StreamTopicsPolicyEnum@@ -157,3 +158,41 @@ def send_message_report(content=content,acting_user=reporting_user,)+++def send_report_confirmation_dm(+ reporting_user: UserProfile,+ realm: Realm,+ reported_message: Message,+ report_type: str,+) -> None:+ if reported_message.is_channel_message:+ message_url = stream_message_url(+ realm=realm,+ message=dict(+ id=reported_message.id,+ stream_id=reported_message.recipient.type_id,+ display_recipient=reported_message.recipient.label(),+ topic=reported_message.topic_name(),+ ),+ )+ else:+ message_url = pm_message_url(+ realm,+ dict(+ id=reported_message.id,+ display_recipient=get_display_recipient(reported_message.recipient),+ ),+ )++ with override_language(reporting_user.default_language):+ reason = str(Realm.REPORT_MESSAGE_REASONS[report_type]).lower()+ content = _("You reported [a message]({message_url}) for {reason}.").format(+ message_url=message_url, reason=reason+ )++ internal_send_private_message(+ get_system_bot(settings.NOTIFICATION_BOT, realm.id),+ reporting_user,+ content,+ )diff --git a/zerver/tests/test_message_report.py b/zerver/tests/test_message_report.pyindex 01f187b9b0d..09e79077671 100644--- a/zerver/tests/test_message_report.py+++ b/zerver/tests/test_message_report.py@@ -15,6 +15,7 @@ from zerver.lib.mention import silent_mention_syntax_for_userfrom zerver.lib.message import get_user_mentions_for_display, truncate_contentfrom zerver.lib.message_report import MAX_REPORT_MESSAGE_SNIPPET_LENGTHfrom zerver.lib.test_classes import ZulipTestCase+from zerver.lib.test_helpers import most_recent_messagefrom zerver.lib.timestamp import datetime_to_global_timefrom zerver.lib.topic_link_util import (get_message_link_syntax,@@ -596,6 +597,83 @@ class ReportMessageTest(ZulipTestCase):self.assertEqual(report_msg.topic_name(), "")self.assertIn("reported", report_msg.content)+ def test_report_sends_confirmation_dm(self) -> None:+ reporting_user = self.example_user("hamlet")+ report_type = "spam"+ notification_bot = get_system_bot(settings.NOTIFICATION_BOT, self.realm.id)++ result = self.report_message(+ reporting_user,+ self.reported_message_id,+ report_type=report_type,+ )+ self.assert_json_success(result)++ # The confirmation DM is sent after the moderation channel post, so it+ # is the most recent message seen by the reporting user.+ confirmation_dm = most_recent_message(reporting_user)+ self.assertEqual(confirmation_dm.sender_id, notification_bot.id)++ realm = get_realm("zulip")+ expected_url = stream_message_url(+ realm=realm,+ message=dict(+ id=self.reported_message.id,+ stream_id=self.reported_message.recipient.type_id,+ display_recipient=self.reported_message.recipient.label(),+ topic=self.reported_message.topic_name(),+ ),+ )+ self.assertEqual(+ confirmation_dm.content,+ f"You reported [a message]({expected_url}) for spam.",+ )++ @time_machine.travel(MOCKED_DATE_SENT, tick=False)+ def test_dm_report_sends_confirmation_dm(self) -> None:+ reporting_user = self.example_user("hamlet")+ reported_dm_id = self.send_personal_message(+ self.reported_user,+ reporting_user,+ content="I eat pizza with a fork",+ )+ reported_dm = self.get_last_message()+ assert reported_dm.id == reported_dm_id++ notification_bot = get_system_bot(settings.NOTIFICATION_BOT, self.realm.id)+ result = self.report_message(reporting_user, reported_dm_id, report_type="harassment")+ self.assert_json_success(result)++ confirmation_dm = most_recent_message(reporting_user)+ self.assertEqual(confirmation_dm.sender_id, notification_bot.id)++ realm = get_realm("zulip")+ expected_url = pm_message_url(+ realm,+ dict(+ id=reported_dm.id,+ display_recipient=get_display_recipient(reported_dm.recipient),+ ),+ )+ self.assertEqual(+ confirmation_dm.content,+ f"You reported [a message]({expected_url}) for harassment.",+ )++ def test_failed_report_sends_no_confirmation_dm(self) -> None:+ do_set_realm_moderation_request_channel(+ self.hamlet.realm, None, -1, acting_user=self.hamlet+ )+ message_count_before = Message.objects.count()++ result = self.report_message(+ self.hamlet,+ self.reported_message_id,+ report_type="spam",+ )+ self.assert_json_error(result, "Message reporting is not enabled in this organization.")+ self.assertEqual(Message.objects.count(), message_count_before)+def test_failed_internal_send_raises_error(self) -> None:with mock.patch("zerver.lib.message_report.internal_send_stream_message",diff --git a/zerver/views/message_report.py b/zerver/views/message_report.pyindex a5128b4a374..1fdfe75fdbc 100644--- a/zerver/views/message_report.py+++ b/zerver/views/message_report.py@@ -7,7 +7,7 @@ from pydantic import StringConstraintsfrom zerver.lib.exceptions import JsonableErrorfrom zerver.lib.message import access_message-from zerver.lib.message_report import send_message_report+from zerver.lib.message_report import send_message_report, send_report_confirmation_dmfrom zerver.lib.response import json_successfrom zerver.lib.typed_endpoint import typed_endpointfrom zerver.lib.typed_endpoint_validators import check_string_in_validator@@ -43,4 +43,7 @@ def report_message_backend(if message_report_id is None:raise JsonableError(_("Failed to send the message report."))++ send_report_confirmation_dm(user_profile, user_profile.realm, reported_message, report_type)+return json_success(request)