diff sat/plugins/plugin_exp_invitation.py @ 2931:b256e90612d0

plugins invitation*, events: added an extra parameter and use it to keep a thumnail URL
author Goffi <goffi@goffi.org>
date Fri, 03 May 2019 13:05:01 +0200
parents 672e6be3290f
children 83cbd4545274
line wrap: on
line diff
--- a/sat/plugins/plugin_exp_invitation.py	Fri May 03 13:00:08 2019 +0200
+++ b/sat/plugins/plugin_exp_invitation.py	Fri May 03 13:05:01 2019 +0200
@@ -68,19 +68,22 @@
         @param callback(callbable): method handling the invitation
             For pubsub invitation, it will be called with following arguments:
                 - client
+                - name(unicode, None): name of the event
+                - extra(dict): extra data
                 - service(jid.JID): pubsub service jid
                 - node(unicode): pubsub node
                 - item_id(unicode, None): pubsub item id
                 - item_elt(domish.Element): item of the invitation
             For file sharing invitation, it will be called with following arguments:
                 - client
+                - name(unicode, None): name of the repository
+                - extra(dict): extra data
                 - service(jid.JID): service jid of the file repository
                 - repos_type(unicode): type of the repository, can be:
                     - files: generic file sharing
                     - photos: photos album
                 - namespace(unicode, None): namespace of the repository
                 - path(unicode, None): path of the repository
-                - name(unicode, None): name of the repository
         @raise exceptions.ConflictError: this namespace is already registered
         """
         if namespace in self._ns_cb:
@@ -89,10 +92,13 @@
                 .format(namespace=namespace, callback=self._ns_cb[namespace]))
         self._ns_cb[namespace] = callback
 
-    def _generateBaseInvitation(self, client, invitee_jid):
+    def _generateBaseInvitation(self, client, invitee_jid, name, extra):
         """Generate common mess_data end invitation_elt
 
         @param invitee_jid(jid.JID): entitee to send invitation to
+        @param name(unicode, None): name of the shared repository
+        @param extra(dict, None): extra data, where key can be:
+            - thumb_url: URL of a thumbnail
         @return (tuple[dict, domish.Element): mess_data and invitation_elt
         """
         mess_data = {
@@ -106,18 +112,33 @@
         }
         client.generateMessageXML(mess_data)
         invitation_elt = mess_data["xml"].addElement("invitation", NS_INVITATION)
+        if name is not None:
+            invitation_elt[u"name"] = name
+        thumb_url = extra.get(u'thumb_url')
+        if thumb_url:
+            if not thumb_url.startswith(u'http'):
+                log.warning(
+                    u"only http URLs are allowed for thumbnails, got {url}, ignoring"
+                    .format(url=thumb_url))
+            else:
+                invitation_elt[u'thumb_url'] = thumb_url
         return mess_data, invitation_elt
 
     def sendPubsubInvitation(self, client, invitee_jid, service, node,
-                              item_id):
+                              item_id, name, extra):
         """Send an pubsub invitation in a <message> stanza
 
         @param invitee_jid(jid.JID): entitee to send invitation to
         @param service(jid.JID): pubsub service
         @param node(unicode): pubsub node
         @param item_id(unicode): pubsub id
+        @param name(unicode, None): see [_generateBaseInvitation]
+        @param extra(dict, None): see [_generateBaseInvitation]
         """
-        mess_data, invitation_elt = self._generateBaseInvitation(client, invitee_jid)
+        if extra is None:
+            extra = {}
+        mess_data, invitation_elt = self._generateBaseInvitation(
+            client, invitee_jid, name, extra)
         pubsub_elt = invitation_elt.addElement(u"pubsub")
         pubsub_elt[u"service"] = service.full()
         pubsub_elt[u"node"] = node
@@ -125,7 +146,7 @@
         return client.send(mess_data[u"xml"])
 
     def sendFileSharingInvitation(self, client, invitee_jid, service, repos_type=None,
-                                  namespace=None, path=None, name=None):
+                                  namespace=None, path=None, name=None, extra=None):
         """Send a file sharing invitation in a <message> stanza
 
         @param invitee_jid(jid.JID): entitee to send invitation to
@@ -135,14 +156,19 @@
             - "photos": photos album
         @param namespace(unicode, None): namespace of the shared repository
         @param path(unicode, None): path of the shared repository
-        @param name(unicode, None): name of the shared repository
+        @param name(unicode, None): see [_generateBaseInvitation]
+        @param extra(dict, None): see [_generateBaseInvitation]
         """
-        mess_data, invitation_elt = self._generateBaseInvitation(client, invitee_jid)
+        if extra is None:
+            extra = {}
+        mess_data, invitation_elt = self._generateBaseInvitation(
+            client, invitee_jid, name, extra)
         file_sharing_elt = invitation_elt.addElement(u"file_sharing")
         file_sharing_elt[u"service"] = service.full()
         if repos_type is not None:
             if repos_type not in (u"files", "photos"):
-                msg = u"unknown repository type: {repos_type}".format(repos_type=repos_type)
+                msg = u"unknown repository type: {repos_type}".format(
+                    repos_type=repos_type)
                 log.warning(msg)
                 raise exceptions.DateError(msg)
             file_sharing_elt[u"type"] = repos_type
@@ -150,8 +176,6 @@
             file_sharing_elt[u"namespace"] = namespace
         if path is not None:
             file_sharing_elt[u"path"] = path
-        if name is not None:
-            file_sharing_elt[u"name"] = name
         return client.send(mess_data[u"xml"])
 
     @defer.inlineCallbacks
@@ -195,8 +219,7 @@
         repos_type = file_sharing_elt.getAttribute(u"type", u"files")
         namespace = file_sharing_elt.getAttribute(u"namespace")
         path = file_sharing_elt.getAttribute(u"path")
-        name = file_sharing_elt.getAttribute(u"name")
-        args = [service, repos_type, namespace, path, name]
+        args = [service, repos_type, namespace, path]
         ns_fis = self.host.getNamespace(u"fis")
         return ns_fis, args
 
@@ -204,6 +227,12 @@
     def onInvitation(self, message_elt, client):
         log.debug(u"invitation received [{profile}]".format(profile=client.profile))
         invitation_elt = message_elt.invitation
+
+        name = invitation_elt.getAttribute(u"name")
+        extra = {}
+        if invitation_elt.hasAttribute(u"thumb_url"):
+            extra[u'thumb_url'] = invitation_elt[u'thumb_url']
+
         for elt in invitation_elt.elements():
             if elt.uri != NS_INVITATION:
                 log.warning(u"unexpected element: {xml}".format(xml=elt.toXml()))
@@ -226,10 +255,11 @@
             try:
                 cb = self._ns_cb[namespace]
             except KeyError:
-                log.warning(_(u'No handler for namespace "{namespace}", invitation ignored')
+                log.warning(_(
+                    u'No handler for namespace "{namespace}", invitation ignored')
                     .format(namespace=namespace))
             else:
-                cb(client, *args)
+                cb(client, name, extra, *args)
 
 
 class PubsubInvitationHandler(XMPPHandler):