77import urwid
88
99from zulipterminal .api_types import Message
10- from zulipterminal .ui_tools .messages import MessageBox
11-
10+ from zulipterminal .ui_tools .messages import MessageBox , PlaceholderMessageBox
11+ from typing import List , Any
1212
1313def create_msg_box_list (
1414 model : Any ,
@@ -23,7 +23,14 @@ def create_msg_box_list(
2323 if not model .narrow and messages is None :
2424 messages = list (model .index ["all_msg_ids" ])
2525 if messages is not None :
26- message_list = [model .index ["messages" ][id ] for id in messages ]
26+ message_list = [model .index ["messages" ][id ] for id in messages if id in model .index ["messages" ]]
27+ else :
28+ message_list = []
29+ if not message_list :
30+ placeholder = urwid .AttrMap (PlaceholderMessageBox ("No messages here" ), None , "msg_selected" )
31+ model .set_focus_in_current_narrow (0 )
32+ return [placeholder ]
33+
2734 message_list .sort (key = lambda msg : msg ["timestamp" ])
2835 w_list = []
2936 focus_msg = None
@@ -32,30 +39,29 @@ def create_msg_box_list(
3239 for msg in message_list :
3340 if is_unsubscribed_message (msg , model ):
3441 continue
35- # Remove messages of muted topics / streams.
3642 if is_muted (msg , model ):
3743 muted_msgs += 1
3844 if model .narrow == []: # Don't show in 'All messages'.
3945 continue
4046 msg_flag : Optional [str ] = "unread"
4147 flags = msg .get ("flags" )
42- # update_messages sends messages with no flags
43- # but flags are set to [] when fetching old messages.
4448 if flags and ("read" in flags ):
4549 msg_flag = None
46- elif focus_msg is None :
50+ elif ( focus_msg is None ) and ( last_msg is None ): # type: ignore[redundant-expr]
4751 focus_msg = message_list .index (msg ) - muted_msgs
4852 if msg ["id" ] == focus_msg_id :
4953 focus_msg = message_list .index (msg ) - muted_msgs
54+ # Skip invalid last_msg from placeholder
55+ if last_msg and "type" not in last_msg :
56+ last_msg = None
5057 w_list .append (
5158 urwid .AttrMap (MessageBox (msg , model , last_msg ), msg_flag , "msg_selected" )
5259 )
5360 last_msg = msg
5461 if focus_msg is not None :
5562 model .set_focus_in_current_narrow (focus_msg )
56- return w_list
57-
5863
64+ return w_list
5965# The SIM114 warnings are ignored here since combining the branches would be less clear
6066def is_muted (msg : Message , model : Any ) -> bool :
6167 # PMs cannot be muted
0 commit comments