Skip to content

Commit 31ba8ee

Browse files
committed
streams: Handle adding new stream events.
1 parent b7547dd commit 31ba8ee

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

tests/model/test_model.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,114 @@ def set_from_list_of_dict(data):
17351735
update_left_panel.assert_called_once_with()
17361736
model.controller.update_screen.assert_called_once_with()
17371737

1738+
@pytest.mark.parametrize(['event', 'expected_pinned_streams',
1739+
'expected_unpinned_streams'], [
1740+
(
1741+
{
1742+
'op': 'add',
1743+
'subscriptions': [
1744+
{
1745+
'name': 'all',
1746+
'stream_id': 4,
1747+
'description': '',
1748+
'color': '#48e',
1749+
'pin_to_top': False,
1750+
'invite_only': False
1751+
}
1752+
]
1753+
},
1754+
[{
1755+
'name': 'test',
1756+
'id': 100,
1757+
'color': '#48a',
1758+
'invite_only': False,
1759+
'description': 'abc'
1760+
}],
1761+
[{
1762+
'name': 'test2',
1763+
'id': 101,
1764+
'color': '#48b',
1765+
'invite_only': False,
1766+
'description': 'lets test'
1767+
}, {
1768+
'name': 'all',
1769+
'id': 4,
1770+
'color': '#48e',
1771+
'invite_only': False,
1772+
'description': ''
1773+
}]
1774+
),
1775+
(
1776+
{
1777+
'op': 'add',
1778+
'subscriptions': [
1779+
{
1780+
'name': 'design',
1781+
'stream_id': 2,
1782+
'description': '',
1783+
'color': '#48e',
1784+
'pin_to_top': True,
1785+
'invite_only': False
1786+
}
1787+
]
1788+
},
1789+
[{
1790+
'name': 'test',
1791+
'id': 100,
1792+
'color': '#48a',
1793+
'invite_only': False,
1794+
'description': 'abc'
1795+
}, {
1796+
'name': 'design',
1797+
'id': 2,
1798+
'color': '#48e',
1799+
'invite_only': False,
1800+
'description': ''
1801+
}],
1802+
[{
1803+
'name': 'test2',
1804+
'id': 101,
1805+
'color': '#48b',
1806+
'invite_only': False,
1807+
'description': 'lets test'
1808+
}]
1809+
)], ids=[
1810+
'add_2',
1811+
'add_and_pin_4'
1812+
]
1813+
)
1814+
def test__handle_subscription_event_add_stream(
1815+
self, model, mocker,
1816+
event,
1817+
expected_pinned_streams,
1818+
expected_unpinned_streams,
1819+
initial_pinned_streams=[{
1820+
'name': 'test',
1821+
'id': 100,
1822+
'color': '#48a',
1823+
'invite_only': False,
1824+
'description': 'abc'
1825+
}],
1826+
initial_unpinned_streams=[{
1827+
'name': 'test2',
1828+
'id': 101,
1829+
'color': '#48b',
1830+
'invite_only': False,
1831+
'description': 'lets test'
1832+
}]
1833+
):
1834+
1835+
model.pinned_streams = deepcopy(initial_pinned_streams)
1836+
model.unpinned_streams = deepcopy(initial_unpinned_streams)
1837+
1838+
model._handle_subscription_event(event)
1839+
1840+
assert model.pinned_streams == expected_pinned_streams
1841+
assert model.unpinned_streams == expected_unpinned_streams
1842+
update_left_panel = model.controller.view.left_panel.update_stream_view
1843+
update_left_panel.assert_called_once_with()
1844+
model.controller.update_screen.assert_called_once_with()
1845+
17381846
@pytest.mark.parametrize(['event', 'feature_level',
17391847
'stream_id', 'expected_subscribers'], [
17401848
({'op': 'peer_add', 'stream_id': 99, 'user_id': 12}, None,

zulipterminal/model.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
'stream_id': int,
5454
'stream_ids': List[int], # NOTE: replaces 'stream_id' in ZFL 35 for peer*
5555
'value': bool,
56-
'message_ids': List[int] # Present when subject of msg(s) is updated
56+
'message_ids': List[int], # Present when subject of msg(s) is updated,
57+
'subscriptions': List[Dict[str, Any]]
5758
}, total=False) # Each Event will only have a subset of these
5859

5960
EditPropagateMode = Literal['change_one', 'change_all', 'change_later']
@@ -741,6 +742,14 @@ def get_stream_by_id(streams: List[StreamData], stream_id: int
741742
return stream
742743
raise RuntimeError("Invalid stream id.")
743744

745+
def make_reduced_stream_data(stream: Any) -> StreamData:
746+
# stream_id has been changed to id.
747+
return StreamData({'name': stream['name'],
748+
'id': stream['stream_id'],
749+
'color': stream['color'],
750+
'invite_only': stream['invite_only'],
751+
'description': stream['description']})
752+
744753
if event['op'] == 'update':
745754
if hasattr(self.controller, 'view'):
746755
if event.get('property', None) == 'in_home_view':
@@ -785,6 +794,17 @@ def get_stream_by_id(streams: List[StreamData], stream_id: int
785794
sort_streams(self.pinned_streams)
786795
self.controller.view.left_panel.update_stream_view()
787796
self.controller.update_screen()
797+
elif event['op'] == 'add':
798+
for subscription in event['subscriptions']:
799+
stream_data = make_reduced_stream_data(subscription)
800+
self.stream_dict[subscription['stream_id']] = subscription
801+
if(subscription['pin_to_top']):
802+
self.pinned_streams.append(stream_data)
803+
else:
804+
self.unpinned_streams.append(stream_data)
805+
self.controller.view.left_panel.update_stream_view()
806+
self.controller.update_screen()
807+
788808
elif event['op'] in ('peer_add', 'peer_remove'):
789809
# NOTE: ZFL 35 commit was not atomic with API change
790810
# (ZFL >=35 can use new plural style)

0 commit comments

Comments
 (0)