diff libervia/backend/plugins/plugin_exp_invitation.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4b842c1fb686
children
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_exp_invitation.py	Tue Jun 18 12:06:45 2024 +0200
+++ b/libervia/backend/plugins/plugin_exp_invitation.py	Wed Jun 19 18:44:57 2024 +0200
@@ -45,9 +45,7 @@
 }
 
 NS_INVITATION = "https://salut-a-toi/protocol/invitation:0"
-INVITATION = '/message/invitation[@xmlns="{ns_invit}"]'.format(
-    ns_invit=NS_INVITATION
-)
+INVITATION = '/message/invitation[@xmlns="{ns_invit}"]'.format(ns_invit=NS_INVITATION)
 NS_INVITATION_LIST = NS_INVITATION + "#list"
 
 
@@ -91,8 +89,10 @@
         """
         if namespace in self._ns_cb:
             raise exceptions.ConflictError(
-                "invitation namespace {namespace} is already register with {callback}"
-                .format(namespace=namespace, callback=self._ns_cb[namespace]))
+                "invitation namespace {namespace} is already register with {callback}".format(
+                    namespace=namespace, callback=self._ns_cb[namespace]
+                )
+            )
         self._ns_cb[namespace] = callback
 
     def _generate_base_invitation(self, client, invitee_jid, name, extra):
@@ -118,14 +118,16 @@
         invitation_elt = mess_data["xml"].addElement("invitation", NS_INVITATION)
         if name is not None:
             invitation_elt["name"] = name
-        thumb_url = extra.get('thumb_url')
+        thumb_url = extra.get("thumb_url")
         if thumb_url:
-            if not thumb_url.startswith('http'):
+            if not thumb_url.startswith("http"):
                 log.warning(
-                    "only http URLs are allowed for thumbnails, got {url}, ignoring"
-                    .format(url=thumb_url))
+                    "only http URLs are allowed for thumbnails, got {url}, ignoring".format(
+                        url=thumb_url
+                    )
+                )
             else:
-                invitation_elt['thumb_url'] = thumb_url
+                invitation_elt["thumb_url"] = thumb_url
         return mess_data, invitation_elt
 
     def send_pubsub_invitation(
@@ -136,7 +138,7 @@
         node: str,
         item_id: Optional[str],
         name: Optional[str],
-        extra: Optional[dict]
+        extra: Optional[dict],
     ) -> None:
         """Send an pubsub invitation in a <message> stanza
 
@@ -151,7 +153,8 @@
         if extra is None:
             extra = {}
         mess_data, invitation_elt = self._generate_base_invitation(
-            client, invitee_jid, name, extra)
+            client, invitee_jid, name, extra
+        )
         pubsub_elt = invitation_elt.addElement("pubsub")
         pubsub_elt["service"] = service.full()
         pubsub_elt["node"] = node
@@ -173,8 +176,15 @@
         client.send(mess_data["xml"])
 
     async def send_file_sharing_invitation(
-        self, client, invitee_jid, service, repos_type=None, namespace=None, path=None,
-        name=None, extra=None
+        self,
+        client,
+        invitee_jid,
+        service,
+        repos_type=None,
+        namespace=None,
+        path=None,
+        name=None,
+        extra=None,
     ):
         """Send a file sharing invitation in a <message> stanza
 
@@ -196,7 +206,7 @@
         # FIXME: not the best place to adapt permission, but it's necessary to check them
         #   for UX
         try:
-            await self.host.plugins['XEP-0329'].affiliationsSet(
+            await self.host.plugins["XEP-0329"].affiliationsSet(
                 client, service, namespace, path, {invitee_jid: "member"}
             )
         except Exception as e:
@@ -214,18 +224,20 @@
                 )
             else:
                 try:
-                    extra['thumb_url'] = own_interest['thumb_url']
+                    extra["thumb_url"] = own_interest["thumb_url"]
                 except KeyError:
                     pass
 
         mess_data, invitation_elt = self._generate_base_invitation(
-            client, invitee_jid, name, extra)
+            client, invitee_jid, name, extra
+        )
         file_sharing_elt = invitation_elt.addElement("file_sharing")
         file_sharing_elt["service"] = service.full()
         if repos_type is not None:
             if repos_type not in ("files", "photos"):
                 msg = "unknown repository type: {repos_type}".format(
-                    repos_type=repos_type)
+                    repos_type=repos_type
+                )
                 log.warning(msg)
                 raise exceptions.DateError(msg)
             file_sharing_elt["type"] = repos_type
@@ -250,8 +262,9 @@
                     client, service, node, item_ids=[item_id]
                 )
             except Exception as e:
-                log.warning(_("Can't get item linked with invitation: {reason}").format(
-                            reason=e))
+                log.warning(
+                    _("Can't get item linked with invitation: {reason}").format(reason=e)
+                )
             try:
                 item_elt = items[0]
             except IndexError:
@@ -261,8 +274,9 @@
             try:
                 namespace = item_elt.firstChildElement().uri
             except Exception as e:
-                log.warning(_("Can't retrieve namespace of invitation: {reason}").format(
-                    reason = e))
+                log.warning(
+                    _("Can't retrieve namespace of invitation: {reason}").format(reason=e)
+                )
                 raise exceptions.DataError
 
             args = [service, node, item_id, item_elt]
@@ -271,7 +285,7 @@
                 node_data_elt = next(pubsub_elt.elements(NS_INVITATION, "node_data"))
             except StopIteration:
                 raise exceptions.DataError("Bad invitation, ignoring")
-            namespace = node_data_elt['namespace']
+            namespace = node_data_elt["namespace"]
             args = [service, node, None, node_data_elt]
 
         return namespace, args
@@ -296,7 +310,7 @@
         name = invitation_elt.getAttribute("name")
         extra = {}
         if invitation_elt.hasAttribute("thumb_url"):
-            extra['thumb_url'] = invitation_elt['thumb_url']
+            extra["thumb_url"] = invitation_elt["thumb_url"]
 
         for elt in invitation_elt.elements():
             if elt.uri != NS_INVITATION:
@@ -307,22 +321,26 @@
             elif elt.name == "file_sharing":
                 method = self._parse_file_sharing_elt
             else:
-                log.warning("not implemented invitation element: {xml}".format(
-                    xml = elt.toXml()))
+                log.warning(
+                    "not implemented invitation element: {xml}".format(xml=elt.toXml())
+                )
                 continue
             try:
                 namespace, args = await method(client, elt)
             except exceptions.DataError:
-                log.warning("Can't parse invitation element: {xml}".format(
-                            xml = elt.toXml()))
+                log.warning(
+                    "Can't parse invitation element: {xml}".format(xml=elt.toXml())
+                )
                 continue
 
             try:
                 cb = self._ns_cb[namespace]
             except KeyError:
-                log.warning(_(
-                    'No handler for namespace "{namespace}", invitation ignored')
-                    .format(namespace=namespace))
+                log.warning(
+                    _(
+                        'No handler for namespace "{namespace}", invitation ignored'
+                    ).format(namespace=namespace)
+                )
             else:
                 await utils.as_deferred(cb, client, namespace, name, extra, *args)