comparison sat_frontends/quick_frontend/quick_chat.py @ 2880:dd650f3e070f

quick frontend(chat): prepared factorisation of merging of user moved info messages: - new MessageWidget class, to have a common ancestor for widgets from different frontends - new message_widgets_rev property to get reverse iterator on message widgets - new isUserMoved method to check if a Message is a user moved info - new handleUserMoved method to merge info message when suitable. This is code from Primitivus made generic.
author Goffi <goffi@goffi.org>
date Thu, 28 Mar 2019 08:39:19 +0100
parents 33a69ee7fbb7
children ca55e02a6fc8
comparison
equal deleted inserted replaced
2879:5ba98fd6c9a4 2880:dd650f3e070f
209 nick = self.nick 209 nick = self.nick
210 for lang, mess in self.message.iteritems(): 210 for lang, mess in self.message.iteritems():
211 self.message[lang] = u"* " + nick + mess[3:] 211 self.message[lang] = u"* " + nick + mess[3:]
212 212
213 213
214 class MessageWidget(object):
215 """Base classe for widgets"""
216 # This class does nothing and is only used to have a common ancestor
217
218 pass
219
220
214 class Occupant(object): 221 class Occupant(object):
215 """Occupant metadata""" 222 """Occupant metadata"""
216 223
217 def __init__(self, parent, data, profile): 224 def __init__(self, parent, data, profile):
218 self.parent = parent 225 self.parent = parent
363 self.host.removeListener("avatar", self.onAvatar) 370 self.host.removeListener("avatar", self.onAvatar)
364 371
365 @property 372 @property
366 def contact_list(self): 373 def contact_list(self):
367 return self.host.contact_lists[self.profile] 374 return self.host.contact_lists[self.profile]
375
376 @property
377 def message_widgets_rev(self):
378 """Return the history of MessageWidget in reverse chronological order
379
380 Must be implemented by frontend
381 """
382 raise NotImplementedError
368 383
369 ## synchornisation handling ## 384 ## synchornisation handling ##
370 385
371 @quick_widgets.QuickWidget.sync.setter 386 @quick_widgets.QuickWidget.sync.setter
372 def sync(self, state): 387 def sync(self, state):
743 You need to override historyPrint to handle the later 758 You need to override historyPrint to handle the later
744 @param message(Message): message data 759 @param message(Message): message data
745 """ 760 """
746 raise NotImplementedError 761 raise NotImplementedError
747 762
763 def isUserMoved(self, message):
764 """Return True if message is a user left/joined message
765
766 @param message(Message): message to check
767 @return (bool): True is message is user moved info message
768 """
769 if message.type != C.MESS_TYPE_INFO:
770 return False
771 try:
772 info_type = message.extra["info_type"]
773 except KeyError:
774 return False
775 else:
776 return info_type in ROOM_USER_MOVED
777
778 def handleUserMoved(self, message):
779 """Check if this message is a UserMoved one, and merge it when possible
780
781 "merge it" means that info message indicating a user joined/left will be
782 grouped if no other non-info messages has been sent since
783 @param message(Message): message to check
784 @return (bool): True if this message has been merged
785 if True, a new MessageWidget must not be created and appended to history
786 """
787 if self.isUserMoved(message):
788 for wid in self.message_widgets_rev:
789 # we merge in/out messages if no message was sent meanwhile
790 if not isinstance(wid, MessageWidget):
791 continue
792 elif wid.mess_data.type != C.MESS_TYPE_INFO:
793 return False
794 elif (
795 wid.info_type in ROOM_USER_MOVED
796 and wid.mess_data.nick == message.nick
797 ):
798 try:
799 count = wid.reentered_count
800 except AttributeError:
801 count = wid.reentered_count = 1
802 nick = wid.mess_data.nick
803 if message.info_type == ROOM_USER_LEFT:
804 wid.message = _(u"<= {nick} has left the room ({count})").format(
805 nick=nick, count=count
806 )
807 else:
808 wid.message = _(
809 u"<=> {nick} re-entered the room ({count})"
810 ).format(nick=nick, count=count)
811 wid.reentered_count += 1
812 return True
813 return False
814
748 def printDayChange(self, day): 815 def printDayChange(self, day):
749 """Display the day on a new line. 816 """Display the day on a new line.
750 817
751 @param day(unicode): day to display (or not if this method is not overwritten) 818 @param day(unicode): day to display (or not if this method is not overwritten)
752 """ 819 """