83
83
from zulipterminal .platform_code import notify
84
84
from zulipterminal .ui_tools .utils import create_msg_box_list
85
85
86
+ from zulipterminal .ui_tools .messages import PlaceholderMessageBox
86
87
87
88
class ServerConnectionFailure (Exception ):
88
89
pass
@@ -1665,39 +1666,20 @@ def notify_user(self, message: Message) -> str:
1665
1666
text ,
1666
1667
)
1667
1668
return ""
1668
-
1669
1669
def _handle_message_event (self , event : Event ) -> None :
1670
- """
1671
- Handle new messages (eg. add message to the end of the view)
1672
- """
1673
1670
assert event ["type" ] == "message"
1674
1671
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.
1677
1672
message ["flags" ] = event .get ("flags" , [])
1678
- # We need to update the topic order in index, unconditionally.
1679
1673
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.
1685
1674
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.
1689
1675
if hasattr (self .controller , "view" ):
1690
1676
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" ]):
1694
1678
view .topic_w .update_topics_list (
1695
1679
message ["stream_id" ], message ["subject" ], message ["sender_id" ]
1696
1680
)
1697
1681
self .controller .update_screen ()
1698
1682
1699
- # We can notify user regardless of whether UI is rendered or not,
1700
- # but depend upon the UI to indicate failures.
1701
1683
failed_command = self .notify_user (message )
1702
1684
if (
1703
1685
failed_command
@@ -1720,31 +1702,33 @@ def _handle_message_event(self, event: Event) -> None:
1720
1702
self .controller .update_screen ()
1721
1703
self ._notified_user_of_notification_failure = True
1722
1704
1723
- # Index messages before calling set_count.
1724
1705
self .index = index_messages ([message ], self , self .index )
1725
1706
if "read" not in message ["flags" ]:
1726
1707
set_count ([message ["id" ]], self .controller , 1 )
1727
1708
1728
- if hasattr (self .controller , "view" ) and self ._have_last_message .get (
1729
- repr (self .narrow ), False
1730
- ):
1709
+ if hasattr (self .controller , "view" ):
1731
1710
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 )
1744
1713
if self .current_narrow_contains_message (message ):
1745
- msg_log . append ( msg_w )
1714
+ self . _have_last_message [ narrow_str ] = True
1746
1715
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 ()
1748
1732
1749
1733
def _update_topic_index (self , stream_id : int , topic_name : str ) -> None :
1750
1734
"""
0 commit comments