annotate libervia/backend/plugins/plugin_comp_ap_gateway/events.py @ 4279:6276242736c3 default tip @

doc (components): Documentation for the new `Conferences` component: fix 445
author Goffi <goffi@goffi.org>
date Fri, 05 Jul 2024 17:18:37 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia ActivityPub Gateway
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import Tuple
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import mimetypes
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import html
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
23
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import shortuuid
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.xish import domish
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import jid
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
28 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
29 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
30 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
31 from libervia.backend.tools.common import date_utils, uri
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from .constants import NS_AP_PUBLIC, TYPE_ACTOR, TYPE_EVENT, TYPE_ITEM
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 log = getLogger(__name__)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 # direct copy of what Mobilizon uses
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 AP_EVENTS_CONTEXT = {
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 "@language": "und",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 "Hashtag": "as:Hashtag",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 "PostalAddress": "sc:PostalAddress",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 "PropertyValue": "sc:PropertyValue",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 "address": {"@id": "sc:address", "@type": "sc:PostalAddress"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "addressCountry": "sc:addressCountry",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 "addressLocality": "sc:addressLocality",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "addressRegion": "sc:addressRegion",
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
48 "anonymousParticipationEnabled": {
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
49 "@id": "mz:anonymousParticipationEnabled",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
50 "@type": "sc:Boolean",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
51 },
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "category": "sc:category",
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
53 "commentsEnabled": {"@id": "pt:commentsEnabled", "@type": "sc:Boolean"},
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 "discoverable": "toot:discoverable",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 "discussions": {"@id": "mz:discussions", "@type": "@id"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 "events": {"@id": "mz:events", "@type": "@id"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 "ical": "http://www.w3.org/2002/12/cal/ical#",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 "inLanguage": "sc:inLanguage",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 "isOnline": {"@id": "mz:isOnline", "@type": "sc:Boolean"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 "joinMode": {"@id": "mz:joinMode", "@type": "mz:joinModeType"},
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
61 "joinModeType": {"@id": "mz:joinModeType", "@type": "rdfs:Class"},
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 "location": {"@id": "sc:location", "@type": "sc:Place"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 "manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 "maximumAttendeeCapacity": "sc:maximumAttendeeCapacity",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 "memberCount": {"@id": "mz:memberCount", "@type": "sc:Integer"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 "members": {"@id": "mz:members", "@type": "@id"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 "mz": "https://joinmobilizon.org/ns#",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 "openness": {"@id": "mz:openness", "@type": "@id"},
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
69 "participantCount": {"@id": "mz:participantCount", "@type": "sc:Integer"},
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
70 "participationMessage": {"@id": "mz:participationMessage", "@type": "sc:Text"},
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 "postalCode": "sc:postalCode",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 "posts": {"@id": "mz:posts", "@type": "@id"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 "propertyID": "sc:propertyID",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 "pt": "https://joinpeertube.org/ns#",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 "remainingAttendeeCapacity": "sc:remainingAttendeeCapacity",
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
76 "repliesModerationOption": {
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
77 "@id": "mz:repliesModerationOption",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
78 "@type": "mz:repliesModerationOptionType",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
79 },
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
80 "repliesModerationOptionType": {
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
81 "@id": "mz:repliesModerationOptionType",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
82 "@type": "rdfs:Class",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
83 },
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 "resources": {"@id": "mz:resources", "@type": "@id"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 "sc": "http://schema.org#",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 "streetAddress": "sc:streetAddress",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 "timezone": {"@id": "mz:timezone", "@type": "sc:Text"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 "todos": {"@id": "mz:todos", "@type": "@id"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 "toot": "http://joinmastodon.org/ns#",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 "uuid": "sc:identifier",
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
91 "value": "sc:value",
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 }
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 class APEvents:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 """XMPP Events <=> AP Events conversion"""
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 def __init__(self, apg):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 self.host = apg.host
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 self.apg = apg
4027
26c3e1bc7fb7 plugin XEP-0471: renamed "events" plugin to XEP-0471 now that there is a XEP
Goffi <goffi@goffi.org>
parents: 4023
diff changeset
101 self._events = self.host.plugins["XEP-0471"]
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
102
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 async def event_data_2_ap_item(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
104 self, event_data: dict, author_jid: jid.JID, is_new: bool = True
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 ) -> dict:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 """Convert event data to AP activity
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 @param event_data: event data as used in [plugin_exp_events]
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
109 @param author_jid: jid of the publisher of the event
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 @param is_new: if True, the item is a new one (no instance has been found in
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 cache).
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 If True, a "Create" activity will be generated, otherwise an "Update" one will
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 be
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 @return: AP activity wrapping an Event object
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 """
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 if not event_data.get("id"):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 event_data["id"] = shortuuid.uuid()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4027
diff changeset
118 ap_account = await self.apg.get_ap_account_from_jid_and_node(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
119 author_jid, self._events.namespace
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4027
diff changeset
121 url_actor = self.apg.build_apurl(TYPE_ACTOR, ap_account)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4027
diff changeset
122 url_item = self.apg.build_apurl(TYPE_ITEM, ap_account, event_data["id"])
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 ap_object = {
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 "actor": url_actor,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 "attributedTo": url_actor,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 "to": [NS_AP_PUBLIC],
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 "id": url_item,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 "type": TYPE_EVENT,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 "name": next(iter(event_data["name"].values())),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 "startTime": date_utils.date_fmt(event_data["start"], "iso"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 "endTime": date_utils.date_fmt(event_data["end"], "iso"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 "url": url_item,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 }
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 attachment = ap_object["attachment"] = []
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
136
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 # FIXME: we only handle URL head-picture for now
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 # TODO: handle jingle and use file metadata
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 try:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 head_picture_url = event_data["head-picture"]["sources"][0]["url"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 except (KeyError, IndexError, TypeError):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 pass
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 media_type = mimetypes.guess_type(head_picture_url, False)[0] or "image/jpeg"
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
145 attachment.append(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
146 {
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
147 "name": "Banner",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
148 "type": "Document",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
149 "mediaType": media_type,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
150 "url": head_picture_url,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
151 }
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
152 )
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
153
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 descriptions = event_data.get("descriptions")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 if descriptions:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 for description in descriptions:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 content = description["description"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 if description["type"] == "xhtml":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 break
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 content = f"<p>{html.escape(content)}</p>" # type: ignore
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 ap_object["content"] = content
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
163
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 categories = event_data.get("categories")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 if categories:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 tag = ap_object["tag"] = []
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 for category in categories:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
168 tag.append(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
169 {
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
170 "name": f"#{category['term']}",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
171 "type": "Hashtag",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
172 }
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
173 )
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
174
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 locations = event_data.get("locations")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 if locations:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 ap_loc = ap_object["location"] = {}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 # we only use the first found location
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 location = locations[0]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 for source, dest in (
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 ("description", "name"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 ("lat", "latitude"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 ("lon", "longitude"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 ):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 value = location.get(source)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 if value is not None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 ap_loc[dest] = value
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 for source, dest in (
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 ("country", "addressCountry"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 ("locality", "addressLocality"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 ("region", "addressRegion"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 ("postalcode", "postalCode"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 ("street", "streetAddress"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 ):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 value = location.get(source)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 if value is not None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 ap_loc.setdefault("address", {})[dest] = value
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
198
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 if event_data.get("comments"):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 ap_object["commentsEnabled"] = True
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
201
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 extra = event_data.get("extra")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
203
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 if extra:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 status = extra.get("status")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 if status:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 ap_object["ical:status"] = status.upper()
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
208
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 website = extra.get("website")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 if website:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
211 attachment.append(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
212 {
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
213 "href": website,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
214 "mediaType": "text/html",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
215 "name": "Website",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
216 "type": "Link",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
217 }
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
218 )
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
219
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 accessibility = extra.get("accessibility")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 if accessibility:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 wheelchair = accessibility.get("wheelchair")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 if wheelchair:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 if wheelchair == "full":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 ap_wc_value = "fully"
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 elif wheelchair == "partial":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 ap_wc_value = "partially"
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 elif wheelchair == "no":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 ap_wc_value = "no"
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 log.error(f"unexpected wheelchair value: {wheelchair}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 ap_wc_value = None
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 if ap_wc_value is not None:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
234 attachment.append(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
235 {
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
236 "propertyID": "mz:accessibility:wheelchairAccessible",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
237 "type": "PropertyValue",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
238 "value": ap_wc_value,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
239 }
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
240 )
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
241
4023
78b5f356900c component AP gateway: handle attachments
Goffi <goffi@goffi.org>
parents: 3904
diff changeset
242 activity = self.apg.create_activity(
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 "Create" if is_new else "Update", url_actor, ap_object, activity_id=url_item
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 )
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 activity["@context"].append(AP_EVENTS_CONTEXT)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 return activity
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
247
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
248 async def ap_item_2_event_data(self, requestor_actor_id: str, ap_item: dict) -> dict:
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 """Convert AP activity or object to event data
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
250
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
251 @param requestor_actor_id: ID of the actor doing the request.
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 @param ap_item: ActivityPub item to convert
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 Can be either an activity of an object
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 @return: AP Item's Object and event data
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 @raise exceptions.DataError: something is invalid in the AP item
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 """
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 is_activity = self.apg.is_activity(ap_item)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 if is_activity:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
259 ap_object = await self.apg.ap_get_object(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
260 requestor_actor_id, ap_item, "object"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
261 )
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 if not ap_object:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 log.warning(f'No "object" found in AP item {ap_item!r}')
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 raise exceptions.DataError
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 ap_object = ap_item
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
267
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 # id
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 if "_repeated" in ap_item:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 # if the event is repeated, we use the original one ID
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 repeated_uri = ap_item["_repeated"]["uri"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4027
diff changeset
272 parsed_uri = uri.parse_xmpp_uri(repeated_uri)
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 object_id = parsed_uri["item"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 object_id = ap_object.get("id")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 if not object_id:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 raise exceptions.DataError('"id" is missing in AP object')
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
278
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 if ap_item["type"] != TYPE_EVENT:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 raise exceptions.DataError("AP Object is not an event")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
281
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 # author
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
283 actor = await self.apg.ap_get_sender_actor(requestor_actor_id, ap_object)
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
284
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
285 account = await self.apg.get_ap_account_from_id(requestor_actor_id, actor)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4027
diff changeset
286 author_jid = self.apg.get_local_jid_from_account(account).full()
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
287
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 # name, start, end
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 event_data = {
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 "id": object_id,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 "name": {"": ap_object.get("name") or "unnamed"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 "start": date_utils.date_parse(ap_object["startTime"]),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 "end": date_utils.date_parse(ap_object["endTime"]),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 }
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
295
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 # attachments/extra
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 event_data["extra"] = extra = {}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 attachments = ap_object.get("attachment") or []
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 for attachment in attachments:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 name = attachment.get("name")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 if name == "Banner":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 try:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 url = attachment["url"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
304 except KeyError:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
305 log.warning(f"invalid attachment: {attachment}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
306 continue
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 event_data["head-picture"] = {"sources": [{"url": url}]}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 elif name == "Website":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 try:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
310 url = attachment["href"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
311 except KeyError:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 log.warning(f"invalid attachment: {attachment}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 continue
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 extra["website"] = url
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 log.debug(f"unmanaged attachment: {attachment}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
317
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 # description
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 content = ap_object.get("content")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 if content:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
321 event_data["descriptions"] = [{"type": "xhtml", "description": content}]
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
322
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 # categories
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
324 tags = ap_object.get("tag")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 if tags:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 categories = event_data["categories"] = []
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
327 for tag in tags:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 if tag.get("type") == "Hashtag":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 try:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 term = tag["name"][1:]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 except KeyError:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
332 log.warning(f"invalid tag: {tag}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 continue
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
334 categories.append({"term": term})
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
335
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
336 # location
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
337 ap_location = ap_object.get("location")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 if ap_location:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
339 location = {}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
340 for source, dest in (
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 ("name", "description"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
342 ("latitude", "lat"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
343 ("longitude", "lon"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
344 ):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 value = ap_location.get(source)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
346 if value is not None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 location[dest] = value
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 address = ap_location.get("address")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 if address:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
350 for source, dest in (
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
351 ("addressCountry", "country"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
352 ("addressLocality", "locality"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
353 ("addressRegion", "region"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
354 ("postalCode", "postalcode"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
355 ("streetAddress", "street"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 ):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
357 value = address.get(source)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 if value is not None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
359 location[dest] = value
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
360 if location:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
361 event_data["locations"] = [location]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
362
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
363 # rsvp
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 # So far Mobilizon seems to only handle participate/don't participate, thus we use
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 # a simple "yes"/"no" form.
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
366 rsvp_data = {"fields": []}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 event_data["rsvp"] = [rsvp_data]
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
368 rsvp_data["fields"].append(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
369 {
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
370 "type": "list-single",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
371 "name": "attending",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
372 "label": "Attending",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
373 "options": [
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
374 {"label": "yes", "value": "yes"},
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
375 {"label": "no", "value": "no"},
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
376 ],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
377 "required": True,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
378 }
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
379 )
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
380
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
381 # comments
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
382
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 if ap_object.get("commentsEnabled"):
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
384 __, comments_node = await self.apg.get_comments_nodes(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
385 requestor_actor_id, object_id, None
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
386 )
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
387 event_data["comments"] = {
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 "service": author_jid,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
389 "node": comments_node,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
390 }
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
391
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
392 # extra
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 # part of extra come from "attachment" above
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
394
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
395 status = ap_object.get("ical:status")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
396 if status is None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
397 pass
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
398 elif status in ("CONFIRMED", "CANCELLED", "TENTATIVE"):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
399 extra["status"] = status.lower()
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
400 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
401 log.warning(f"unknown event status: {status}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
402
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
403 return event_data
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
404
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
405 async def ap_item_2_event_data_and_elt(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
406 self, requestor_actor_id: str, ap_item: dict
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
407 ) -> tuple[dict, domish.Element]:
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
408 """Convert AP item to parsed event data and corresponding item element"""
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
409 event_data = await self.ap_item_2_event_data(requestor_actor_id, ap_item)
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
410 event_elt = self._events.event_data_2_event_elt(event_data)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
411 item_elt = domish.Element((None, "item"))
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 item_elt["id"] = event_data["id"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
413 item_elt.addChild(event_elt)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
414 return event_data, item_elt
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
415
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
416 async def ap_item_2_event_elt(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
417 self, requestor_actor_id: str, ap_item: dict
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
418 ) -> domish.Element:
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
419 """Convert AP item to XMPP item element"""
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
420 __, item_elt = await self.ap_item_2_event_data_and_elt(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4259
diff changeset
421 requestor_actor_id, ap_item
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
422 )
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
423 return item_elt