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

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 49019947cc76
children
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_comp_ap_gateway/events.py	Tue Jun 18 12:06:45 2024 +0200
+++ b/libervia/backend/plugins/plugin_comp_ap_gateway/events.py	Wed Jun 19 18:44:57 2024 +0200
@@ -45,11 +45,12 @@
     "addressCountry": "sc:addressCountry",
     "addressLocality": "sc:addressLocality",
     "addressRegion": "sc:addressRegion",
-    "anonymousParticipationEnabled": {"@id": "mz:anonymousParticipationEnabled",
-                                      "@type": "sc:Boolean"},
+    "anonymousParticipationEnabled": {
+        "@id": "mz:anonymousParticipationEnabled",
+        "@type": "sc:Boolean",
+    },
     "category": "sc:category",
-    "commentsEnabled": {"@id": "pt:commentsEnabled",
-                        "@type": "sc:Boolean"},
+    "commentsEnabled": {"@id": "pt:commentsEnabled", "@type": "sc:Boolean"},
     "discoverable": "toot:discoverable",
     "discussions": {"@id": "mz:discussions", "@type": "@id"},
     "events": {"@id": "mz:events", "@type": "@id"},
@@ -57,8 +58,7 @@
     "inLanguage": "sc:inLanguage",
     "isOnline": {"@id": "mz:isOnline", "@type": "sc:Boolean"},
     "joinMode": {"@id": "mz:joinMode", "@type": "mz:joinModeType"},
-    "joinModeType": {"@id": "mz:joinModeType",
-                     "@type": "rdfs:Class"},
+    "joinModeType": {"@id": "mz:joinModeType", "@type": "rdfs:Class"},
     "location": {"@id": "sc:location", "@type": "sc:Place"},
     "manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
     "maximumAttendeeCapacity": "sc:maximumAttendeeCapacity",
@@ -66,19 +66,21 @@
     "members": {"@id": "mz:members", "@type": "@id"},
     "mz": "https://joinmobilizon.org/ns#",
     "openness": {"@id": "mz:openness", "@type": "@id"},
-    "participantCount": {"@id": "mz:participantCount",
-                         "@type": "sc:Integer"},
-    "participationMessage": {"@id": "mz:participationMessage",
-                             "@type": "sc:Text"},
+    "participantCount": {"@id": "mz:participantCount", "@type": "sc:Integer"},
+    "participationMessage": {"@id": "mz:participationMessage", "@type": "sc:Text"},
     "postalCode": "sc:postalCode",
     "posts": {"@id": "mz:posts", "@type": "@id"},
     "propertyID": "sc:propertyID",
     "pt": "https://joinpeertube.org/ns#",
     "remainingAttendeeCapacity": "sc:remainingAttendeeCapacity",
-    "repliesModerationOption": {"@id": "mz:repliesModerationOption",
-                                "@type": "mz:repliesModerationOptionType"},
-    "repliesModerationOptionType": {"@id": "mz:repliesModerationOptionType",
-                                    "@type": "rdfs:Class"},
+    "repliesModerationOption": {
+        "@id": "mz:repliesModerationOption",
+        "@type": "mz:repliesModerationOptionType",
+    },
+    "repliesModerationOptionType": {
+        "@id": "mz:repliesModerationOptionType",
+        "@type": "rdfs:Class",
+    },
     "resources": {"@id": "mz:resources", "@type": "@id"},
     "sc": "http://schema.org#",
     "streetAddress": "sc:streetAddress",
@@ -86,7 +88,7 @@
     "todos": {"@id": "mz:todos", "@type": "@id"},
     "toot": "http://joinmastodon.org/ns#",
     "uuid": "sc:identifier",
-    "value": "sc:value"
+    "value": "sc:value",
 }
 
 
