2018-08-16 21:45:10 +02:00
|
|
|
from typing import Iterable, List, Optional, Sequence, Union, cast
|
2017-04-27 22:48:06 +02:00
|
|
|
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-08-18 05:01:22 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2022-01-11 21:49:40 +01:00
|
|
|
from zerver.lib.string_validation import check_stream_topic
|
2017-08-18 02:15:51 +02:00
|
|
|
from zerver.models import (
|
2017-09-25 23:20:44 +02:00
|
|
|
Realm,
|
2020-06-11 00:54:34 +02:00
|
|
|
Stream,
|
2017-08-18 02:15:51 +02:00
|
|
|
UserProfile,
|
2018-08-16 20:54:49 +02:00
|
|
|
get_user_by_id_in_realm_including_cross_realm,
|
2020-06-11 00:54:34 +02:00
|
|
|
get_user_including_cross_realm,
|
2017-08-18 02:15:51 +02:00
|
|
|
)
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2018-08-21 01:59:28 +02:00
|
|
|
def get_user_profiles(emails: Iterable[str], realm: Realm) -> List[UserProfile]:
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
user_profiles: List[UserProfile] = []
|
2017-08-18 02:15:51 +02:00
|
|
|
for email in emails:
|
|
|
|
try:
|
2017-09-25 21:51:47 +02:00
|
|
|
user_profile = get_user_including_cross_realm(email, realm)
|
2017-08-18 02:15:51 +02:00
|
|
|
except UserProfile.DoesNotExist:
|
2020-06-15 23:22:24 +02:00
|
|
|
raise JsonableError(_("Invalid email '{}'").format(email))
|
2017-08-18 02:15:51 +02:00
|
|
|
user_profiles.append(user_profile)
|
|
|
|
return user_profiles
|
2017-04-27 22:48:06 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-08-16 20:54:49 +02:00
|
|
|
def get_user_profiles_by_ids(user_ids: Iterable[int], realm: Realm) -> List[UserProfile]:
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
user_profiles: List[UserProfile] = []
|
2018-08-16 20:54:49 +02:00
|
|
|
for user_id in user_ids:
|
|
|
|
try:
|
|
|
|
user_profile = get_user_by_id_in_realm_including_cross_realm(user_id, realm)
|
|
|
|
except UserProfile.DoesNotExist:
|
2019-04-20 03:49:03 +02:00
|
|
|
raise JsonableError(_("Invalid user ID {}").format(user_id))
|
2018-08-16 20:54:49 +02:00
|
|
|
user_profiles.append(user_profile)
|
|
|
|
return user_profiles
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-05 11:37:41 +01:00
|
|
|
class Addressee:
|
2017-04-27 22:48:06 +02:00
|
|
|
# This is really just a holder for vars that tended to be passed
|
|
|
|
# around in a non-type-safe way before this class was introduced.
|
|
|
|
#
|
|
|
|
# It also avoids some nonsense where you have to think about whether
|
|
|
|
# topic should be None or '' for a PM, or you have to make an array
|
|
|
|
# of one stream.
|
|
|
|
#
|
|
|
|
# Eventually we can use this to cache Stream and UserProfile objects
|
|
|
|
# in memory.
|
|
|
|
#
|
|
|
|
# This should be treated as an immutable class.
|
2021-02-12 08:19:30 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
msg_type: str,
|
|
|
|
user_profiles: Optional[Sequence[UserProfile]] = None,
|
|
|
|
stream: Optional[Stream] = None,
|
|
|
|
stream_name: Optional[str] = None,
|
|
|
|
stream_id: Optional[int] = None,
|
|
|
|
topic: Optional[str] = None,
|
|
|
|
) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
assert msg_type in ["stream", "private"]
|
|
|
|
if msg_type == "stream" and topic is None:
|
2019-03-20 22:53:26 +01:00
|
|
|
raise JsonableError(_("Missing topic"))
|
2017-04-27 22:48:06 +02:00
|
|
|
self._msg_type = msg_type
|
2017-08-18 05:01:22 +02:00
|
|
|
self._user_profiles = user_profiles
|
2019-02-07 02:05:34 +01:00
|
|
|
self._stream = stream
|
2017-04-27 22:48:06 +02:00
|
|
|
self._stream_name = stream_name
|
2019-01-26 03:39:02 +01:00
|
|
|
self._stream_id = stream_id
|
2017-04-27 22:48:06 +02:00
|
|
|
self._topic = topic
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def is_stream(self) -> bool:
|
2021-02-12 08:20:45 +01:00
|
|
|
return self._msg_type == "stream"
|
2017-04-27 22:48:06 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def is_private(self) -> bool:
|
2021-02-12 08:20:45 +01:00
|
|
|
return self._msg_type == "private"
|
2017-04-27 22:48:06 +02:00
|
|
|
|
2019-08-10 00:30:33 +02:00
|
|
|
def user_profiles(self) -> Sequence[UserProfile]:
|
2021-02-12 08:19:30 +01:00
|
|
|
assert self.is_private()
|
2019-08-10 00:30:33 +02:00
|
|
|
assert self._user_profiles is not None
|
|
|
|
return self._user_profiles
|
2017-04-27 22:48:06 +02:00
|
|
|
|
2019-02-07 02:05:34 +01:00
|
|
|
def stream(self) -> Optional[Stream]:
|
2021-02-12 08:19:30 +01:00
|
|
|
assert self.is_stream()
|
2019-02-07 02:05:34 +01:00
|
|
|
return self._stream
|
|
|
|
|
2019-01-28 05:28:29 +01:00
|
|
|
def stream_name(self) -> Optional[str]:
|
2021-02-12 08:19:30 +01:00
|
|
|
assert self.is_stream()
|
2017-04-27 22:48:06 +02:00
|
|
|
return self._stream_name
|
|
|
|
|
2019-01-26 03:39:02 +01:00
|
|
|
def stream_id(self) -> Optional[int]:
|
2021-02-12 08:19:30 +01:00
|
|
|
assert self.is_stream()
|
2019-01-26 03:39:02 +01:00
|
|
|
return self._stream_id
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def topic(self) -> str:
|
2021-02-12 08:19:30 +01:00
|
|
|
assert self.is_stream()
|
|
|
|
assert self._topic is not None
|
2017-04-27 22:48:06 +02:00
|
|
|
return self._topic
|
|
|
|
|
|
|
|
@staticmethod
|
2021-02-12 08:19:30 +01:00
|
|
|
def legacy_build(
|
|
|
|
sender: UserProfile,
|
2023-04-18 17:23:58 +02:00
|
|
|
recipient_type_name: str,
|
2021-02-12 08:19:30 +01:00
|
|
|
message_to: Union[Sequence[int], Sequence[str]],
|
|
|
|
topic_name: Optional[str],
|
|
|
|
realm: Optional[Realm] = None,
|
2021-02-12 08:20:45 +01:00
|
|
|
) -> "Addressee":
|
2017-04-27 22:48:06 +02:00
|
|
|
# For legacy reason message_to used to be either a list of
|
|
|
|
# emails or a list of streams. We haven't fixed all of our
|
|
|
|
# callers yet.
|
2017-09-25 21:55:02 +02:00
|
|
|
if realm is None:
|
|
|
|
realm = sender.realm
|
|
|
|
|
2023-04-18 17:23:58 +02:00
|
|
|
if recipient_type_name == "stream":
|
2017-04-27 22:48:06 +02:00
|
|
|
if len(message_to) > 1:
|
|
|
|
raise JsonableError(_("Cannot send to multiple streams"))
|
|
|
|
|
|
|
|
if message_to:
|
2019-01-26 03:39:02 +01:00
|
|
|
stream_name_or_id = message_to[0]
|
2017-04-27 22:48:06 +02:00
|
|
|
else:
|
|
|
|
# This is a hack to deal with the fact that we still support
|
|
|
|
# default streams (and the None will be converted later in the
|
2022-02-08 00:13:33 +01:00
|
|
|
# call path).
|
2017-09-28 21:14:08 +02:00
|
|
|
if sender.default_sending_stream:
|
2022-02-08 00:13:33 +01:00
|
|
|
# Use the user's default stream
|
2019-01-30 03:54:28 +01:00
|
|
|
stream_name_or_id = sender.default_sending_stream.id
|
2017-09-28 21:14:08 +02:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
raise JsonableError(_("Missing stream"))
|
2017-04-27 22:48:06 +02:00
|
|
|
|
2019-03-20 22:53:26 +01:00
|
|
|
if topic_name is None:
|
|
|
|
raise JsonableError(_("Missing topic"))
|
|
|
|
|
2019-01-26 03:39:02 +01:00
|
|
|
if isinstance(stream_name_or_id, int):
|
2020-06-16 23:29:42 +02:00
|
|
|
return Addressee.for_stream_id(stream_name_or_id, topic_name)
|
2019-01-26 03:39:02 +01:00
|
|
|
|
2020-06-16 23:29:42 +02:00
|
|
|
return Addressee.for_stream_name(stream_name_or_id, topic_name)
|
2023-04-18 17:23:58 +02:00
|
|
|
elif recipient_type_name == "private":
|
2018-08-25 00:24:46 +02:00
|
|
|
if not message_to:
|
|
|
|
raise JsonableError(_("Message must have recipients"))
|
|
|
|
|
2018-08-16 21:45:10 +02:00
|
|
|
if isinstance(message_to[0], str):
|
|
|
|
emails = cast(Sequence[str], message_to)
|
|
|
|
return Addressee.for_private(emails, realm)
|
|
|
|
elif isinstance(message_to[0], int):
|
|
|
|
user_ids = cast(Sequence[int], message_to)
|
|
|
|
return Addressee.for_user_ids(user_ids=user_ids, realm=realm)
|
2017-04-27 22:48:06 +02:00
|
|
|
else:
|
|
|
|
raise JsonableError(_("Invalid message type"))
|
|
|
|
|
2019-02-07 02:05:34 +01:00
|
|
|
@staticmethod
|
2021-02-12 08:20:45 +01:00
|
|
|
def for_stream(stream: Stream, topic: str) -> "Addressee":
|
2022-01-11 21:54:50 +01:00
|
|
|
topic = topic.strip()
|
|
|
|
check_stream_topic(topic)
|
2019-02-07 02:05:34 +01:00
|
|
|
return Addressee(
|
2021-02-12 08:20:45 +01:00
|
|
|
msg_type="stream",
|
2019-02-07 02:05:34 +01:00
|
|
|
stream=stream,
|
|
|
|
topic=topic,
|
|
|
|
)
|
|
|
|
|
2017-04-27 22:48:06 +02:00
|
|
|
@staticmethod
|
2021-02-12 08:20:45 +01:00
|
|
|
def for_stream_name(stream_name: str, topic: str) -> "Addressee":
|
2022-01-11 21:54:50 +01:00
|
|
|
topic = topic.strip()
|
|
|
|
check_stream_topic(topic)
|
2017-04-27 22:48:06 +02:00
|
|
|
return Addressee(
|
2021-02-12 08:20:45 +01:00
|
|
|
msg_type="stream",
|
2017-04-27 22:48:06 +02:00
|
|
|
stream_name=stream_name,
|
|
|
|
topic=topic,
|
|
|
|
)
|
|
|
|
|
2019-01-26 03:39:02 +01:00
|
|
|
@staticmethod
|
2021-02-12 08:20:45 +01:00
|
|
|
def for_stream_id(stream_id: int, topic: str) -> "Addressee":
|
2022-01-11 21:54:50 +01:00
|
|
|
topic = topic.strip()
|
|
|
|
check_stream_topic(topic)
|
2019-01-26 03:39:02 +01:00
|
|
|
return Addressee(
|
2021-02-12 08:20:45 +01:00
|
|
|
msg_type="stream",
|
2019-01-26 03:39:02 +01:00
|
|
|
stream_id=stream_id,
|
|
|
|
topic=topic,
|
|
|
|
)
|
|
|
|
|
2017-04-27 22:48:06 +02:00
|
|
|
@staticmethod
|
2021-02-12 08:20:45 +01:00
|
|
|
def for_private(emails: Sequence[str], realm: Realm) -> "Addressee":
|
2018-08-25 00:24:46 +02:00
|
|
|
assert len(emails) > 0
|
2017-09-25 21:52:55 +02:00
|
|
|
user_profiles = get_user_profiles(emails, realm)
|
2017-04-27 22:48:06 +02:00
|
|
|
return Addressee(
|
2021-02-12 08:20:45 +01:00
|
|
|
msg_type="private",
|
2017-08-18 05:01:22 +02:00
|
|
|
user_profiles=user_profiles,
|
2017-04-27 22:48:06 +02:00
|
|
|
)
|
|
|
|
|
2018-08-16 20:54:49 +02:00
|
|
|
@staticmethod
|
2021-02-12 08:20:45 +01:00
|
|
|
def for_user_ids(user_ids: Sequence[int], realm: Realm) -> "Addressee":
|
2018-08-25 00:24:46 +02:00
|
|
|
assert len(user_ids) > 0
|
2018-08-16 20:54:49 +02:00
|
|
|
user_profiles = get_user_profiles_by_ids(user_ids, realm)
|
|
|
|
return Addressee(
|
2021-02-12 08:20:45 +01:00
|
|
|
msg_type="private",
|
2018-08-16 20:54:49 +02:00
|
|
|
user_profiles=user_profiles,
|
|
|
|
)
|
|
|
|
|
2017-04-27 22:48:06 +02:00
|
|
|
@staticmethod
|
2021-02-12 08:20:45 +01:00
|
|
|
def for_user_profile(user_profile: UserProfile) -> "Addressee":
|
2017-08-18 05:02:02 +02:00
|
|
|
user_profiles = [user_profile]
|
2017-04-27 22:48:06 +02:00
|
|
|
return Addressee(
|
2021-02-12 08:20:45 +01:00
|
|
|
msg_type="private",
|
2017-08-18 05:01:22 +02:00
|
|
|
user_profiles=user_profiles,
|
2017-04-27 22:48:06 +02:00
|
|
|
)
|