lib/actions: Add helper recipient_for_user_ids().

This function is equivalent to recipient_for_emails, but fetches
user_profiles by IDs, not by emails.

This commit is a part of our efforts surrounding #9474, but is
more primarily geared towards adding support for sending typing
notifications by user IDs.
This commit is contained in:
Eeshan Garg
2018-08-20 22:11:48 -02:30
committed by Tim Abbott
parent 8ffc437e70
commit 91b90460ee
2 changed files with 46 additions and 2 deletions

View File

@@ -97,7 +97,7 @@ from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity,
UserGroup, UserGroupMembership, get_default_stream_groups, \
get_bot_services, get_bot_dicts_in_realm, DomainNotAllowedForRealmError, \
DisposableEmailError, EmailContainsPlusError, \
get_user_including_cross_realm
get_user_including_cross_realm, get_user_by_id_in_realm_including_cross_realm
from zerver.lib.alert_words import alert_words_in_realm
from zerver.lib.avatar import avatar_url, avatar_url_from_dict
@@ -1785,6 +1785,23 @@ def recipient_for_emails(emails: Iterable[str], not_forged_mirror_message: bool,
sender=sender
)
def recipient_for_user_ids(user_ids: Iterable[int], sender: UserProfile) -> Recipient:
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, sender.realm)
except UserProfile.DoesNotExist:
raise ValidationError(_("Invalid user ID {}".format(user_id)))
user_profiles.append(user_profile)
return recipient_for_user_profiles(
user_profiles=user_profiles,
not_forged_mirror_message=False,
forwarder_user_profile=None,
sender=sender
)
def recipient_for_user_profiles(user_profiles: List[UserProfile], not_forged_mirror_message: bool,
forwarder_user_profile: Optional[UserProfile],
sender: UserProfile) -> Recipient:

View File

@@ -3,11 +3,14 @@
import ujson
from typing import Any, Mapping, List
from django.core.exceptions import ValidationError
from zerver.lib.actions import recipient_for_user_ids
from zerver.lib.test_helpers import tornado_redirected_to_list, get_display_recipient
from zerver.lib.test_classes import (
ZulipTestCase,
)
from zerver.models import get_realm, get_user
from zerver.models import get_realm, get_user, get_display_recipient
class TypingNotificationOperatorTest(ZulipTestCase):
def test_missing_parameter(self) -> None:
@@ -222,3 +225,27 @@ class StoppedTypingNotificationTest(ZulipTestCase):
self.assertEqual(event['sender']['email'], sender.email)
self.assertEqual(event['type'], 'typing')
self.assertEqual(event['op'], 'stop')
class TypingValidationHelpersTest(ZulipTestCase):
def test_recipient_for_user_ids(self) -> None:
hamlet = self.example_user('hamlet')
othello = self.example_user('othello')
cross_realm_bot = self.example_user('welcome_bot')
sender = self.example_user('iago')
recipient_user_ids = [hamlet.id, othello.id, cross_realm_bot.id]
result = recipient_for_user_ids(recipient_user_ids, sender)
recipient = get_display_recipient(result)
recipient_ids = [recipient[0]['id'], recipient[1]['id'], # type: ignore
recipient[2]['id'], recipient[3]['id']] # type: ignore
expected_recipient_ids = [hamlet.id, othello.id,
sender.id, cross_realm_bot.id]
self.assertEqual(set(recipient_ids), set(expected_recipient_ids))
def test_recipient_for_user_ids_non_existent_id(self) -> None:
sender = self.example_user('iago')
recipient_user_ids = [999]
with self.assertRaisesRegex(ValidationError, 'Invalid user ID '):
recipient_for_user_ids(recipient_user_ids, sender)