annotate libervia/backend/plugins/plugin_comp_ap_gateway/events.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_comp_ap_gateway/events.py@524856bd7b19
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",
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]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 @param author_jid: jid of the published of the event
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
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 async def ap_item_2_event_data(self, ap_item: dict) -> dict:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 """Convert AP activity or object to event data
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
241
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 @param ap_item: ActivityPub item to convert
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 Can be either an activity of an object
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 @return: AP Item's Object and event data
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 @raise exceptions.DataError: something is invalid in the AP item
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 """
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 is_activity = self.apg.is_activity(ap_item)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 if is_activity:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4027
diff changeset
249 ap_object = await self.apg.ap_get_object(ap_item, "object")
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 if not ap_object:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 log.warning(f'No "object" found in AP item {ap_item!r}')
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 raise exceptions.DataError
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 ap_object = ap_item
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
255
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 # id
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 if "_repeated" in ap_item:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 # if the event is repeated, we use the original one ID
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 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
260 parsed_uri = uri.parse_xmpp_uri(repeated_uri)
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 object_id = parsed_uri["item"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 object_id = ap_object.get("id")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 if not object_id:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 raise exceptions.DataError('"id" is missing in AP object')
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
266
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 if ap_item["type"] != TYPE_EVENT:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 raise exceptions.DataError("AP Object is not an event")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
269
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 # author
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4027
diff changeset
271 actor = await self.apg.ap_get_sender_actor(ap_object)
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
272
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4027
diff changeset
273 account = await self.apg.get_ap_account_from_id(actor)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4027
diff changeset
274 author_jid = self.apg.get_local_jid_from_account(account).full()
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
275
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 # name, start, end
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 event_data = {
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 "id": object_id,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 "name": {"": ap_object.get("name") or "unnamed"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 "start": date_utils.date_parse(ap_object["startTime"]),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 "end": date_utils.date_parse(ap_object["endTime"]),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 }
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
283
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 # attachments/extra
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 event_data["extra"] = extra = {}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 attachments = ap_object.get("attachment") or []
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 for attachment in attachments:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 name = attachment.get("name")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 if name == "Banner":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 try:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 url = attachment["url"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 except KeyError:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 log.warning(f"invalid attachment: {attachment}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 continue
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 event_data["head-picture"] = {"sources": [{"url": url}]}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 elif name == "Website":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 try:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 url = attachment["href"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 except KeyError:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 log.warning(f"invalid attachment: {attachment}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 continue
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 extra["website"] = url
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
304 log.debug(f"unmanaged attachment: {attachment}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
305
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
306 # description
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 content = ap_object.get("content")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 if content:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 event_data["descriptions"] = [{
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
310 "type": "xhtml",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
311 "description": content
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 }]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
313
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 # categories
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 tags = ap_object.get("tag")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 if tags:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 categories = event_data["categories"] = []
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 for tag in tags:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 if tag.get("type") == "Hashtag":
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 try:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 term = tag["name"][1:]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 except KeyError:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 log.warning(f"invalid tag: {tag}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
324 continue
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 categories.append({"term": term})
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
326
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
327 #location
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 ap_location = ap_object.get("location")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 if ap_location:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 location = {}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 for source, dest in (
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
332 ("name", "description"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 ("latitude", "lat"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
334 ("longitude", "lon"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 ):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 value = ap_location.get(source)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
337 if value is not None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 location[dest] = value
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
339 address = ap_location.get("address")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
340 if address:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 for source, dest in (
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
342 ("addressCountry", "country"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
343 ("addressLocality", "locality"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
344 ("addressRegion", "region"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 ("postalCode", "postalcode"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
346 ("streetAddress", "street"),
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 ):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 value = address.get(source)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 if value is not None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
350 location[dest] = value
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
351 if location:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
352 event_data["locations"] = [location]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
353
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
354 # rsvp
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
355 # 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
356 # a simple "yes"/"no" form.
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
357 rsvp_data = {"fields": []}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 event_data["rsvp"] = [rsvp_data]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
359 rsvp_data["fields"].append({
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
360 "type": "list-single",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
361 "name": "attending",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
362 "label": "Attending",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
363 "options": [
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 {"label": "yes", "value": "yes"},
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 {"label": "no", "value": "no"}
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
366 ],
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 "required": True
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
368 })
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
369
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
370 # comments
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 if ap_object.get("commentsEnabled"):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4027
diff changeset
373 __, comments_node = await self.apg.get_comments_nodes(object_id, None)
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
374 event_data["comments"] = {
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 "service": author_jid,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
376 "node": comments_node,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
377 }
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
378
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
379 # extra
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
380 # part of extra come from "attachment" above
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
381
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
382 status = ap_object.get("ical:status")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 if status is None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
384 pass
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
385 elif status in ("CONFIRMED", "CANCELLED", "TENTATIVE"):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 extra["status"] = status.lower()
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
387 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 log.warning(f"unknown event status: {status}")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
389
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
390 return event_data
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 async def ap_item_2_event_data_and_elt(
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 self,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
394 ap_item: dict
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
395 ) -> Tuple[dict, domish.Element]:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
396 """Convert AP item to parsed event data and corresponding item element"""
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
397 event_data = await self.ap_item_2_event_data(ap_item)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
398 event_elt = self._events.event_data_2_event_elt(event_data)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
399 item_elt = domish.Element((None, "item"))
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
400 item_elt["id"] = event_data["id"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
401 item_elt.addChild(event_elt)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
402 return event_data, item_elt
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
403
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
404 async def ap_item_2_event_elt(self, ap_item: dict) -> domish.Element:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
405 """Convert AP item to XMPP item element"""
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
406 __, item_elt = await self.ap_item_2_event_data_and_elt(ap_item)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
407 return item_elt