diff frontends/src/quick_frontend/quick_app.py @ 2016:f09562b0704d

quick_frontend, primitivus: better notifications handling
author Goffi <goffi@goffi.org>
date Sun, 24 Jul 2016 17:56:14 +0200
parents 90134b2e3dc4
children 01aff34e8873
line wrap: on
line diff
--- a/frontends/src/quick_frontend/quick_app.py	Sun Jul 24 17:47:09 2016 +0200
+++ b/frontends/src/quick_frontend/quick_app.py	Sun Jul 24 17:56:14 2016 +0200
@@ -604,30 +604,49 @@
         self._notifications[self._notif_id] = notif_data
         self.callListeners('notification', entity, notif_data, profile=profile)
 
-    def getNotifs(self, entity, type_=None, profile=C.PROF_KEY_NONE):
+    def getNotifs(self, entity=None, type_=None, exact_jid=None, profile=C.PROF_KEY_NONE):
         """return notifications for given entity
 
-        @param entity(jid.JID, None, C.ENTITY_ALL): bare jid of the entity to check
+        @param entity(jid.JID, None, C.ENTITY_ALL): jid of the entity to check
+            bare jid to get all notifications, full jid to filter on resource
             None to get general notifications
             C.ENTITY_ALL to get all notifications
         @param type_(unicode, None): notification type to filter
             None to get all notifications
-        @return (list[dict]): list of notifications
+        @param exact_jid(bool, None): if True, only return notifications from
+            exact entity jid (i.e. not including other resources)
+            None for automatic selection (True for full jid, False else)
+            False to get resources notifications
+            False doesn't do anything if entity is not a bare jid
+        @return (iter[dict]): notifications
         """
-        notif_dict = self.profiles[profile].notifications
-        ret = []
-        if entity == C.ENTITY_ALL:
-            for type_notifs in notif_dict.itervalues():
-                for notifs_list in type_notifs.itervalues():
-                    ret.extend(notifs_list)
-            return ret
-        key = '' if entity is None else entity.bare
-        key_notifs = notif_dict.setdefault(key, {})
-        if type_ is not None:
-            return key_notifs.get(type_, ret)
-        for notifs_list in key_notifs.itervalues():
-            ret.extend(notifs_list)
-        return ret
+        main_notif_dict = self.profiles[profile].notifications
+
+        if entity is C.ENTITY_ALL:
+            selected_notifs = main_notif_dict.itervalues()
+            exact_jid = False
+        else:
+            if entity is None:
+                key = ''
+                exact_jid = False
+            else:
+                key = entity.bare
+                if exact_jid is None:
+                    exact_jid = bool(entity.resource)
+            selected_notifs = (main_notif_dict.setdefault(key, {}),)
+
+        for notifs_from_select in selected_notifs:
+
+            if type_ is None:
+                type_notifs = notifs_from_select.itervalues()
+            else:
+                type_notifs = (notifs_from_select.get(type_, []),)
+
+            for notifs in type_notifs:
+                for notif in notifs:
+                    if exact_jid and notif['entity'] != entity:
+                        continue
+                    yield notif
 
     def clearNotifs(self, entity, type_=None, profile=C.PROF_KEY_NONE):
         """return notifications for given entity