events: Fix apply_event for streams.

In 1bcb8d8ee8 I made
it so the webapp doesn't include "streams" in its
state from `fetch_initial_state_data`, but I didn't
address all the places in apply_event.
This commit is contained in:
Steve Howell 2020-12-01 13:13:09 +00:00 committed by Alex Vandiver
parent c566ecfb30
commit 92ce2d0e31
2 changed files with 49 additions and 41 deletions

View File

@ -606,12 +606,16 @@ def apply_event(state: Dict[str, Any],
# Add stream to never_subscribed (if not invite_only) # Add stream to never_subscribed (if not invite_only)
state['never_subscribed'].append(stream_data) state['never_subscribed'].append(stream_data)
state['streams'].append(stream) if 'streams' in state:
state['streams'].sort(key=lambda elt: elt["name"]) state['streams'].append(stream)
if 'streams' in state:
state['streams'].sort(key=lambda elt: elt["name"])
if event['op'] == 'delete': if event['op'] == 'delete':
deleted_stream_ids = {stream['stream_id'] for stream in event['streams']} deleted_stream_ids = {stream['stream_id'] for stream in event['streams']}
state['streams'] = [s for s in state['streams'] if s['stream_id'] not in deleted_stream_ids] if 'streams' in state:
state['streams'] = [s for s in state['streams'] if s['stream_id'] not in deleted_stream_ids]
state['never_subscribed'] = [stream for stream in state['never_subscribed'] if state['never_subscribed'] = [stream for stream in state['never_subscribed'] if
stream['stream_id'] not in deleted_stream_ids] stream['stream_id'] not in deleted_stream_ids]
@ -624,13 +628,14 @@ def apply_event(state: Dict[str, Any],
if event['property'] == "description": if event['property'] == "description":
obj['rendered_description'] = event['rendered_description'] obj['rendered_description'] = event['rendered_description']
# Also update the pure streams data # Also update the pure streams data
for stream in state['streams']: if 'streams' in state:
if stream['name'].lower() == event['name'].lower(): for stream in state['streams']:
prop = event['property'] if stream['name'].lower() == event['name'].lower():
if prop in stream: prop = event['property']
stream[prop] = event['value'] if prop in stream:
if prop == 'description': stream[prop] = event['value']
stream['rendered_description'] = event['rendered_description'] if prop == 'description':
stream['rendered_description'] = event['rendered_description']
elif event['type'] == 'default_streams': elif event['type'] == 'default_streams':
state['realm_default_streams'] = event['default_streams'] state['realm_default_streams'] = event['default_streams']
elif event['type'] == 'default_stream_groups': elif event['type'] == 'default_stream_groups':

View File

@ -1439,47 +1439,50 @@ class NormalActionsTest(BaseAction):
check_hotspots('events[0]', events[0]) check_hotspots('events[0]', events[0])
def test_rename_stream(self) -> None: def test_rename_stream(self) -> None:
stream = self.make_stream('old_name') for i, include_streams in enumerate([True, False]):
new_name = 'stream with a brand new name' old_name = f'old name{i}'
self.subscribe(self.user_profile, stream.name) new_name = f'new name{i}'
action = lambda: do_rename_stream(stream, new_name, self.user_profile)
events = self.verify_action(action, num_events=3)
check_stream_update('events[0]', events[0]) stream = self.make_stream(old_name)
self.assertEqual(events[0]['name'], 'old_name') self.subscribe(self.user_profile, stream.name)
action = lambda: do_rename_stream(stream, new_name, self.user_profile)
events = self.verify_action(action, num_events=3, include_streams=include_streams)
check_stream_update('events[1]', events[1]) check_stream_update('events[0]', events[0])
self.assertEqual(events[1]['name'], 'old_name') self.assertEqual(events[0]['name'], old_name)
check_message('events[2]', events[2]) check_stream_update('events[1]', events[1])
self.assertEqual(events[1]['name'], old_name)
fields = dict( check_message('events[2]', events[2])
sender_email='notification-bot@zulip.com',
display_recipient=new_name,
sender_full_name='Notification Bot',
is_me_message=False,
type='stream',
client='Internal',
)
fields[TOPIC_NAME] = 'stream events' fields = dict(
sender_email='notification-bot@zulip.com',
display_recipient=new_name,
sender_full_name='Notification Bot',
is_me_message=False,
type='stream',
client='Internal',
)
msg = events[2]['message'] fields[TOPIC_NAME] = 'stream events'
for k, v in fields.items():
self.assertEqual(msg[k], v) msg = events[2]['message']
for k, v in fields.items():
self.assertEqual(msg[k], v)
def test_deactivate_stream_neversubscribed(self) -> None: def test_deactivate_stream_neversubscribed(self) -> None:
stream = self.make_stream('old_name') for i, include_streams in enumerate([True, False]):
stream = self.make_stream(f"stream{i}")
action = lambda: do_deactivate_stream(stream) action = lambda: do_deactivate_stream(stream)
events = self.verify_action(action) events = self.verify_action(action, include_streams=include_streams)
check_stream_delete('events[0]', events[0])
check_stream_delete('events[0]', events[0])
def test_subscribe_other_user_never_subscribed(self) -> None: def test_subscribe_other_user_never_subscribed(self) -> None:
action = lambda: self.subscribe(self.example_user("othello"), "test_stream") for i, include_streams in enumerate([True, False]):
events = self.verify_action(action, num_events=2) action = lambda: self.subscribe(self.example_user("othello"), f"test_stream{i}")
check_subscription_peer_add('events[1]', events[1]) events = self.verify_action(action, num_events=2, include_streams=True)
check_subscription_peer_add('events[1]', events[1])
def test_remove_other_user_never_subscribed(self) -> None: def test_remove_other_user_never_subscribed(self) -> None:
self.subscribe(self.example_user("othello"), "test_stream") self.subscribe(self.example_user("othello"), "test_stream")