@@ -99,7 +101,7 @@
         self._events = self.host.plugins["XEP-0471"]
 
     async def event_data_2_ap_item(
-        self, event_data: dict, author_jid: jid.JID, is_new: bool=True
+        self, event_data: dict, author_jid: jid.JID, is_new: bool = True
     ) -> dict:
         """Convert event data to AP activity
 
@@ -114,8 +116,7 @@
         if not event_data.get("id"):
             event_data["id"] = shortuuid.uuid()
         ap_account = await self.apg.get_ap_account_from_jid_and_node(
-            author_jid,
-            self._events.namespace
+            author_jid, self._events.namespace
         )
         url_actor = self.apg.build_apurl(TYPE_ACTOR, ap_account)
         url_item = self.apg.build_apurl(TYPE_ITEM, ap_account, event_data["id"])
@@ -141,12 +142,14 @@
             pass
         else:
             media_type = mimetypes.guess_type(head_picture_url, False)[0] or "image/jpeg"
-            attachment.append({
-                "name": "Banner",
-                "type": "Document",
-                "mediaType": media_type,
-                "url": head_picture_url,
-            })
+            attachment.append(
+                {
+                    "name": "Banner",
+                    "type": "Document",
+                    "mediaType": media_type,
+                    "url": head_picture_url,
+                }
+            )
 
         descriptions = event_data.get("descriptions")
         if descriptions:
@@ -162,10 +165,12 @@
         if categories:
             tag = ap_object["tag"] = []
             for category in categories:
-                tag.append({
-                    "name": f"#{category['term']}",
-                    "type": "Hashtag",
-                })
+                tag.append(
+                    {
+                        "name": f"#{category['term']}",
+                        "type": "Hashtag",
+                    }
+                )
 
         locations = event_data.get("locations")
         if locations:
@@ -203,12 +208,14 @@
 
             website = extra.get("website")
             if website:
-                attachment.append({
-                    "href": website,
-                    "mediaType": "text/html",
-                    "name": "Website",
-                    "type": "Link"
-                })
+                attachment.append(
+                    {
+                        "href": website,
+                        "mediaType": "text/html",
+                        "name": "Website",
+                        "type": "Link",
+                    }
+                )
 
             accessibility = extra.get("accessibility")
             if accessibility:
@@ -224,11 +231,13 @@
                         log.error(f"unexpected wheelchair value: {wheelchair}")
                         ap_wc_value = None
                     if ap_wc_value is not None:
-                        attachment.append({
-                            "propertyID": "mz:accessibility:wheelchairAccessible",
-                            "type": "PropertyValue",
-                            "value": ap_wc_value
-                        })
+                        attachment.append(
+                            {
+                                "propertyID": "mz:accessibility:wheelchairAccessible",
+                                "type": "PropertyValue",
+                                "value": ap_wc_value,
+                            }
+                        )
 
         activity = self.apg.create_activity(
             "Create" if is_new else "Update", url_actor, ap_object, activity_id=url_item
@@ -236,11 +245,7 @@
         activity["@context"].append(AP_EVENTS_CONTEXT)
         return activity
 
-    async def ap_item_2_event_data(
-        self,
-        requestor_actor_id: str,
-        ap_item: dict
-    ) -> dict:
+    async def ap_item_2_event_data(self, requestor_actor_id: str, ap_item: dict) -> dict:
         """Convert AP activity or object to event data
 
         @param requestor_actor_id: ID of the actor doing the request.
@@ -251,7 +256,9 @@
         """
         is_activity = self.apg.is_activity(ap_item)
         if is_activity:
-            ap_object = await self.apg.ap_get_object(requestor_actor_id, ap_item, "object")
+            ap_object = await self.apg.ap_get_object(
+                requestor_actor_id, ap_item, "object"
+            )
             if not ap_object:
                 log.warning(f'No "object" found in AP item {ap_item!r}')
                 raise exceptions.DataError
@@ -311,10 +318,7 @@
         # description
         content = ap_object.get("content")
         if content:
-            event_data["descriptions"] = [{
-                "type": "xhtml",
-                "description": content
-            }]
+            event_data["descriptions"] = [{"type": "xhtml", "description": content}]
 
         # categories
         tags = ap_object.get("tag")
@@ -329,7 +333,7 @@
                         continue
                     categories.append({"term": term})
 
-        #location
+        # location
         ap_location = ap_object.get("location")
         if ap_location:
             location = {}
@@ -361,24 +365,24 @@
         # a simple "yes"/"no" form.
         rsvp_data = {"fields": []}
         event_data["rsvp"] = [rsvp_data]
-        rsvp_data["fields"].append({
-            "type": "list-single",
-            "name": "attending",
-            "label": "Attending",
-            "options": [
-                {"label": "yes", "value": "yes"},
-                {"label": "no", "value": "no"}
-            ],
-            "required": True
-        })
+        rsvp_data["fields"].append(
+            {
+                "type": "list-single",
+                "name": "attending",
+                "label": "Attending",
+                "options": [
+                    {"label": "yes", "value": "yes"},
+                    {"label": "no", "value": "no"},
+                ],
+                "required": True,
+            }
+        )
 
         # comments
 
         if ap_object.get("commentsEnabled"):
             __, comments_node = await self.apg.get_comments_nodes(
-                requestor_actor_id,
-                object_id,
-                None
+                requestor_actor_id, object_id, None
             )
             event_data["comments"] = {
                 "service": author_jid,
@@ -399,15 +403,10 @@
         return event_data
 
     async def ap_item_2_event_data_and_elt(
-        self,
-        requestor_actor_id: str,
-        ap_item: dict
+        self, requestor_actor_id: str, ap_item: dict
     ) -> tuple[dict, domish.Element]:
         """Convert AP item to parsed event data and corresponding item element"""
-        event_data = await self.ap_item_2_event_data(
-            requestor_actor_id,
-            ap_item
-        )
+        event_data = await self.ap_item_2_event_data(requestor_actor_id, ap_item)
         event_elt = self._events.event_data_2_event_elt(event_data)
         item_elt = domish.Element((None, "item"))
         item_elt["id"] = event_data["id"]
@@ -415,13 +414,10 @@
         return event_data, item_elt
 
     async def ap_item_2_event_elt(
-        self,
-        requestor_actor_id: str,
-        ap_item: dict
+        self, requestor_actor_id: str, ap_item: dict
     ) -> domish.Element:
         """Convert AP item to XMPP item element"""
         __, item_elt = await self.ap_item_2_event_data_and_elt(
-            requestor_actor_id,
-            ap_item
+            requestor_actor_id, ap_item
         )
         return item_elt