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

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents bc898879af34
children
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_xep_0471.py	Tue Jun 18 12:06:45 2024 +0200
+++ b/libervia/backend/plugins/plugin_xep_0471.py	Wed Jun 19 18:44:57 2024 +0200
@@ -53,8 +53,11 @@
     C.PI_MODES: C.PLUG_MODE_BOTH,
     C.PI_PROTOCOLS: [],
     C.PI_DEPENDENCIES: [
-        "XEP-0060", "XEP-0080", "XEP-0447", "XEP-0470", # "INVITATION", "PUBSUB_INVITATION",
-        # "LIST_INTEREST"
+        "XEP-0060",
+        "XEP-0080",
+        "XEP-0447",
+        "XEP-0470",  # "INVITATION", "PUBSUB_INVITATION",
+        # "LIST_INTEREST"
     ],
     C.PI_RECOMMENDATIONS: ["XEP-0277", "EMAIL_INVITATION"],
     C.PI_MAIN: "XEP_0471",
@@ -81,7 +84,9 @@
         self._a = host.plugins["XEP-0470"]
         # self._i = host.plugins.get("EMAIL_INVITATION")
         host.register_namespace("events", NS_EVENTS)
-        self._a.register_attachment_handler("rsvp", NS_EVENTS, self.rsvp_get, self.rsvp_set)
+        self._a.register_attachment_handler(
+            "rsvp", NS_EVENTS, self.rsvp_get, self.rsvp_set
+        )
         # host.plugins["PUBSUB_INVITATION"].register(NS_EVENTS, self)
         host.bridge.add_method(
             "events_get",
@@ -304,9 +309,7 @@
 
         for category_elt in event_elt.elements(NS_EVENTS, "category"):
             try:
-                category_data = {
-                    "term": category_elt["term"]
-                }
+                category_data = {"term": category_elt["term"]}
             except KeyError:
                 log.warning(
                     "<category/> element is missing mandatory term: "
@@ -360,10 +363,7 @@
             elt = next(event_elt.elements(NS_EVENTS, name), None)
             if elt is not None:
                 try:
-                    event_data[name] = {
-                        "service": elt["jid"],
-                        "node": elt["node"]
-                    }
+                    event_data[name] = {"service": elt["jid"], "node": elt["node"]}
                 except KeyError:
                     log.warning(f"invalid {name} element: {elt.toXml()}")
 
@@ -373,7 +373,8 @@
         if attachments_elt:
             attachments = event_data["attachments"] = []
             for file_sharing_elt in attachments_elt.elements(
-                    self._sfs.namespace, "file-sharing"):
+                self._sfs.namespace, "file-sharing"
+            ):
                 try:
                     file_sharing_data = self._sfs.parse_file_sharing_elt(file_sharing_elt)
                 except Exception as e:
@@ -409,7 +410,7 @@
                 event_data["external"] = {
                     "jid": external_elt["jid"],
                     "node": external_elt["node"],
-                    "item": external_elt["item"]
+                    "item": external_elt["item"],
                 }
             except KeyError:
                 log.warning(f"invalid <external/> element: {external_elt.toXml()}")
@@ -417,7 +418,7 @@
         return event_data
 
     def _events_get(
-            self, service: str, node: str, event_ids: List[str], extra: str, profile_key: str
+        self, service: str, node: str, event_ids: List[str], extra: str, profile_key: str
     ):
         client = self.host.get_client(profile_key)
         d = defer.ensureDeferred(
@@ -426,7 +427,7 @@
                 jid.JID(service) if service else None,
                 node if node else NS_EVENTS,
                 event_ids,
-                data_format.deserialise(extra)
+                data_format.deserialise(extra),
             )
         )
         d.addCallback(data_format.serialise)
@@ -457,9 +458,7 @@
             try:
                 events.append(self.event_elt_2_event_data((item)))
             except (ValueError, exceptions.NotFound):
-                log.warning(
-                    f"Can't parse event for item {item['id']}: {item.toXml()}"
-                )
+                log.warning(f"Can't parse event for item {item['id']}: {item.toXml()}")
 
         return events
 
@@ -469,7 +468,7 @@
         service: str,
         node: str,
         event_id: str = "",
