diff src/core/sat_main.py @ 2536:27539029a662

core: added bare_jid and identities arguments to discoGetByFeatures: - bare_jid indicate if we are looking for server implementation (True) of devices (False) - identities is an optional filter, only entities with this identities set will be kept
author Goffi <goffi@goffi.org>
date Mon, 26 Mar 2018 08:01:06 +0200
parents 7da86e1633a5
children
line wrap: on
line diff
--- a/src/core/sat_main.py	Sun Mar 25 20:51:02 2018 +0200
+++ b/src/core/sat_main.py	Mon Mar 26 08:01:06 2018 +0200
@@ -671,15 +671,19 @@
     def findFeaturesSet(self, *args, **kwargs):
         return self.memory.disco.findFeaturesSet(*args, **kwargs)
 
-    def _findByFeatures(self, namespace, service, roster, own_jid, profile_key):
+    def _findByFeatures(self, namespaces, identities, bare_jids, service, roster, own_jid, profile_key):
         client = self.getClient(profile_key)
-        return self.findByFeatures(client, namespace, service, roster, own_jid)
+        return self.findByFeatures(client, namespaces, identities, bare_jids, service, roster, own_jid)
 
     @defer.inlineCallbacks
-    def findByFeatures(self, client, namespaces, service, roster, own_jid):
+    def findByFeatures(self, client, namespaces, identities=None, bare_jids=False, service=True, roster=True, own_jid=True):
         """retrieve all services or contacts managing a set a features
 
         @param namespaces(list[unicode]): features which must be handled
+        @param identities(list[tuple[unicode,unicode]], None): if not None or empty, only keep those identities
+            tuple must by (category, type)
+        @param bare_jids(bool): retrieve only bare_jids if True
+            if False, retrieve full jid of connected devices
         @param service(bool): if True return service from our roster
         @param roster(bool): if True, return entities in roster
             full jid of all matching resources available will be returned
@@ -689,8 +693,10 @@
             - own entities
             - roster entities
         """
-        if not namespaces:
-            raise exceptions.DataError("namespaces must not be empty")
+        if not identities:
+            identities = None
+        if not namespaces and not identities:
+            raise exceptions.DataError("at least one namespace or one identity must be set")
         found_service = {}
         found_own = {}
         found_roster = {}
@@ -698,8 +704,10 @@
             services_jids = yield self.findFeaturesSet(client, namespaces)
             for service_jid in services_jids:
                 infos = yield self.getDiscoInfos(client, service_jid)
-                identities = [(cat, type_, name or u'') for (cat, type_), name in infos.identities.iteritems()]
-                found_service[service_jid.full()] = identities
+                if identities is not None and not set(infos.identities.keys()).issuperset(identities):
+                    continue
+                found_identities = [(cat, type_, name or u'') for (cat, type_), name in infos.identities.iteritems()]
+                found_service[service_jid.full()] = found_identities
 
         jids = []
         if roster:
@@ -711,18 +719,25 @@
                             (found_roster, client.roster.getJids())):
             for jid_ in jids:
                 if jid_.resource:
+                    if bare_jids:
+                        continue
                     resources = [jid_.resource]
                 else:
-                    try:
-                        resources = self.memory.getAllResources(client, jid_)
-                    except exceptions.UnknownEntityError:
-                        continue
+                    if bare_jids:
+                        resources = [None]
+                    else:
+                        try:
+                            resources = self.memory.getAllResources(client, jid_)
+                        except exceptions.UnknownEntityError:
+                            continue
                 for resource in resources:
                     full_jid = jid.JID(tuple=(jid_.user, jid_.host, resource))
                     infos = yield self.getDiscoInfos(client, full_jid)
                     if infos.features.issuperset(namespaces):
-                        identities = [(cat, type_, name or u'') for (cat, type_), name in infos.identities.iteritems()]
-                        found[full_jid.full()] = identities
+                        if identities is not None and not set(infos.identities.keys()).issuperset(identities):
+                            continue
+                        found_identities = [(cat, type_, name or u'') for (cat, type_), name in infos.identities.iteritems()]
+                        found[full_jid.full()] = found_identities
 
         defer.returnValue((found_service, found_own, found_roster))