2017-04-27 22:48:06 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2017-08-18 02:15:51 +02:00
|
|
|
from typing import Iterable, List, Optional, Sequence, Text
|
2017-04-27 22:48:06 +02:00
|
|
|
|
2017-08-18 02:15:51 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2017-04-27 22:48:06 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2017-08-18 05:01:22 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2017-04-27 22:48:06 +02:00
|
|
|
from zerver.lib.request import JsonableError
|
2017-08-18 02:15:51 +02:00
|
|
|
from zerver.models import (
|
|
|
|
UserProfile,
|
2017-08-18 12:26:43 +02:00
|
|
|
get_user_including_cross_realm,
|
2017-08-18 02:15:51 +02:00
|
|
|
)
|
2017-08-18 05:01:22 +02:00
|
|
|
import six
|
2017-08-18 02:15:51 +02:00
|
|
|
|
2017-09-25 21:51:47 +02:00
|
|
|
def user_profiles_from_unvalidated_emails(emails, realm):
|
|
|
|
# type: (Iterable[Text], Realm) -> List[UserProfile]
|
2017-08-18 02:15:51 +02:00
|
|
|
user_profiles = [] # type: List[UserProfile]
|
|
|
|
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:
|
|
|
|
raise ValidationError(_("Invalid email '%s'") % (email,))
|
|
|
|
user_profiles.append(user_profile)
|
|
|
|
return user_profiles
|
2017-04-27 22:48:06 +02:00
|
|
|
|
2017-09-25 21:51:47 +02:00
|
|
|
def get_user_profiles(emails, realm):
|
|
|
|
# type: (Iterable[Text], Realm) -> List[UserProfile]
|
2017-08-18 05:01:22 +02:00
|
|
|
try:
|
2017-09-25 21:51:47 +02:00
|
|
|
return user_profiles_from_unvalidated_emails(emails, realm)
|
2017-08-18 05:01:22 +02:00
|
|
|
except ValidationError as e:
|
|
|
|
assert isinstance(e.messages[0], six.string_types)
|
|
|
|
raise JsonableError(e.messages[0])
|
|
|
|
|
2017-04-27 22:48:06 +02:00
|
|
|
class Addressee(object):
|
|
|
|
# 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.
|
2017-08-18 05:01:22 +02:00
|
|
|
def __init__(self, msg_type, user_profiles=None, stream_name=None, topic=None):
|
|
|
|
# type: (str, Optional[Sequence[UserProfile]], Optional[Text], Text) -> None
|
2017-04-27 22:48:06 +02:00
|
|
|
assert(msg_type in ['stream', 'private'])
|
|
|
|
self._msg_type = msg_type
|
2017-08-18 05:01:22 +02:00
|
|
|
self._user_profiles = user_profiles
|
2017-04-27 22:48:06 +02:00
|
|
|
self._stream_name = stream_name
|
|
|
|
self._topic = topic
|
|
|
|
|
|
|
|
def msg_type(self):
|
|
|
|
# type: () -> str
|
|
|
|
return self._msg_type
|
|
|
|
|
|
|
|
def is_stream(self):
|
|
|
|
# type: () -> bool
|
|
|
|
return self._msg_type == 'stream'
|
|
|
|
|
|
|
|
def is_private(self):
|
|
|
|
# type: () -> bool
|
|
|
|
return self._msg_type == 'private'
|
|
|
|
|
2017-08-18 05:01:22 +02:00
|
|
|
def user_profiles(self):
|
|
|
|
# type: () -> List[UserProfile]
|
2017-04-27 22:48:06 +02:00
|
|
|
assert(self.is_private())
|
2017-08-18 05:01:22 +02:00
|
|
|
return self._user_profiles # type: ignore # assertion protects us
|
2017-04-27 22:48:06 +02:00
|
|
|
|
|
|
|
def stream_name(self):
|
|
|
|
# type: () -> Text
|
|
|
|
assert(self.is_stream())
|
|
|
|
return self._stream_name
|
|
|
|
|
|
|
|
def topic(self):
|
|
|
|
# type: () -> Text
|
|
|
|
assert(self.is_stream())
|
|
|
|
return self._topic
|
|
|
|
|
|
|
|
@staticmethod
|
2017-08-18 12:26:43 +02:00
|
|
|
def legacy_build(sender, message_type_name, message_to, topic_name):
|
|
|
|
# type: (UserProfile, Text, Sequence[Text], Text) -> 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.
|
|
|
|
if message_type_name == 'stream':
|
|
|
|
if len(message_to) > 1:
|
|
|
|
raise JsonableError(_("Cannot send to multiple streams"))
|
|
|
|
|
|
|
|
if message_to:
|
|
|
|
stream_name = message_to[0]
|
|
|
|
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
|
|
|
|
# callpath).
|
|
|
|
stream_name = None
|
|
|
|
|
|
|
|
return Addressee.for_stream(stream_name, topic_name)
|
|
|
|
elif message_type_name == 'private':
|
|
|
|
emails = message_to
|
2017-08-18 12:26:43 +02:00
|
|
|
return Addressee.for_private(emails=emails, sender=sender)
|
2017-04-27 22:48:06 +02:00
|
|
|
else:
|
|
|
|
raise JsonableError(_("Invalid message type"))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def for_stream(stream_name, topic):
|
|
|
|
# type: (Text, Text) -> Addressee
|
|
|
|
return Addressee(
|
|
|
|
msg_type='stream',
|
|
|
|
stream_name=stream_name,
|
|
|
|
topic=topic,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
2017-08-18 12:26:43 +02:00
|
|
|
def for_private(emails, sender):
|
|
|
|
# type: (Sequence[Text], UserProfile) -> Addressee
|
2017-09-25 21:51:47 +02:00
|
|
|
user_profiles = get_user_profiles(emails, sender.realm)
|
2017-04-27 22:48:06 +02:00
|
|
|
return Addressee(
|
|
|
|
msg_type='private',
|
2017-08-18 05:01:22 +02:00
|
|
|
user_profiles=user_profiles,
|
2017-04-27 22:48:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
2017-08-18 05:02:02 +02:00
|
|
|
def for_user_profile(user_profile):
|
|
|
|
# type: (UserProfile) -> Addressee
|
|
|
|
user_profiles = [user_profile]
|
2017-04-27 22:48:06 +02:00
|
|
|
return Addressee(
|
|
|
|
msg_type='private',
|
2017-08-18 05:01:22 +02:00
|
|
|
user_profiles=user_profiles,
|
2017-04-27 22:48:06 +02:00
|
|
|
)
|