Skip to content

Commit e8f08f3

Browse files
committed
buttons/helper/model/utils: Support API migration for muted topics.
This reflects the expected response change for muted_topics, from [stream_name, topic_name] to [stream_name, topic_name, date_muted] in Zulip version 3.0, Feature level 1 (see zulip/zulip@9340cd1). Given that we only used muted_topics for lookups, this introduces muted_topics_lookup (built from muted_topics) data structure to avoid server version/feature level checks everywhere in the codebase. Tests amended.
1 parent a88c5fa commit e8f08f3

File tree

9 files changed

+28
-10
lines changed

9 files changed

+28
-10
lines changed

tests/core/test_core.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def test_narrow_to_stream(self, mocker, controller,
8686
}
8787
controller.model.muted_streams = []
8888
controller.model.muted_topics = []
89+
controller.model.muted_topics_lookup = []
8990

9091
controller.narrow_to_stream(stream_button)
9192

@@ -114,6 +115,7 @@ def test_narrow_to_topic(self, mocker, controller,
114115
}
115116
controller.model.muted_streams = []
116117
controller.model.muted_topics = []
118+
controller.model.muted_topics_lookup = []
117119
controller.narrow_to_topic(msg_box)
118120
assert controller.model.stream_id == msg_box.stream_id
119121
assert controller.model.narrow == expected_narrow
@@ -158,6 +160,7 @@ def test_show_all_messages(self, mocker, controller, index_all_messages):
158160
}
159161
controller.model.muted_streams = []
160162
controller.model.muted_topics = []
163+
controller.model.muted_topics_lookup = []
161164

162165
controller.show_all_messages('')
163166

