8383from zulipterminal .platform_code import notify
8484from zulipterminal .ui_tools .utils import create_msg_box_list
8585
86+ from zulipterminal .ui_tools .messages import PlaceholderMessageBox
8687
8788class ServerConnectionFailure (Exception ):
8889 pass
@@ -1665,39 +1666,20 @@ def notify_user(self, message: Message) -> str:
16651666 text ,
16661667 )
16671668 return ""
1668-
16691669 def _handle_message_event (self , event : Event ) -> None :
1670- """
1671- Handle new messages (eg. add message to the end of the view)
1672- """
16731670 assert event ["type" ] == "message"
16741671 message = self .modernize_message_response (event ["message" ])
1675- # sometimes `flags` are missing in `event` so initialize
1676- # an empty list of flags in that case.
16771672 message ["flags" ] = event .get ("flags" , [])
1678- # We need to update the topic order in index, unconditionally.
16791673 if message ["type" ] == "stream" :
1680- # NOTE: The subsequent helper only updates the topic index based
1681- # on the message event not the UI (the UI is updated in a
1682- # consecutive block independently). However, it is critical to keep
1683- # the topics index synchronized as it used whenever the topics list
1684- # view is reconstructed later.
16851674 self ._update_topic_index (message ["stream_id" ], message ["subject" ])
1686- # If the topic view is toggled for incoming message's
1687- # recipient stream, then we re-arrange topic buttons
1688- # with most recent at the top.
16891675 if hasattr (self .controller , "view" ):
16901676 view = self .controller .view
1691- if view .left_panel .is_in_topic_view_with_stream_id (
1692- message ["stream_id" ]
1693- ):
1677+ if view .left_panel .is_in_topic_view_with_stream_id (message ["stream_id" ]):
16941678 view .topic_w .update_topics_list (
16951679 message ["stream_id" ], message ["subject" ], message ["sender_id" ]
16961680 )
16971681 self .controller .update_screen ()
16981682
1699- # We can notify user regardless of whether UI is rendered or not,
1700- # but depend upon the UI to indicate failures.
17011683 failed_command = self .notify_user (message )
17021684 if (
17031685 failed_command
@@ -1720,31 +1702,33 @@ def _handle_message_event(self, event: Event) -> None:
17201702 self .controller .update_screen ()
17211703 self ._notified_user_of_notification_failure = True
17221704
1723- # Index messages before calling set_count.
17241705 self .index = index_messages ([message ], self , self .index )
17251706 if "read" not in message ["flags" ]:
17261707 set_count ([message ["id" ]], self .controller , 1 )
17271708
1728- if hasattr (self .controller , "view" ) and self ._have_last_message .get (
1729- repr (self .narrow ), False
1730- ):
1709+ if hasattr (self .controller , "view" ):
17311710 msg_log = self .controller .view .message_view .log
1732- if msg_log :
1733- last_message = msg_log [- 1 ].original_widget .message
1734- else :
1735- last_message = None
1736- msg_w_list = create_msg_box_list (
1737- self , [message ["id" ]], last_message = last_message
1738- )
1739- if not msg_w_list :
1740- return
1741- else :
1742- msg_w = msg_w_list [0 ]
1743-
1711+ # Assume we have the latest message if this is a new message we sent
1712+ narrow_str = repr (self .narrow )
17441713 if self .current_narrow_contains_message (message ):
1745- msg_log . append ( msg_w )
1714+ self . _have_last_message [ narrow_str ] = True
17461715
1747- self .controller .update_screen ()
1716+ if self ._have_last_message .get (narrow_str , False ):
1717+ last_message = msg_log [- 1 ].original_widget .message if msg_log else None
1718+ msg_w_list = create_msg_box_list (
1719+ self , [message ["id" ]], last_message = last_message
1720+ )
1721+ if not msg_w_list :
1722+ return
1723+ msg_w = msg_w_list [0 ]
1724+
1725+ if self .current_narrow_contains_message (message ):
1726+ # Clear placeholder if present
1727+ if msg_log and isinstance (msg_log [0 ].original_widget , PlaceholderMessageBox ):
1728+ msg_log .clear ()
1729+ msg_log .append (msg_w )
1730+ self .controller .view .message_view .set_focus (len (msg_log ) - 1 )
1731+ self .controller .update_screen ()
17481732
17491733 def _update_topic_index (self , stream_id : int , topic_name : str ) -> None :
17501734 """
0 commit comments