changeset 2933:47df940738a7

plugin list of interest: added an interestsList bridge method + handle thumb_url
author Goffi <goffi@goffi.org>
date Fri, 03 May 2019 13:06:47 +0200
parents 472fadadefe6
children 0bafdbda7f5f
files sat/plugins/plugin_exp_list_of_interest.py
diffstat 1 files changed, 72 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_exp_list_of_interest.py	Fri May 03 13:05:47 2019 +0200
+++ b/sat/plugins/plugin_exp_list_of_interest.py	Fri May 03 13:06:47 2019 +0200
@@ -23,7 +23,7 @@
 from wokkel import disco, iwokkel, pubsub
 from zope.interface import implements
 from twisted.internet import defer
-from twisted.words.protocols.jabber import error as jabber_error
+from twisted.words.protocols.jabber import error as jabber_error, jid
 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
 from twisted.words.xish import domish
 
@@ -52,6 +52,14 @@
         log.info(_(u"List of Interest plugin initialization"))
         self.host = host
         self._p = self.host.plugins["XEP-0060"]
+        host.bridge.addMethod(
+            "interestsList",
+            ".plugin",
+            in_sign="ssss",
+            out_sign="aa{ss}",
+            method=self._listInterests,
+            async=True,
+        )
 
     def getHandler(self, client):
         return ListInterestHandler(self)
@@ -73,7 +81,7 @@
 
     @defer.inlineCallbacks
     def registerPubsub(self, client, namespace, service, node, item_id=None,
-                       creator=False, name=None, element=None):
+                       creator=False, name=None, element=None, extra=None):
         """Register an interesting element in personal list
 
         @param namespace(unicode): namespace of the interest
@@ -87,12 +95,17 @@
         @param name(unicode, None): name of the interest
         @param element(domish.Element, None): element to attach
             may be used to cache some extra data
+        @param extra(dict, None): extra data, key can be:
+            - thumb_url: http(s) URL of a thumbnail
         """
         yield self.createNode(client)
         interest_elt = domish.Element((NS_LIST_INTEREST, u"interest"))
         interest_elt[u"namespace"] = namespace
         if name is not None:
             interest_elt[u'name'] = name
+        thumb_url = extra.get(u'thumb_url')
+        if thumb_url:
+            interest_elt[u'thumb_url'] = thumb_url
         pubsub_elt = interest_elt.addElement(u"pubsub")
         pubsub_elt[u"service"] = service.full()
         pubsub_elt[u"node"] = node
@@ -109,7 +122,8 @@
 
     @defer.inlineCallbacks
     def registerFileSharing(
-            self, client, service, repos_type=None, namespace=None, path=None, name=None):
+            self, client, service, repos_type=None, namespace=None, path=None, name=None,
+            extra=None):
         """Register an interesting file repository in personal list
 
         @param service(jid.JID): service of the file repository
@@ -117,12 +131,18 @@
         @param namespace(unicode, None): namespace of the repository
         @param path(unicode, None): path of the repository
         @param name(unicode, None): name of the repository
+        @param extra(dict, None): as ad for [registerPubsub]
         """
+        if extra is None:
+            extra = {}
         yield self.createNode(client)
         interest_elt = domish.Element((NS_LIST_INTEREST, u"interest"))
         interest_elt[u"namespace"] = self.host.getNamespace(u"fis")
         if name is not None:
             interest_elt[u'name'] = name
+        thumb_url = extra.get(u'thumb_url')
+        if thumb_url:
+            interest_elt[u'thumb_url'] = thumb_url
         file_sharing_elt = interest_elt.addElement(u"file_sharing")
         file_sharing_elt[u"service"] = service.full()
         if repos_type is not None:
@@ -136,6 +156,55 @@
             client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt]
         )
 
+    def _listInterestsSerialise(self, interests_data):
+        interests = []
+        for item_elt in interests_data[0]:
+            interest_data = {}
+            interest_elt = item_elt.interest
+            if interest_elt.hasAttribute(u'name'):
+                interest_data[u'name'] = interest_elt.getAttribute(u'name')
+            if interest_elt.hasAttribute(u'thumb_url'):
+                interest_data[u'thumb_url'] = interest_elt.getAttribute(u'thumb_url')
+            elt = interest_elt.firstChildElement()
+            if elt.uri != NS_LIST_INTEREST:
+                log.warning(u"unexpected child element, ignoring: {xml}".format(
+                    xml = elt.toXml()))
+                continue
+            if elt.name == u'pubsub':
+                interest_data.update({
+                    u"type": u"pubsub",
+                    u"service": elt[u'service'],
+                    u"node": elt[u'node'],
+                })
+                for attr in (u'item', u'creator'):
+                    if elt.hasAttribute(attr):
+                        interest_data[attr] = elt[attr]
+            elif elt.name == u'file_sharing':
+                interest_data.update({
+                    u"type": u"file_sharing",
+                    u"service": elt[u'service'],
+                })
+                if elt.hasAttribute(u'type'):
+                    interest_data[u'subtype'] = elt[u'type']
+                for attr in (u'namespace', u'path'):
+                    if elt.hasAttribute(attr):
+                        interest_data[attr] = elt[attr]
+            else:
+                log.warning(u"unknown element, ignoring: {xml}".format(xml=elt.toXml()))
+                continue
+            interests.append(interest_data)
+
+        return interests
+
+    def _listInterests(self, service, node, namespace, profile):
+        service = jid.JID(service) if service else None
+        node = node or None
+        namespace = namespace or None
+        client = self.host.getClient(profile)
+        d = self.listInterests(client, service, node, namespace)
+        d.addCallback(self._listInterestsSerialise)
+        return d
+
     @defer.inlineCallbacks
     def listInterests(self, client, service=None, node=None, namespace=None):
         """Retrieve list of interests