changeset 2019:c0ff84243650

quick_frontend(chat): better handling of cached signals when initialising widget
author Goffi <goffi@goffi.org>
date Sun, 24 Jul 2016 18:02:34 +0200
parents 7199e6bdb94e
children f67da1cab6d3
files frontends/src/quick_frontend/quick_chat.py src/bridge/bridge_constructor/bridge_template.ini
diffstat 2 files changed, 10 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/quick_frontend/quick_chat.py	Sun Jul 24 17:59:24 2016 +0200
+++ b/frontends/src/quick_frontend/quick_chat.py	Sun Jul 24 18:02:34 2016 +0200
@@ -155,7 +155,7 @@
         quick_widgets.QuickWidget.__init__(self, host, target, profiles=profiles)
         self._locked = True   # True when we are waiting for history/search
                               # messageNew signals are cached when locked
-        self._cache = []
+        self._cache = OrderedDict()
         assert type_ in (C.CHAT_ONE2ONE, C.CHAT_GROUP)
         self.current_target = target
         self.type = type_
@@ -288,6 +288,7 @@
         @param profile (str): %(doc_profile)s
         """
         self._locked = True
+        self._cache = OrderedDict()
         self.messages.clear()
         self.historyPrint(size, filters, profile)
 
@@ -298,8 +299,9 @@
         the focus after the history is printed
         """
         self._locked = False
-        for data in self._cache:
+        for data in self._cache.itervalues():
             self.messageNew(*data)
+        del self._cache
 
     def historyPrint(self, size=C.HISTORY_LIMIT_DEFAULT, filters=None, profile='@NONE@'):
         """Print the current history
@@ -330,6 +332,10 @@
             #     previous_day = message_day
             for data in history:
                 uid, timestamp, from_jid, to_jid, message, subject, type_, extra = data
+                # cached messages may already be in history
+                # so we check it to avoid duplicates, they'll be added later
+                if uid in self._cache:
+                    continue
                 from_jid = jid.JID(from_jid)
                 to_jid = jid.JID(to_jid)
                 # if ((self.type == C.CHAT_GROUP and type_ != C.MESS_TYPE_GROUPCHAT) or
@@ -345,9 +351,8 @@
         self.host.bridge.historyGet(unicode(self.host.profiles[profile].whoami.bare), unicode(target), size, True, filters, profile, callback=_historyGetCb, errback=_historyGetEb)
 
     def messageNew(self, uid, timestamp, from_jid, to_jid, msg, subject, type_, extra, profile):
-        log.debug(u"messageNew ==> {}".format((uid, timestamp, from_jid, to_jid, msg, subject, type_, extra, profile)))
         if self._locked:
-            self._cache.append(uid, timestamp, from_jid, to_jid, msg, subject, type_, extra, profile)
+            self._cache[uid] = (uid, timestamp, from_jid, to_jid, msg, subject, type_, extra, profile)
             return
         if self.type == C.CHAT_GROUP:
             if to_jid.resource and type_ != C.MESS_TYPE_GROUPCHAT:
--- a/src/bridge/bridge_constructor/bridge_template.ini	Sun Jul 24 17:59:24 2016 +0200
+++ b/src/bridge/bridge_constructor/bridge_template.ini	Sun Jul 24 18:02:34 2016 +0200
@@ -457,7 +457,7 @@
 doc_param_1=message: body of the message:
     key is the language of the body, use '' when unknown
 doc_param_2=subject: Subject of the message
-    key is the language of the subkect, use '' when unknown
+    key is the language of the subject, use '' when unknown
 doc_param_3=mess_type: Type of the message (cf RFC 6121 ยง5.2.2) or "auto" for automatic type detection
 doc_param_4=extra: optional data that can be used by a plugin to build more specific messages 
 doc_param_5=%(doc_profile_key)s