annotate sat/plugins/plugin_comp_ap_gateway/ad_hoc.py @ 3916:40d47cc29ea4

plugin XEP-0300: type hints
author Goffi <goffi@goffi.org>
date Thu, 06 Oct 2022 15:19:08 +0200
parents 0aa7023dcd08
children 524856bd7b19
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
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.constants import Const as C
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.core_types import SatXMPPEntity
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core.i18n import _
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.core.log import getLogger
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
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 class APAdHocService:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 """Ad-Hoc commands for AP Gateway"""
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 def __init__(self, apg):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 self.host = apg.host
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 self.apg = apg
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self._c = self.host.plugins["XEP-0050"]
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 def init(self, client: SatXMPPEntity) -> None:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self._c.addAdHocCommand(
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 client,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.xmpp_jid_node_2_ap_actor,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 "Convert XMPP JID/Node to AP actor",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 node=NS_XMPP_JID_NODE_2_AP,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 allowed_magics=C.ENTITY_ALL,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 )
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 async def xmpp_jid_node_2_ap_actor(
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 client: SatXMPPEntity,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 command_elt: domish.Element,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 session_data: dict,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 action: str,
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 node: str
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 ):
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 try:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 command_form = data_form.Form.fromElement(x_elt)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 except StopIteration:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 command_form = None
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 if command_form is None or len(command_form.fields) == 0:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 # root request
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 status = self._c.STATUS.EXECUTING
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 form = data_form.Form(
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 "form", title="XMPP JID/node to AP actor conversion",
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 formNamespace=NS_XMPP_JID_NODE_2_AP
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 )
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 field = data_form.Field(
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 "text-single", "jid", required=True
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 )
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
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 field = data_form.Field(
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 "text-single", "node", required=False
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 form.addField(field)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 payload = form.toElement()
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 return payload, status, None, None
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 else:
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 xmpp_jid = jid.JID(command_form["jid"])
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 xmpp_node = command_form.get("node")
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 actor = await self.apg.getAPAccountFromJidAndNode(xmpp_jid, xmpp_node)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 note = (self._c.NOTE.INFO, actor)
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 status = self._c.STATUS.COMPLETED
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 payload = None
0aa7023dcd08 component AP gateway: events:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 return (payload, status, None, note)