diff sat_frontends/primitivus/chat.py @ 2881:13e0a260e7b8

primitivus (chat): use the factorised code for user moved info messages.
author Goffi <goffi@goffi.org>
date Thu, 28 Mar 2019 08:40:38 +0100
parents 003b8b4b56a7
children 5bba8953061e
line wrap: on
line diff
--- a/sat_frontends/primitivus/chat.py	Thu Mar 28 08:39:19 2019 +0100
+++ b/sat_frontends/primitivus/chat.py	Thu Mar 28 08:40:38 2019 +0100
@@ -38,7 +38,7 @@
 OCCUPANTS_FOOTER = _(u"{} occupants")
 
 
-class MessageWidget(urwid.WidgetWrap):
+class MessageWidget(urwid.WidgetWrap, quick_chat.MessageWidget):
     def __init__(self, mess_data):
         """
         @param mess_data(quick_chat.Message, None): message data
@@ -276,16 +276,8 @@
 
 
 class Chat(PrimitivusWidget, quick_chat.QuickChat):
-    def __init__(
-        self,
-        host,
-        target,
-        type_=C.CHAT_ONE2ONE,
-        nick=None,
-        occupants=None,
-        subject=None,
-        profiles=None,
-    ):
+    def __init__(self, host, target, type_=C.CHAT_ONE2ONE, nick=None, occupants=None,
+                 subject=None, profiles=None):
         quick_chat.QuickChat.__init__(
             self, host, target, type_, nick, occupants, subject, profiles=profiles
         )
@@ -315,6 +307,10 @@
         self.show_title = 1  # 0: clip title; 1: full title; 2: no title
         self.postInit()
 
+    @property
+    def message_widgets_rev(self):
+        return reversed(self.mess_walker)
+
     def keypress(self, size, key):
         if key == a_key["OCCUPANTS_HIDE"]:  # user wants to (un)hide the occupants panel
             if self.type == C.CHAT_GROUP:
@@ -329,9 +325,8 @@
         elif key == a_key["SHORT_NICKNAME"]:  # user wants to (not) use short nick
             self.show_short_nick = not self.show_short_nick
             self.redraw()
-        elif (
-            key == a_key["SUBJECT_SWITCH"]
-        ):  # user wants to (un)hide group's subject or change its apperance
+        elif (key == a_key["SUBJECT_SWITCH"]):
+            # user wants to (un)hide group's subject or change its apperance
             if self.subject:
                 self.show_title = (self.show_title + 1) % 3
                 if self.show_title == 0:
@@ -420,20 +415,6 @@
     def createMessage(self, message):
         self.appendMessage(message)
 
-    def _user_moved(self, message):
-        """return true if message is a user left/joined message
-
-        @param message(quick_chat.Message): message to add
-        """
-        if message.type != C.MESS_TYPE_INFO:
-            return False
-        try:
-            info_type = message.extra["info_type"]
-        except KeyError:
-            return False
-        else:
-            return info_type in quick_chat.ROOM_USER_MOVED
-
     def _scrollDown(self):
         """scroll down message only if we are already at the bottom (minus 1)"""
         current_focus = self.mess_widgets.focus_position
@@ -451,32 +432,9 @@
         if self.filters:
             if not all([f(message) for f in self.filters]):
                 return
-        if self._user_moved(message):
-            for wid in reversed(self.mess_walker):
-                # we merge in/out messages if no message was sent meanwhile
-                if not isinstance(wid, MessageWidget):
-                    continue
-                if wid.mess_data.type != C.MESS_TYPE_INFO:
-                    break
-                if (
-                    wid.info_type in quick_chat.ROOM_USER_MOVED
-                    and wid.mess_data.nick == message.nick
-                ):
-                    try:
-                        count = wid.reentered_count
-                    except AttributeError:
-                        count = wid.reentered_count = 1
-                    nick = wid.mess_data.nick
-                    if message.info_type == quick_chat.ROOM_USER_LEFT:
-                        wid.message = _(u"<= {nick} has left the room ({count})").format(
-                            nick=nick, count=count
-                        )
-                    else:
-                        wid.message = _(
-                            u"<=> {nick} re-entered the room ({count})"
-                        ).format(nick=nick, count=count)
-                        wid.reentered_count += 1
-                    return
+
+        if self.handleUserMoved(message):
+            return
 
         if (
             self.host.selected_widget != self or not self.host.x_notify.hasFocus()
@@ -495,7 +453,7 @@
         wid = MessageWidget(message)
         self.mess_walker.append(wid)
         self._scrollDown()
-        if self._user_moved(message):
+        if self.isUserMoved(message):
             return  # no notification for moved messages
 
         # notifications