-        profile_key: str = C.PROF_KEY_NONE
+        profile_key: str = C.PROF_KEY_NONE,
     ):
         client = self.host.get_client(profile_key)
         return defer.ensureDeferred(
@@ -478,7 +477,7 @@
                 data_format.deserialise(data_s),
                 jid.JID(service) if service else None,
                 node or None,
-                event_id or None
+                event_id or None,
             )
         )
 
@@ -690,17 +689,19 @@
                     try:
                         next(f for f in rsvp_data["fields"] if f["name"] == "attending")
                     except StopIteration:
-                        rsvp_data["fields"].append({
-                            "type": "list-single",
-                            "name": "attending",
-                            "label": "Attending",
-                            "options": [
-                                {"label": "maybe", "value": "maybe"},
-                                {"label": "yes", "value": "yes"},
-                                {"label": "no", "value": "no"}
-                            ],
-                            "required": True
-                        })
+                        rsvp_data["fields"].append(
+                            {
+                                "type": "list-single",
+                                "name": "attending",
+                                "label": "Attending",
+                                "options": [
+                                    {"label": "maybe", "value": "maybe"},
+                                    {"label": "yes", "value": "yes"},
+                                    {"label": "no", "value": "no"},
+                                ],
+                                "required": True,
+                            }
+                        )
                 rsvp_data["namespace"] = NS_RSVP
                 rsvp_form = xml_tools.data_dict_2_data_form(rsvp_data)
                 rsvp_elt.addChild(rsvp_form.toElement())
@@ -728,10 +729,7 @@
 
         extra = event_data.get("extra")
         if extra:
-            extra_form = data_form.Form(
-                "result",
-                formNamespace=NS_EXTRA
-            )
+            extra_form = data_form.Form("result", formNamespace=NS_EXTRA)
             for node_type in ("website", "status"):
                 if node_type in extra:
                     extra_form.addField(
@@ -803,7 +801,7 @@
         event_id: str,
         service: str,
         node: str,
-        profile_key: str = C.PROF_KEY_NONE
+        profile_key: str = C.PROF_KEY_NONE,
     ) -> None:
         client = self.host.get_client(profile_key)
         defer.ensureDeferred(
@@ -846,9 +844,7 @@
     ) -> None:
         """Get RSVP answers from attachments"""
         try:
-            rsvp_elt = next(
-                attachments_elt.elements(NS_EVENTS, "rsvp")
-            )
+            rsvp_elt = next(attachments_elt.elements(NS_EVENTS, "rsvp"))
         except StopIteration:
             pass
         else:
@@ -861,7 +857,7 @@
         self,
         client: SatXMPPEntity,
         data: Dict[str, Any],
-        former_elt: Optional[domish.Element]
+        former_elt: Optional[domish.Element],
     ) -> Optional[domish.Element]:
         """update the <reaction> attachment"""
         rsvp_data = data["extra"].get("rsvp")
@@ -869,10 +865,7 @@
             return former_elt
         elif rsvp_data:
             rsvp_elt = domish.Element(
-                (NS_EVENTS, "rsvp"),
-                attribs = {
-                    "timestamp": utils.xmpp_date()
-                }
+                (NS_EVENTS, "rsvp"), attribs={"timestamp": utils.xmpp_date()}
             )
             rsvp_form = data_form.Form("submit", formNamespace=NS_RSVP)
             rsvp_form.makeFields(rsvp_data)
@@ -888,7 +881,7 @@
         item: str,
         invitees: List[str],
         extra: str,
-        profile_key: str
+        profile_key: str,
     ) -> defer.Deferred:
         client = self.host.get_client(profile_key)
         if invitees:
@@ -902,7 +895,7 @@
                 node or None,
                 item,
                 invitees_jid,
-                data_format.deserialise(extra)
+                data_format.deserialise(extra),
             )
         )
         d.addCallback(lambda ret: data_format.serialise(ret))
@@ -945,12 +938,7 @@
         return ret
 
     def _event_invitee_set(
-        self,
-        service: str,
-        node: str,
-        item: str,
-        rsvp_s: str,
-        profile_key: str
+        self, service: str, node: str, item: str, rsvp_s: str, profile_key: str
     ):
         client = self.host.get_client(profile_key)
         return defer.ensureDeferred(
@@ -959,7 +947,7 @@
                 jid.JID(service) if service else None,
                 node or None,
                 item,
-                data_format.deserialise(rsvp_s)
+                data_format.deserialise(rsvp_s),
             )
         )
 
