diff sat/core/sat_main.py @ 3206:ae09989e9feb

core, bridge: new `devicesInfosGet` method to get infos on known devices of an entity
author Goffi <goffi@goffi.org>
date Fri, 06 Mar 2020 18:19:03 +0100
parents 439e2f88c3a9
children 4fbea7f1e012
line wrap: on
line diff
--- a/sat/core/sat_main.py	Fri Mar 06 18:19:03 2020 +0100
+++ b/sat/core/sat_main.py	Fri Mar 06 18:19:03 2020 +0100
@@ -21,6 +21,7 @@
 import os.path
 import uuid
 import hashlib
+import copy
 from pathlib import Path
 import sat
 from sat.core.i18n import _, D_, languageSwitch
@@ -172,6 +173,7 @@
         self.bridge.register_method("saveParamsTemplate", self.memory.save_xml)
         self.bridge.register_method("loadParamsTemplate", self.memory.load_xml)
         self.bridge.register_method("sessionInfosGet", self.getSessionInfos)
+        self.bridge.register_method("devicesInfosGet", self._getDevicesInfos)
         self.bridge.register_method("namespacesGet", self.getNamespaces)
         self.bridge.register_method("imageCheck", self._imageCheck)
         self.bridge.register_method("imageResize", self._imageResize)
@@ -676,6 +678,65 @@
             }
         return defer.succeed(data)
 
+    def _getDevicesInfos(self, bare_jid, profile_key):
+        client = self.getClient(profile_key)
+        if not bare_jid:
+            bare_jid = None
+        d = defer.ensureDeferred(self.getDevicesInfos(client, bare_jid))
+        d.addCallback(lambda data: data_format.serialise(data))
+        return d
+
+    async def getDevicesInfos(self, client, bare_jid=None):
+        """compile data on an entity devices
+
+        @param bare_jid(jid.JID, None): bare jid of entity to check
+            None to use client own jid
+        @return (list[dict]): list of data, one item per resource.
+            Following keys can be set:
+                - resource(str): resource name
+        """
+        own_jid = client.jid.userhostJID()
+        if bare_jid is None:
+            bare_jid = own_jid
+        else:
+            bare_jid = jid.JID(bare_jid)
+        resources = self.memory.getAllResources(client, bare_jid)
+        if bare_jid == own_jid:
+            # our own jid is not stored in memory's cache
+            resources.add(client.jid.resource)
+        ret_data = []
+        for resource in resources:
+            res_jid = copy.copy(bare_jid)
+            res_jid.resource = resource
+            cache_data = self.memory.getEntityData(res_jid, profile_key=client.profile)
+            res_data = {
+                "resource": resource,
+            }
+            try:
+                presence = cache_data['presence']
+            except KeyError:
+                pass
+            else:
+                res_data['presence'] = {
+                    "show": presence.show,
+                    "priority": presence.priority,
+                    "statuses": presence.statuses,
+                }
+
+            disco = await self.getDiscoInfos(client, res_jid)
+
+            for (category, type_), name in disco.identities.items():
+                identities = res_data.setdefault('identities', [])
+                identities.append({
+                    "name": name,
+                    "category": category,
+                    "type": type_,
+                })
+
+            ret_data.append(res_data)
+
+        return ret_data
+
     # images
 
     def _imageCheck(self, path):