@@ -192,6 +195,7 @@ def test_show_all_starred(self, mocker, controller, index_all_starred):
192195
controller.model.index = index_all_starred
193196
controller.model.muted_streams = set() # FIXME Expand upon this
194197
controller.model.muted_topics = [] # FIXME Expand upon this
198+
controller.model.muted_topics_lookup = [] # FIXME Expand upon this
195199
controller.model.user_email = "some@email"
196200
controller.model.stream_dict = {
197201
205: {
@@ -216,6 +220,7 @@ def test_show_all_mentions(self, mocker, controller, index_all_mentions):
216220
controller.model.index = index_all_mentions
217221
controller.model.muted_streams = set() # FIXME Expand upon this
218222
controller.model.muted_topics = [] # FIXME Expand upon this
223+
controller.model.muted_topics_lookup = [] # FIXME Expand upon this
219224
controller.model.user_email = "some@email"
220225
controller.model.stream_dict = {
221226
205: {

tests/helper/test_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def test_classify_unread_counts(mocker, initial_data, stream_dict,
212212
model = mocker.Mock()
213213
model.stream_dict = stream_dict
214214
model.initial_data = initial_data
215-
model.muted_topics = muted_topics
215+
model.muted_topics_lookup = muted_topics
216216
model.muted_streams = muted_streams
217217
assert classify_unread_counts(model) == dict(classified_unread_counts,
218218
**vary_in_unreads)

tests/model/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ def test_is_muted_stream(self, muted_streams, stream_id, is_muted,
15191519
def test_is_muted_topic(self, topic, is_muted, stream_dict, model):
15201520
model.stream_dict = stream_dict
15211521
model.muted_streams = [1]
1522-
model.muted_topics = [
1522+
model.muted_topics_lookup = [
15231523
['Stream 2', 'muted topic'],
15241524
['Stream 1', 'muted stream muted topic'],
15251525
]

tests/ui/test_ui_tools.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ def test_update_topics_list(self, mocker, topic_view, topic_name,
630630
86: {'name': 'PTEST'}
631631
}
632632
topic_view.view.controller.model.muted_topics = []
633+
topic_view.view.controller.model.muted_topics_lookup = []
633634
topic_view.log = [mocker.Mock(topic_name=topic_name)
634635
for topic_name in topic_initial_log]
635636

@@ -2661,6 +2662,7 @@ def test_init_calls_top_button(self, mocker, width, count, title,
26612662
14: {'name': 'GSoC'},
26622663
}
26632664
controller.model.muted_topics = []
2665+
controller.model.muted_topics_lookup = []
26642666
top_button = mocker.patch(TOPBUTTON + '.__init__')
26652667
params = dict(controller=controller,
26662668
width=width,
@@ -2692,7 +2694,7 @@ def test_init_calls_mark_muted(self, mocker, stream_name, title,
26922694
mark_muted = mocker.patch(
26932695
'zulipterminal.ui_tools.buttons.TopicButton.mark_muted')
26942696
controller = mocker.Mock()
2695-
controller.model.muted_topics = muted_topics
2697+
controller.model.muted_topics_lookup = muted_topics
26962698
controller.model.stream_dict = {
26972699
205: {'name': stream_name}
26982700
}

tests/ui/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_is_muted(mocker, msg, narrow, muted_streams, muted_topics, muted):
8484
mocker.Mock(return_value=(msg.get('stream_id', '') in muted_streams))
8585
)
8686
model.narrow = narrow
87-
model.muted_topics = muted_topics
87+
model.muted_topics_lookup = muted_topics
8888
return_value = is_muted(msg, model)
8989
assert return_value is muted
9090

zulipterminal/helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def _set_count_in_view(controller: Any, new_count: int,
189189
break
190190
# FIXME: Update unread_counts['unread_topics']?
191191
if ([message['display_recipient'], msg_topic] in
192-
controller.model.muted_topics):
192+
controller.model.muted_topics_lookup):
193193
add_to_counts = False
194194
if is_open_topic_view and stream_id == toggled_stream_id:
195195
# If topic_view is open for incoming messages's stream,
@@ -443,7 +443,7 @@ def classify_unread_counts(model: Any) -> UnreadCounts:
443443
if stream_id not in model.stream_dict:
444444
continue
445445
if [model.stream_dict[stream_id]['name'],
446-
stream['topic']] in model.muted_topics:
446+
stream['topic']] in model.muted_topics_lookup:
447447
continue
448448
stream_topic = (stream_id, stream['topic'])
449449
unread_counts['unread_topics'][stream_topic] = count

zulipterminal/model.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,18 @@ def __init__(self, controller: Any) -> None:
115115
(self.stream_dict, self.muted_streams,
116116
self.pinned_streams, self.unpinned_streams) = stream_data
117117

118+
# NOTE: The expected response has been upgraded from
119+
# [stream_name, topic_name] to [stream_name, topic_name, date_muted] in
120+
# 3.0, Feature level 1.
118121
self.muted_topics = (
119-
self.initial_data['muted_topics']) # type: List[List[str]]
122+
self.initial_data['muted_topics']
123+
) # type: List[List[Union[str, int]]]
124+
# The intent is to use the following data structure for lookups without
125+
# having server version/feature level checks outside model.py.
126+
self.muted_topics_lookup = [
127+
[stream_name, topic_name]
128+
for stream_name, topic_name, date_muted in self.muted_topics
129+
]
120130

121131
groups = self.initial_data['realm_user_groups']
122132
self.user_group_by_id = {} # type: Dict[int, Dict[str, Any]]
@@ -422,7 +432,7 @@ def is_muted_topic(self, stream_id: int, topic: str) -> bool:
422432
return True
423433
stream_name = self.stream_dict[stream_id]['name']
424434
topic_to_search = [stream_name, topic] # type: List[str]
425-
return topic_to_search in self.muted_topics
435+
return topic_to_search in self.muted_topics_lookup
426436

427437
def _update_initial_data(self) -> None:
428438
# Thread Processes to reduce start time.

zulipterminal/ui_tools/buttons.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def __init__(self, stream_id: int, topic: str,
243243
count=count)
244244

245245
topic_pair = [self.stream_name, self.topic_name]
246-
if topic_pair in controller.model.muted_topics:
246+
if topic_pair in controller.model.muted_topics_lookup:
247247
self.mark_muted()
248248

249249
def mark_muted(self) -> None:

zulipterminal/ui_tools/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def is_muted(msg: Message, model: Any) -> bool:
6060
return False
6161
elif model.is_muted_stream(msg['stream_id']):
6262
return True
63-
elif [msg['display_recipient'], msg['subject']] in model.muted_topics:
63+
elif ([msg['display_recipient'], msg['subject']] in
64+
model.muted_topics_lookup):
6465
return True
6566
return False
6667

0 commit comments

Comments
 (0)