annotate libervia/backend/plugins/plugin_comp_ap_gateway/events.py @ 4260:57ff857bf96e

doc (components/AP Gateway): document new `http_sign_get` configuration option.
author Goffi <goffi@goffi.org>
date Wed, 05 Jun 2024 22:34:43 +0200
parents 49019947cc76
children 0d7bb4df2343
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",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 "anonymousParticipationEnabled": {"@id": "mz:anonymousParticipationEnabled",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 "@type": "sc:Boolean"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "category": "sc:category",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 "commentsEnabled": {"@id": "pt:commentsEnabled",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "@type": "sc:Boolean"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 "discoverable": "toot:discoverable",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 "discussions": {"@id": "mz:discussions", "@type": "@id"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 "events": {"@id": "mz:events", "@type": "@id"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 "ical": "http://www.w3.org/2002/12/cal/ical#",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 "inLanguage": "sc:inLanguage",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 "isOnline": {"@id": "mz:isOnline", "@type": "sc:Boolean"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 "joinMode": {"@id": "mz:joinMode", "@type": "mz:joinModeType"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 "joinModeType": {"@id": "mz:joinModeType",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 "@type": "rdfs:Class"},
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"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 "participantCount": {"@id": "mz:participantCount",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 "@type": "sc:Integer"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 "participationMessage": {"@id": "mz:participationMessage",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 "@type": "sc:Text"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 "postalCode": "sc:postalCode",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 "posts": {"@id": "mz:posts", "@type": "@id"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 "propertyID": "sc:propertyID",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 "pt": "https://joinpeertube.org/ns#",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 "remainingAttendeeCapacity": "sc:remainingAttendeeCapacity",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 "repliesModerationOption": {"@id": "mz:repliesModerationOption",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 "@type": "mz:repliesModerationOptionType"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 "repliesModerationOptionType": {"@id": "mz:repliesModerationOptionType",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 "@type": "rdfs:Class"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 "resources": {"@id": "mz:resources", "@type": "@id"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 "sc": "http://schema.org#",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 "streetAddress": "sc:streetAddress",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 "timezone": {"@id": "mz:timezone", "@type": "sc:Text"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 "todos": {"@id": "mz:todos", "@type": "@id"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 "toot": "http://joinmastodon.org/ns#",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 "uuid": "sc:identifier",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 "value": "sc:value"
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 }
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
91
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 class APEvents:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 """XMPP Events <=> AP Events conversion"""
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def __init__(self, apg):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 self.host = apg.host
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 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
99 self._events = self.host.plugins["XEP-0471"]
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
100
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 async def event_data_2_ap_item(
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 self, event_data: dict, author_jid: jid.JID, is_new: bool=True
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 ) -> dict:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 """Convert event data to AP activity
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 @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
107 @param author_jid: jid of the publisher of the event
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 @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
109 cache).
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 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
111 be
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 @return: AP activity wrapping an Event object
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 """
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 if not event_data.get("id"):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 event_data["id"] = shortuuid.uuid()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4027
diff changeset
116 ap_account = await self.apg.get_ap_account_from_jid_and_node(
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 author_jid,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 self._events.namespace
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4027
diff changeset
120 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
121 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
122 ap_object = {
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 "actor": url_actor,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 "attributedTo": url_actor,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 "to": [NS_AP_PUBLIC],
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 "id": url_item,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 "type": TYPE_EVENT,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 "name": next(iter(event_data["name"].values())),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 "startTime": date_utils.date_fmt(event_data["start"], "iso"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 "endTime": date_utils.date_fmt(event_data["end"], "iso"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 "url": url_item,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 }
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 attachment = ap_object["attachment"] = []
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
135
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 # FIXME: we only handle URL head-picture for now
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 # TODO: handle jingle and use file metadata
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 try:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 head_picture_url = event_data["head-picture"]["sources"][0]["url"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 except (KeyError, IndexError, TypeError):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 pass
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 media_type = mimetypes.guess_type(head_picture_url, False)[0] or "image/jpeg"
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 attachment.append({
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 "name": "Banner",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 "type": "Document",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 "mediaType": media_type,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 "url": head_picture_url,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 })
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
150
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 descriptions = event_data.get("descriptions")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 if descriptions:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 for description in descriptions:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 content = description["description"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 if description["type"] == "xhtml":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 break
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 content = f"<p>{html.escape(content)}</p>" # type: ignore
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 ap_object["content"] = content
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
160
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 categories = event_data.get("categories")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 if categories:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 tag = ap_object["tag"] = []
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 for category in categories:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 tag.append({
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 "name": f"#{category['term']}",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 "type": "Hashtag",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 })
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
169
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 locations = event_data.get("locations")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 if locations:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 ap_loc = ap_object["location"] = {}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 # we only use the first found location
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 location = locations[0]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 for source, dest in (
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 ("description", "name"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 ("lat", "latitude"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 ("lon", "longitude"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 ):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 value = location.get(source)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 if value is not None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 ap_loc[dest] = value
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 for source, dest in (
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 ("country", "addressCountry"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 ("locality", "addressLocality"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 ("region", "addressRegion"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 ("postalcode", "postalCode"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 ("street", "streetAddress"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 ):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 value = location.get(source)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 if value is not None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 ap_loc.setdefault("address", {})[dest] = value
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
193
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 if event_data.get("comments"):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 ap_object["commentsEnabled"] = True
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
196
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 extra = event_data.get("extra")
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 extra:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 status = extra.get("status")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 if status:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 ap_object["ical:status"] = status.upper()
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 website = extra.get("website")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 if website:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 attachment.append({
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 "href": website,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 "mediaType": "text/html",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 "name": "Website",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 "type": "Link"
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 })
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
212
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 accessibility = extra.get("accessibility")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 if accessibility:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 wheelchair = accessibility.get("wheelchair")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 if wheelchair:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 if wheelchair == "full":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 ap_wc_value = "fully"
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 elif wheelchair == "partial":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 ap_wc_value = "partially"
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 elif wheelchair == "no":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 ap_wc_value = "no"
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 log.error(f"unexpected wheelchair value: {wheelchair}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 ap_wc_value = None
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 if ap_wc_value is not None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 attachment.append({
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 "propertyID": "mz:accessibility:wheelchairAccessible",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 "type": "PropertyValue",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 "value": ap_wc_value
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 })
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
232
4023
78b5f356900c component AP gateway: handle attachments
Goffi <goffi@goffi.org>
parents: 3904
diff changeset
233 activity = self.apg.create_activity(
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 "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
235 )
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 activity["@context"].append(AP_EVENTS_CONTEXT)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 return activity
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
238
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
239 async def ap_item_2_event_data(
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
240 self,
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
241 requestor_actor_id: str,
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
242 ap_item: dict
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
243 ) -> dict:
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 """Convert AP activity or object to event data
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
245
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
246 @param requestor_actor_id: ID of the actor doing the request.
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 @param ap_item: ActivityPub item to convert
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 Can be either an activity of an object
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 @return: AP Item's Object and event data
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 @raise exceptions.DataError: something is invalid in the AP item
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 """
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 is_activity = self.apg.is_activity(ap_item)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 if is_activity:
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
254 ap_object = await self.apg.ap_get_object(requestor_actor_id, ap_item, "object")
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 if not ap_object:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 log.warning(f'No "object" found in AP item {ap_item!r}')
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 raise exceptions.DataError
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 ap_object = ap_item
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
260
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 # id
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 if "_repeated" in ap_item:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 # if the event is repeated, we use the original one ID
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 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
265 parsed_uri = uri.parse_xmpp_uri(repeated_uri)
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 object_id = parsed_uri["item"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 object_id = ap_object.get("id")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 if not object_id:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 raise exceptions.DataError('"id" is missing in AP object')
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
271
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 if ap_item["type"] != TYPE_EVENT:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 raise exceptions.DataError("AP Object is not an event")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
274
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 # author
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
276 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
277
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
278 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
279 author_jid = self.apg.get_local_jid_from_account(account).full()
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
280
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 # name, start, end
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 event_data = {
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 "id": object_id,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 "name": {"": ap_object.get("name") or "unnamed"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 "start": date_utils.date_parse(ap_object["startTime"]),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 "end": date_utils.date_parse(ap_object["endTime"]),
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
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 # attachments/extra
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 event_data["extra"] = extra = {}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 attachments = ap_object.get("attachment") or []
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 for attachment in attachments:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 name = attachment.get("name")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 if name == "Banner":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 try:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 url = attachment["url"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 except KeyError:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 log.warning(f"invalid attachment: {attachment}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 continue
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 event_data["head-picture"] = {"sources": [{"url": url}]}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 elif name == "Website":
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["href"]
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 extra["website"] = url
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 log.debug(f"unmanaged attachment: {attachment}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
310
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
311 # description
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 content = ap_object.get("content")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 if content:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 event_data["descriptions"] = [{
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 "type": "xhtml",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 "description": content
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
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 # categories
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 tags = ap_object.get("tag")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 if tags:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 categories = event_data["categories"] = []
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 for tag in tags:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
324 if tag.get("type") == "Hashtag":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 try:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 term = tag["name"][1:]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
327 except KeyError:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 log.warning(f"invalid tag: {tag}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 continue
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 categories.append({"term": term})
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
331
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
332 #location
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 ap_location = ap_object.get("location")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
334 if ap_location:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 location = {}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 for source, dest in (
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
337 ("name", "description"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 ("latitude", "lat"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
339 ("longitude", "lon"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
340 ):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 value = ap_location.get(source)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
342 if value is not None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
343 location[dest] = value
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
344 address = ap_location.get("address")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 if address:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
346 for source, dest in (
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 ("addressCountry", "country"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 ("addressLocality", "locality"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 ("addressRegion", "region"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
350 ("postalCode", "postalcode"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
351 ("streetAddress", "street"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
352 ):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
353 value = address.get(source)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
354 if value is not None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
355 location[dest] = value
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 if location:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
357 event_data["locations"] = [location]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
358
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
359 # rsvp
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
360 # 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
361 # a simple "yes"/"no" form.
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
362 rsvp_data = {"fields": []}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
363 event_data["rsvp"] = [rsvp_data]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 rsvp_data["fields"].append({
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 "type": "list-single",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
366 "name": "attending",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 "label": "Attending",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
368 "options": [
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
369 {"label": "yes", "value": "yes"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
370 {"label": "no", "value": "no"}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
371 ],
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
372 "required": True
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
373 })
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
374
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 # comments
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
376
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
377 if ap_object.get("commentsEnabled"):
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
378 __, comments_node = await self.apg.get_comments_nodes(
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
379 requestor_actor_id,
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
380 object_id,
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
381 None
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
382 )
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 event_data["comments"] = {
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
384 "service": author_jid,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
385 "node": comments_node,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 }
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
387
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 # extra
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
389 # part of extra come from "attachment" above
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 status = ap_object.get("ical:status")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
392 if status is None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 pass
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
394 elif status in ("CONFIRMED", "CANCELLED", "TENTATIVE"):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
395 extra["status"] = status.lower()
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
396 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
397 log.warning(f"unknown event status: {status}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
398
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
399 return event_data
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
400
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
401 async def ap_item_2_event_data_and_elt(
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
402 self,
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
403 requestor_actor_id: str,
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
404 ap_item: dict
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
405 ) -> tuple[dict, domish.Element]:
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
406 """Convert AP item to parsed event data and corresponding item element"""
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
407 event_data = await self.ap_item_2_event_data(
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
408 requestor_actor_id,
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
409 ap_item
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
410 )
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
411 event_elt = self._events.event_data_2_event_elt(event_data)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 item_elt = domish.Element((None, "item"))
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
413 item_elt["id"] = event_data["id"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
414 item_elt.addChild(event_elt)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
415 return event_data, item_elt
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
416
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
417 async def ap_item_2_event_elt(
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
418 self,
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
419 requestor_actor_id: str,
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
420 ap_item: dict
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
421 ) -> domish.Element:
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
422 """Convert AP item to XMPP item element"""
4259
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
423 __, item_elt = await self.ap_item_2_event_data_and_elt(
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
424 requestor_actor_id,
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
425 ap_item
49019947cc76 component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
426 )
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
427 return item_elt