@@ -982,20 +970,21 @@
             service = client.jid.userhostJID()
         if node is None:
             node = NS_EVENTS
-        await self._a.set_attachements(client, {
-            "service": service.full(),
-            "node": node,
-            "id": item,
-            "extra": {"rsvp": rsvp}
-        })
+        await self._a.set_attachements(
+            client,
+            {
+                "service": service.full(),
+                "node": node,
+                "id": item,
+                "extra": {"rsvp": rsvp},
+            },
+        )
 
     def _event_invitees_list(self, service, node, profile_key):
         service = jid.JID(service) if service else None
         node = node if node else NS_EVENT
         client = self.host.get_client(profile_key)
-        return defer.ensureDeferred(
-            self.event_invitees_list(client, service, node)
-        )
+        return defer.ensureDeferred(self.event_invitees_list(client, service, node))
 
     async def event_invitees_list(self, client, service, node):
         """Retrieve attendance from event node
@@ -1012,9 +1001,13 @@
                 event_elt = next(item.elements(NS_EVENT, "invitee"))
             except StopIteration:
                 # no item found, event data are not set yet
-                log.warning(_(
-                    "no data found for {item_id} (service: {service}, node: {node})"
-                    .format(item_id=item["id"], service=service, node=node)))
+                log.warning(
+                    _(
+                        "no data found for {item_id} (service: {service}, node: {node})".format(
+                            item_id=item["id"], service=service, node=node
+                        )
+                    )
+                )
             else:
                 data = {}
                 for key in ("attend", "guests"):
@@ -1032,7 +1025,7 @@
         service: jid.JID,
         node: str,
         item_id: Optional[str] = None,
-        name: str = '',
+        name: str = "",
         extra: Optional[dict] = None,
     ) -> None:
         if self._b is None:
@@ -1066,9 +1059,7 @@
                 comments_node = item["comments_node"]
             except KeyError:
                 log.debug(
-                    "no comment service set for item {item_id}".format(
-                        item_id=item["id"]
-                    )
+                    "no comment service set for item {item_id}".format(item_id=item["id"])
                 )
             else:
                 await self._p.set_node_affiliations(
@@ -1081,10 +1072,21 @@
             invitee_jid, service, node, item_id or NS_EVENT, profile_key=profile
         )
 
-    def _invite_by_email(self, service, node, id_=NS_EVENT, email="", emails_extra=None,
-                       name="", host_name="", language="", url_template="",
-                       message_subject="", message_body="",
-                       profile_key=C.PROF_KEY_NONE):
+    def _invite_by_email(
+        self,
+        service,
+        node,
+        id_=NS_EVENT,
+        email="",
+        emails_extra=None,
+        name="",
+        host_name="",
+        language="",
+        url_template="",
+        message_subject="",
+        message_body="",
+        profile_key=C.PROF_KEY_NONE,
+    ):
         client = self.host.get_client(profile_key)
         kwargs = {
             "profile": client.profile,
@@ -1101,9 +1103,15 @@
         ):
             value = locals()[key]
             kwargs[key] = str(value)
-        return defer.ensureDeferred(self.invite_by_email(
-            client, jid.JID(service) if service else None, node, id_ or NS_EVENT, **kwargs
-        ))
+        return defer.ensureDeferred(
+            self.invite_by_email(
+                client,
+                jid.JID(service) if service else None,
+                node,
+                id_ or NS_EVENT,
+                **kwargs,
+            )
+        )
 
     async def invite_by_email(self, client, service, node, id_=NS_EVENT, **kwargs):
         """High level method to create an email invitation to an event
@@ -1139,7 +1147,7 @@
         service: jid.JID,
         node: str,
         item_id: Optional[str],
-        item_elt: domish.Element
+        item_elt: domish.Element,
     ) -> None:
         event_elt = item_elt.event
         link_elt = event_elt.addElement("link")
@@ -1153,8 +1161,8 @@
             pass
         else:
             extra["name"] = name
-        if 'image' in event_data:
-            extra["thumb_url"] = event_data['image']
+        if "image" in event_data:
+            extra["thumb_url"] = event_data["image"]
         extra["element"] = event_elt