diff sat/plugins/plugin_misc_lists.py @ 3464:54b9cdbeb335

plugin lists: new `listsList` method to retrieve lists from personal interests
author Goffi <goffi@goffi.org>
date Fri, 19 Feb 2021 15:53:20 +0100
parents 483bcfeb11c9
children c063cbb2ad6e
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_lists.py	Fri Feb 19 15:52:28 2021 +0100
+++ b/sat/plugins/plugin_misc_lists.py	Fri Feb 19 15:53:20 2021 +0100
@@ -258,6 +258,14 @@
             async_=True,
         )
         host.bridge.addMethod(
+            "listsList",
+            ".plugin",
+            in_sign="sss",
+            out_sign="s",
+            method=self._listsList,
+            async_=True,
+        )
+        host.bridge.addMethod(
             "listTemplatesNamesGet",
             ".plugin",
             in_sign="ss",
@@ -367,6 +375,51 @@
             client, service, node, values, schema, item_id, extra, deserialise, form_ns
         )
 
+    def _listsList(self, service, node, profile):
+        service = jid.JID(service) if service else None
+        node = node or None
+        client = self.host.getClient(profile)
+        d = defer.ensureDeferred(self.listsList(client, service, node))
+        d.addCallback(data_format.serialise)
+        return d
+
+    async def listsList(
+        self, client, service: Optional[jid.JID], node: Optional[str]=None
+    ) -> List[dict]:
+        """Retrieve list of pubsub lists registered in personal interests
+
+        @return list: list of lists metadata
+        """
+        items, metadata = await self.host.plugins['LIST_INTEREST'].listInterests(
+            client, service, node, namespace=APP_NS_TICKETS)
+        lists = []
+        for item in items:
+            interest_elt = item.interest
+            if interest_elt is None:
+                log.warning(f"invalid interest for {client.profile}: {item.toXml}")
+                continue
+            if interest_elt.getAttribute("namespace") != APP_NS_TICKETS:
+                continue
+            pubsub_elt = interest_elt.pubsub
+            list_data = {
+                "id": item["id"],
+                "name": interest_elt["name"],
+                "service": pubsub_elt["service"],
+                "node": pubsub_elt["node"],
+                "creator": C.bool(pubsub_elt.getAttribute("creator", C.BOOL_FALSE)),
+            }
+            try:
+                list_elt = next(pubsub_elt.elements(APP_NS_TICKETS, "list"))
+            except StopIteration:
+                pass
+            else:
+                list_type = list_data["type"] = list_elt["type"]
+                if list_type in TEMPLATES:
+                    list_data["icon_name"] = TEMPLATES[list_type]["icon"]
+            lists.append(list_data)
+
+        return lists
+
     def _getTemplatesNames(self, language, profile):
         client = self.host.getClient(profile)
         return data_format.serialise(self.getTemplatesNames(client, language))