changeset 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 32b6893240e0
children 472fadadefe6
files sat/plugins/plugin_exp_events.py sat/plugins/plugin_exp_invitation.py sat/plugins/plugin_exp_invitation_file.py
diffstat 3 files changed, 62 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_exp_events.py	Fri May 03 13:00:08 2019 +0200
+++ b/sat/plugins/plugin_exp_events.py	Fri May 03 13:05:01 2019 +0200
@@ -231,7 +231,8 @@
             raise exceptions.NotFound(_(u"No event with this id has been found"))
         defer.returnValue(event_elt)
 
-    def register(self, client, service, node, event_id, event_elt, creator=False):
+    def register(self, client, name, extra, service, node, event_id, event_elt,
+                 creator=False):
         """register evenement in personal events list
 
         @param service(jid.JID): pubsub service of the event
@@ -241,6 +242,7 @@
             note that this element will be modified in place
         @param creator(bool): True if client's profile is the creator of the node
         """
+        # FIXME: name and extra are unused
         link_elt = event_elt.addElement("link")
         link_elt["service"] = service.full()
         link_elt["node"] = node
@@ -371,7 +373,8 @@
         yield self._p.publish(client, service, node, items=[item_elt])
 
         if register:
-            yield self.register(client, service, node, event_id, event_elt, creator=True)
+            yield self.register(
+                client, service, None, {}, node, event_id, event_elt, creator=True)
         defer.returnValue(node)
 
     def _eventModify(self, service, node, id_, timestamp_update, data_update,
@@ -546,6 +549,9 @@
         @param node(unicode): event node
         @param item_id(unicode): event id
         """
+        # FIXME: handle name and extra
+        name = u''
+        extra = {}
         if self._b is None:
             raise exceptions.FeatureNotFound(
                 _(u'"XEP-0277" (blog) plugin is needed for this feature')
@@ -590,7 +596,7 @@
         # now we send the invitation
         pubsub_invitation = self.host.plugins[u'PUBSUB_INVITATION']
         pubsub_invitation.sendPubsubInvitation(client, invitee_jid, service, node,
-                                                item_id)
+                                                item_id, name, extra)
 
     def _inviteByEmail(self, service, node, id_=NS_EVENT, email=u"", emails_extra=None,
                        name=u"", host_name=u"", language=u"", url_template=u"",
--- 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):
--- a/sat/plugins/plugin_exp_invitation_file.py	Fri May 03 13:00:08 2019 +0200
+++ b/sat/plugins/plugin_exp_invitation_file.py	Fri May 03 13:05:01 2019 +0200
@@ -20,6 +20,7 @@
 from sat.core.i18n import _
 from sat.core.constants import Const as C
 from sat.core.log import getLogger
+from sat.tools.common import data_format
 from twisted.words.protocols.jabber import jid
 
 log = getLogger(__name__)
@@ -48,22 +49,24 @@
         host.bridge.addMethod(
             "FISInvite",
             ".plugin",
-            in_sign="sssssss",
+            in_sign="ssssssss",
             out_sign="",
             method=self._sendFileSharingInvitation,
         )
 
     def _sendFileSharingInvitation(
             self, invitee_jid_s, service_s, repos_type=None, namespace=None, path=None,
-            name=None, profile_key=C.PROF_KEY_NONE):
+            name=None, extra_s=u'', profile_key=C.PROF_KEY_NONE):
         client = self.host.getClient(profile_key)
         invitee_jid = jid.JID(invitee_jid_s)
         service = jid.JID(service_s)
+        extra = data_format.deserialise(extra_s)
         return self.host.plugins[u"INVITATION"].sendFileSharingInvitation(
-            client, invitee_jid, service, repos_type=None, namespace=None, path=None,
-            name=None)
+            client, invitee_jid, service, repos_type=repos_type or None,
+            namespace=namespace or None, path=path or None, name=name or None,
+            extra=extra)
 
-    def onInvitation(self, client, service, repos_type, namespace, path, name):
+    def onInvitation(self, client, name, extra, service, repos_type, namespace, path):
         if repos_type == u"files":
             type_human = _(u"file sharing")
         elif repos_type == u"photos":
@@ -79,4 +82,4 @@
             profile=client.profile, type_human=type_human, namespace=namespace, path=path)
             )
         return self.host.plugins[u'LIST_INTEREST'].registerFileSharing(
-            client, service, repos_type, namespace, path, name)
+            client, service, repos_type, namespace, path, name, extra)