mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 21:43:21 +00:00
send_message_backend: Add support for recipient user IDs.
Note that create_mirrored_message_users has not been updated to use user IDs. Tweaked by tabbott to add a unit test for the huddle case as well.
This commit is contained in:
@@ -1950,7 +1950,8 @@ def check_send_private_message(sender: UserProfile, client: Client,
|
||||
# check_send_message:
|
||||
# Returns the id of the sent message. Has same argspec as check_message.
|
||||
def check_send_message(sender: UserProfile, client: Client, message_type_name: str,
|
||||
message_to: Sequence[str], topic_name: Optional[str],
|
||||
message_to: Union[Sequence[int], Sequence[str]],
|
||||
topic_name: Optional[str],
|
||||
message_content: str, realm: Optional[Realm]=None,
|
||||
forged: bool=False, forged_timestamp: Optional[float]=None,
|
||||
forwarder_user_profile: Optional[UserProfile]=None,
|
||||
|
||||
@@ -85,7 +85,7 @@ from zerver.models import (
|
||||
Message, Realm, Recipient, Stream, UserMessage, UserProfile, Attachment,
|
||||
RealmAuditLog, RealmDomain, get_realm, UserPresence, Subscription,
|
||||
get_stream, get_stream_recipient, get_system_bot, get_user, Reaction,
|
||||
flush_per_request_caches, ScheduledMessage
|
||||
flush_per_request_caches, ScheduledMessage, get_huddle_recipient
|
||||
)
|
||||
|
||||
|
||||
@@ -1395,6 +1395,51 @@ class MessagePOSTTest(ZulipTestCase):
|
||||
"to": self.example_email("othello")})
|
||||
self.assert_json_success(result)
|
||||
|
||||
def test_personal_message_by_id(self) -> None:
|
||||
"""
|
||||
Sending a personal message to a valid user ID is successful.
|
||||
"""
|
||||
self.login(self.example_email("hamlet"))
|
||||
result = self.client_post(
|
||||
"/json/messages",
|
||||
{
|
||||
"type": "private",
|
||||
"content": "Test message",
|
||||
"client": "test suite",
|
||||
"to": ujson.dumps([self.example_user("othello").id])
|
||||
}
|
||||
)
|
||||
self.assert_json_success(result)
|
||||
|
||||
msg = self.get_last_message()
|
||||
self.assertEqual("Test message", msg.content)
|
||||
self.assertEqual(msg.recipient_id, self.example_user("othello").id)
|
||||
|
||||
def test_group_personal_message_by_id(self) -> None:
|
||||
"""
|
||||
Sending a personal message to a valid user ID is successful.
|
||||
"""
|
||||
self.login(self.example_email("hamlet"))
|
||||
result = self.client_post(
|
||||
"/json/messages",
|
||||
{
|
||||
"type": "private",
|
||||
"content": "Test message",
|
||||
"client": "test suite",
|
||||
"to": ujson.dumps([self.example_user("othello").id,
|
||||
self.example_user("cordelia").id])
|
||||
}
|
||||
)
|
||||
self.assert_json_success(result)
|
||||
|
||||
msg = self.get_last_message()
|
||||
self.assertEqual("Test message", msg.content)
|
||||
self.assertEqual(msg.recipient_id, get_huddle_recipient(
|
||||
{self.example_user("hamlet").id,
|
||||
self.example_user("othello").id,
|
||||
self.example_user("cordelia").id}).id
|
||||
)
|
||||
|
||||
def test_personal_message_copying_self(self) -> None:
|
||||
"""
|
||||
Sending a personal message to yourself plus another user is successful,
|
||||
@@ -1715,6 +1760,22 @@ class MessagePOSTTest(ZulipTestCase):
|
||||
subdomain="notzephyr")
|
||||
self.assert_json_error(result, "Zephyr mirroring is not allowed in this organization")
|
||||
|
||||
@mock.patch("zerver.views.messages.create_mirrored_message_users")
|
||||
def test_send_message_when_client_is_zephyr_mirror_but_recipient_is_user_id(
|
||||
self, create_mirrored_message_users_mock: Any) -> None:
|
||||
create_mirrored_message_users_mock.return_value = (True, True)
|
||||
user = self.mit_user("starnine")
|
||||
user_id = user.id
|
||||
user_email = user.email
|
||||
self.login(user_email, realm=get_realm("zephyr"))
|
||||
result = self.client_post("/json/messages", {"type": "private",
|
||||
"sender": self.mit_email("sipbtest"),
|
||||
"content": "Test message",
|
||||
"client": "zephyr_mirror",
|
||||
"to": ujson.dumps([user_id])},
|
||||
subdomain="zephyr")
|
||||
self.assert_json_error(result, "Mirroring not allowed with recipient user IDs")
|
||||
|
||||
def test_send_message_irc_mirror(self) -> None:
|
||||
self.login(self.example_email('hamlet'))
|
||||
bot_info = {
|
||||
|
||||
@@ -6,7 +6,7 @@ from django.core.exceptions import ValidationError
|
||||
from django.db import connection
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from typing import Dict, List, Set, Any, Callable, Iterable, \
|
||||
Optional, Tuple, Union, Sequence
|
||||
Optional, Tuple, Union, Sequence, cast
|
||||
from zerver.lib.exceptions import JsonableError, ErrorCode
|
||||
from zerver.lib.html_diff import highlight_html_differences
|
||||
from zerver.decorator import has_request_variables, \
|
||||
@@ -1207,7 +1207,8 @@ def handle_deferred_message(sender: UserProfile, client: Client,
|
||||
@has_request_variables
|
||||
def send_message_backend(request: HttpRequest, user_profile: UserProfile,
|
||||
message_type_name: str=REQ('type'),
|
||||
message_to: List[str]=REQ('to', converter=extract_recipients, default=[]),
|
||||
message_to: Union[Sequence[int], Sequence[str]]=REQ(
|
||||
'to', converter=extract_recipients, default=[]),
|
||||
forged: bool=REQ(default=False),
|
||||
topic_name: Optional[str]=REQ_topic(),
|
||||
message_content: str=REQ('content'),
|
||||
@@ -1252,6 +1253,17 @@ def send_message_backend(request: HttpRequest, user_profile: UserProfile,
|
||||
return json_error(_("Missing sender"))
|
||||
if message_type_name != "private" and not is_super_user:
|
||||
return json_error(_("User not authorized for this query"))
|
||||
|
||||
# For now, mirroring only works with recipient emails, not for
|
||||
# recipient user IDs.
|
||||
if not all(isinstance(to_item, str) for to_item in message_to):
|
||||
return json_error(_("Mirroring not allowed with recipient user IDs"))
|
||||
|
||||
# We need this manual cast so that mypy doesn't complain about
|
||||
# create_mirrored_message_users not being able to accept a Sequence[int]
|
||||
# type parameter.
|
||||
message_to = cast(Sequence[str], message_to)
|
||||
|
||||
(valid_input, mirror_sender) = \
|
||||
create_mirrored_message_users(request, user_profile, message_to)
|
||||
if not valid_input:
|
||||
|
||||
Reference in New Issue
Block a user