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,11 +606,15 @@ def apply_event(state: Dict[str, Any],
# Add stream to never_subscribed (if not invite_only)
state['never_subscribed'].append(stream_data)
if 'streams' in state:
state['streams'].append(stream)
if 'streams' in state:
state['streams'].sort(key=lambda elt: elt["name"])
if event['op'] == 'delete':
deleted_stream_ids = {stream['stream_id'] for stream in event['streams']}
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
stream['stream_id'] not in deleted_stream_ids]
@ -624,6 +628,7 @@ def apply_event(state: Dict[str, Any],
if event['property'] == "description":
obj['rendered_description'] = event['rendered_description']
# Also update the pure streams data
if 'streams' in state:
for stream in state['streams']:
if stream['name'].lower() == event['name'].lower():
prop = event['property']

View File

@ -1439,17 +1439,20 @@ class NormalActionsTest(BaseAction):
check_hotspots('events[0]', events[0])
def test_rename_stream(self) -> None:
stream = self.make_stream('old_name')
new_name = 'stream with a brand new name'
for i, include_streams in enumerate([True, False]):
old_name = f'old name{i}'
new_name = f'new name{i}'
stream = self.make_stream(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)
events = self.verify_action(action, num_events=3, include_streams=include_streams)
check_stream_update('events[0]', events[0])
self.assertEqual(events[0]['name'], 'old_name')
self.assertEqual(events[0]['name'], old_name)
check_stream_update('events[1]', events[1])
self.assertEqual(events[1]['name'], 'old_name')
self.assertEqual(events[1]['name'], old_name)
check_message('events[2]', events[2])
@ -1469,16 +1472,16 @@ class NormalActionsTest(BaseAction):
self.assertEqual(msg[k], v)
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)
events = self.verify_action(action)
events = self.verify_action(action, include_streams=include_streams)
check_stream_delete('events[0]', events[0])
def test_subscribe_other_user_never_subscribed(self) -> None:
action = lambda: self.subscribe(self.example_user("othello"), "test_stream")
events = self.verify_action(action, num_events=2)
for i, include_streams in enumerate([True, False]):
action = lambda: self.subscribe(self.example_user("othello"), f"test_stream{i}")
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: