Only send stream update events to clients that need them

Stream name and descriptions updates were being sent to all of the
active users on a realm. They are now only send to users who would have
information about that stream.

(imported from commit 2621ee8029f7356bf44ec493d7b5361bd546a8f5)
This commit is contained in:
Jason Michalski
2014-01-27 17:43:05 -05:00
parent c573efd437
commit 05e00575bb
2 changed files with 28 additions and 4 deletions

View File

@@ -86,6 +86,14 @@ def log_event(event):
def active_user_ids(realm): def active_user_ids(realm):
return [userdict['id'] for userdict in get_active_user_dicts_in_realm(realm)] return [userdict['id'] for userdict in get_active_user_dicts_in_realm(realm)]
def stream_user_ids(stream):
subscriptions = Subscription.objects.filter(recipient__type=Recipient.STREAM,
recipient__type_id=stream.id)
if stream.invite_only:
subscriptions = subscriptions.filter(active=True)
return [sub['user_profile_id'] for sub in subscriptions.values('user_profile_id')]
def notify_created_user(user_profile): def notify_created_user(user_profile):
event = dict(type="realm_user", op="add", event = dict(type="realm_user", op="add",
person=dict(email=user_profile.email, person=dict(email=user_profile.email,
@@ -1246,7 +1254,7 @@ def do_rename_stream(realm, old_name, new_name, log=True):
value=new_name, value=new_name,
name=old_name name=old_name
) )
send_event(event, active_user_ids(realm)) send_event(event, stream_user_ids(stream))
# Even though the token doesn't change, the web client needs to update the # Even though the token doesn't change, the web client needs to update the
# email forwarding address to display the correctly-escaped new name. # email forwarding address to display the correctly-escaped new name.
@@ -1260,7 +1268,7 @@ def do_change_stream_description(realm, stream_name, new_description):
event = dict(type='stream', op='update', event = dict(type='stream', op='update',
property='description', name=stream_name, property='description', name=stream_name,
value=new_description) value=new_description)
send_event(event, active_user_ids(realm)) send_event(event, stream_user_ids(stream))
return {} return {}
def do_create_realm(domain, name, restricted_to_domain=True): def do_create_realm(domain, name, restricted_to_domain=True):

View File

@@ -391,6 +391,8 @@ class StreamAdminTest(AuthedTestCase):
value='stream_name2', value='stream_name2',
name='stream_name1' name='stream_name1'
)) ))
users = events[0]['users']
self.assertEqual(users, [user_profile.id])
stream_name1_exists = Stream.objects.filter( stream_name1_exists = Stream.objects.filter(
name='stream_name1', name='stream_name1',
@@ -422,10 +424,23 @@ class StreamAdminTest(AuthedTestCase):
do_add_subscription(user_profile, stream, no_log=True) do_add_subscription(user_profile, stream, no_log=True)
do_change_is_admin(user_profile, True) do_change_is_admin(user_profile, True)
result = self.client_patch('/json/streams/stream_name1', events = []
{'description': ujson.dumps('Test description')}) with tornado_redirected_to_list(events):
result = self.client_patch('/json/streams/stream_name1',
{'description': ujson.dumps('Test description')})
self.assert_json_success(result) self.assert_json_success(result)
event = events[0]['event']
self.assertEqual(event, dict(
op='update',
type='stream',
property='description',
value='Test description',
name='stream_name1'
))
users = events[0]['users']
self.assertEqual(users, [user_profile.id])
stream = Stream.objects.get( stream = Stream.objects.get(
name='stream_name1', name='stream_name1',
realm=realm, realm=realm,
@@ -436,6 +451,7 @@ class StreamAdminTest(AuthedTestCase):
email = 'hamlet@zulip.com' email = 'hamlet@zulip.com'
self.login(email) self.login(email)
user_profile = get_user_profile_by_email(email) user_profile = get_user_profile_by_email(email)
realm = user_profile.realm realm = user_profile.realm
stream, _ = create_stream_if_needed(realm, 'stream_name1') stream, _ = create_stream_if_needed(realm, 'stream_name1')
do_add_subscription(user_profile, stream, no_log=True) do_add_subscription(user_profile, stream, no_log=True)