Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_exp_invitation_pubsub.py @ 4298:060d695ae98e
plugin XEP-0106, tools (common/email): type hints.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 06 Sep 2024 17:44:08 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3462 | 1 #!/usr/bin/env python3 |
2 | |
3 # SàT plugin to send invitations for Pubsub | |
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) | |
5 | |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
19 from typing import Optional | |
20 from twisted.internet import defer | |
21 from twisted.words.protocols.jabber import jid | |
22 from twisted.words.xish import domish | |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
23 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
24 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
|
25 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
26 from libervia.backend.core.xmpp import SatXMPPEntity |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
27 from libervia.backend.tools import utils |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
28 from libervia.backend.tools.common import data_format |
3462 | 29 |
30 log = getLogger(__name__) | |
31 | |
32 | |
33 PLUGIN_INFO = { | |
34 C.PI_NAME: "Pubsub Invitation", | |
35 C.PI_IMPORT_NAME: "PUBSUB_INVITATION", | |
36 C.PI_TYPE: "EXP", | |
37 C.PI_PROTOCOLS: [], | |
38 C.PI_DEPENDENCIES: ["XEP-0060", "INVITATION"], | |
39 C.PI_RECOMMENDATIONS: [], | |
40 C.PI_MAIN: "PubsubInvitation", | |
41 C.PI_HANDLER: "no", | |
42 C.PI_DESCRIPTION: _("Invitations for pubsub based features"), | |
43 } | |
44 | |
45 | |
46 class PubsubInvitation: | |
47 | |
48 def __init__(self, host): | |
49 log.info(_("Pubsub Invitation plugin initialization")) | |
50 self.host = host | |
51 self._p = host.plugins["XEP-0060"] | |
52 # namespace to handler map | |
53 self._ns_handler = {} | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
54 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
55 "ps_invite", |
3462 | 56 ".plugin", |
57 in_sign="sssssss", | |
58 out_sign="", | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
59 method=self._send_pubsub_invitation, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
60 async_=True, |
3462 | 61 ) |
62 | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
63 def register(self, namespace: str, handler) -> None: |
3462 | 64 self._ns_handler[namespace] = handler |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
65 self.host.plugins["INVITATION"].register_namespace(namespace, self.on_invitation) |
3462 | 66 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
67 def _send_pubsub_invitation( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
68 self, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
69 invitee_jid_s, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
70 service_s, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
71 node, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
72 item_id=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
73 name=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
74 extra_s="", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
75 profile_key=C.PROF_KEY_NONE, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
76 ): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
77 client = self.host.get_client(profile_key) |
3462 | 78 invitee_jid = jid.JID(invitee_jid_s) |
79 service = jid.JID(service_s) | |
80 extra = data_format.deserialise(extra_s) | |
81 return defer.ensureDeferred( | |
82 self.invite( | |
83 client, | |
84 invitee_jid, | |
85 service, | |
86 node, | |
87 item_id or None, | |
88 name=name or None, | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
89 extra=extra, |
3462 | 90 ) |
91 ) | |
92 | |
93 async def invite( | |
94 self, | |
95 client: SatXMPPEntity, | |
96 invitee_jid: jid.JID, | |
97 service: jid.JID, | |
98 node: str, | |
99 item_id: Optional[str] = None, | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
100 name: str = "", |
3462 | 101 extra: Optional[dict] = None, |
102 ) -> None: | |
103 if extra is None: | |
104 extra = {} | |
105 else: | |
106 namespace = extra.get("namespace") | |
107 if namespace: | |
108 try: | |
109 handler = self._ns_handler[namespace] | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
110 preflight = handler.invite_preflight |
3462 | 111 except KeyError: |
112 pass | |
113 except AttributeError: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
114 log.debug(f"no invite_preflight method found for {namespace!r}") |
3462 | 115 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
116 await utils.as_deferred( |
3462 | 117 preflight, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
118 client, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
119 invitee_jid, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
120 service, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
121 node, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
122 item_id, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
123 name, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
124 extra, |
3462 | 125 ) |
126 if item_id is None: | |
127 item_id = extra.pop("default_item_id", None) | |
128 | |
129 # we authorize our invitee to see the nodes of interest | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
130 await self._p.set_node_affiliations( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
131 client, service, node, {invitee_jid: "member"} |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
132 ) |
3462 | 133 log.debug(f"affiliation set on {service}'s {node!r} node") |
134 | |
135 # now we send the invitation | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
136 self.host.plugins["INVITATION"].send_pubsub_invitation( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
137 client, invitee_jid, service, node, item_id, name=name or None, extra=extra |
3462 | 138 ) |
139 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
140 async def on_invitation( |
3462 | 141 self, |
142 client: SatXMPPEntity, | |
143 namespace: str, | |
144 name: str, | |
145 extra: dict, | |
146 service: jid.JID, | |
147 node: str, | |
148 item_id: Optional[str], | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
149 item_elt: domish.Element, |
3462 | 150 ) -> None: |
151 if extra is None: | |
152 extra = {} | |
153 try: | |
154 handler = self._ns_handler[namespace] | |
4027
26c3e1bc7fb7
plugin XEP-0471: renamed "events" plugin to XEP-0471 now that there is a XEP
Goffi <goffi@goffi.org>
parents:
3584
diff
changeset
|
155 preflight = handler.on_invitation_preflight |
3462 | 156 except KeyError: |
157 pass | |
158 except AttributeError: | |
4027
26c3e1bc7fb7
plugin XEP-0471: renamed "events" plugin to XEP-0471 now that there is a XEP
Goffi <goffi@goffi.org>
parents:
3584
diff
changeset
|
159 log.debug(f"no on_invitation_preflight method found for {namespace!r}") |
3462 | 160 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
161 await utils.as_deferred( |
3462 | 162 preflight, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
163 client, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
164 namespace, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
165 name, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
166 extra, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
167 service, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
168 node, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
169 item_id, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
170 item_elt, |
3462 | 171 ) |
172 if item_id is None: | |
173 item_id = extra.pop("default_item_id", None) | |
174 creator = extra.pop("creator", False) | |
175 element = extra.pop("element", None) | |
176 if not name: | |
177 name = extra.pop("name", "") | |
178 | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
179 return await self.host.plugins["LIST_INTEREST"].register_pubsub( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
180 client, namespace, service, node, item_id, creator, name, element, extra |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
181 ) |