annotate libervia/backend/plugins/plugin_comp_ap_gateway/ad_hoc.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4b842c1fb686
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 twisted.words.protocols.jabber import jid
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from twisted.words.xish import domish
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from wokkel import data_form
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
23 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
24 from libervia.backend.core.core_types import SatXMPPEntity
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
25 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
26 from libervia.backend.core.log import getLogger
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 log = getLogger(__name__)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 NS_XMPP_JID_NODE_2_AP = "https://libervia.org/ap_gateway/xmpp_jid_node_2_ap_actor"
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
32
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 class APAdHocService:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 """Ad-Hoc commands for AP Gateway"""
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 def __init__(self, apg):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 self.host = apg.host
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self.apg = apg
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 self._c = self.host.plugins["XEP-0050"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def init(self, client: SatXMPPEntity) -> None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3904
diff changeset
42 self._c.add_ad_hoc_command(
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 client,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self.xmpp_jid_node_2_ap_actor,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "Convert XMPP JID/Node to AP actor",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 node=NS_XMPP_JID_NODE_2_AP,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 allowed_magics=C.ENTITY_ALL,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 )
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 async def xmpp_jid_node_2_ap_actor(
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 client: SatXMPPEntity,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 command_elt: domish.Element,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 session_data: dict,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 action: str,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
56 node: str,
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 ):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 try:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 command_form = data_form.Form.fromElement(x_elt)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 except StopIteration:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 command_form = None
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 if command_form is None or len(command_form.fields) == 0:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 # root request
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 status = self._c.STATUS.EXECUTING
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 form = data_form.Form(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
67 "form",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
68 title="XMPP JID/node to AP actor conversion",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
69 formNamespace=NS_XMPP_JID_NODE_2_AP,
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 )
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
71
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
72 field = data_form.Field("text-single", "jid", required=True)
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 form.addField(field)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
74
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
75 field = data_form.Field("text-single", "node", required=False)
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 form.addField(field)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 payload = form.toElement()
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 return payload, status, None, None
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 xmpp_jid = jid.JID(command_form["jid"])
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 xmpp_node = command_form.get("node")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3904
diff changeset
83 actor = await self.apg.get_ap_account_from_jid_and_node(xmpp_jid, xmpp_node)
3904
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 note = (self._c.NOTE.INFO, actor)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 status = self._c.STATUS.COMPLETED
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 payload = None
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 return (payload, status, None, note)