messages: Add Addressee.for_user_ids().

This commit is a part of our efforts surrounding #9474.
This commit is contained in:
Eeshan Garg
2018-08-16 16:24:49 -02:30
committed by Tim Abbott
parent 91b90460ee
commit 7fb674cc58
2 changed files with 42 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ from zerver.models import (
Realm,
UserProfile,
get_user_including_cross_realm,
get_user_by_id_in_realm_including_cross_realm,
)
def raw_pm_with_emails(email_str: str, my_email: str) -> List[str]:
@@ -31,6 +32,16 @@ def get_user_profiles(emails: Iterable[str], realm: Realm) -> List[UserProfile]:
user_profiles.append(user_profile)
return user_profiles
def get_user_profiles_by_ids(user_ids: Iterable[int], realm: Realm) -> List[UserProfile]:
user_profiles = [] # type: List[UserProfile]
for user_id in user_ids:
try:
user_profile = get_user_by_id_in_realm_including_cross_realm(user_id, realm)
except UserProfile.DoesNotExist:
raise JsonableError(_("Invalid user ID {}".format(user_id)))
user_profiles.append(user_profile)
return user_profiles
class Addressee:
# 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.
@@ -130,6 +141,14 @@ class Addressee:
user_profiles=user_profiles,
)
@staticmethod
def for_user_ids(user_ids: Sequence[int], realm: Realm) -> 'Addressee':
user_profiles = get_user_profiles_by_ids(user_ids, realm)
return Addressee(
msg_type='private',
user_profiles=user_profiles,
)
@staticmethod
def for_user_profile(user_profile: UserProfile) -> 'Addressee':
user_profiles = [user_profile]

View File

@@ -277,7 +277,6 @@ class TopicHistoryTest(ZulipTestCase):
result = self.client_get(endpoint, dict())
self.assert_json_error(result, 'Invalid stream id')
class TestCrossRealmPMs(ZulipTestCase):
def make_realm(self, domain: str) -> Realm:
realm = Realm.objects.create(string_id=domain, invite_required=False)
@@ -391,6 +390,29 @@ class TestCrossRealmPMs(ZulipTestCase):
self.send_huddle_message(user1_email, [user2_email, user3_email],
sender_realm="1.example.com")
class TestAddressee(ZulipTestCase):
def test_addressee_for_user_ids(self) -> None:
realm = get_realm('zulip')
user_ids = [self.example_user('cordelia').id,
self.example_user('hamlet').id,
self.example_user('othello').id]
result = Addressee.for_user_ids(user_ids=user_ids, realm=realm)
user_profiles = result.user_profiles()
result_user_ids = [user_profiles[0].id, user_profiles[1].id,
user_profiles[2].id]
self.assertEqual(set(result_user_ids), set(user_ids))
def test_addressee_for_user_ids_nonexistent_id(self) -> None:
def assert_invalid_user_id() -> Any:
return self.assertRaisesRegex(
JsonableError,
'Invalid user ID ')
with assert_invalid_user_id():
Addressee.for_user_ids(user_ids=[779], realm=get_realm('zulip'))
class InternalPrepTest(ZulipTestCase):
def test_returns_for_internal_sends(self) -> None: