Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_exp_invitation_pubsub.py @ 4108:238e305f2306
plugin JID Search: JID search plugin, first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 06 Jul 2023 11:34:51 +0200 |
parents | 4b842c1fb686 |
children | 0d7bb4df2343 |
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, |
3462 | 60 async_=True |
61 ) | |
62 | |
63 def register( | |
64 self, | |
65 namespace: str, | |
66 handler | |
67 ) -> None: | |
68 self._ns_handler[namespace] = handler | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
69 self.host.plugins["INVITATION"].register_namespace(namespace, self.on_invitation) |
3462 | 70 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
71 def _send_pubsub_invitation( |
3462 | 72 self, invitee_jid_s, service_s, node, item_id=None, |
73 name=None, extra_s='', profile_key=C.PROF_KEY_NONE): | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
74 client = self.host.get_client(profile_key) |
3462 | 75 invitee_jid = jid.JID(invitee_jid_s) |
76 service = jid.JID(service_s) | |
77 extra = data_format.deserialise(extra_s) | |
78 return defer.ensureDeferred( | |
79 self.invite( | |
80 client, | |
81 invitee_jid, | |
82 service, | |
83 node, | |
84 item_id or None, | |
85 name=name or None, | |
86 extra=extra | |
87 ) | |
88 ) | |
89 | |
90 async def invite( | |
91 self, | |
92 client: SatXMPPEntity, | |
93 invitee_jid: jid.JID, | |
94 service: jid.JID, | |
95 node: str, | |
96 item_id: Optional[str] = None, | |
97 name: str = '', | |
98 extra: Optional[dict] = None, | |
99 ) -> None: | |
100 if extra is None: | |
101 extra = {} | |
102 else: | |
103 namespace = extra.get("namespace") | |
104 if namespace: | |
105 try: | |
106 handler = self._ns_handler[namespace] | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
107 preflight = handler.invite_preflight |
3462 | 108 except KeyError: |
109 pass | |
110 except AttributeError: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
111 log.debug(f"no invite_preflight method found for {namespace!r}") |
3462 | 112 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
113 await utils.as_deferred( |
3462 | 114 preflight, |
115 client, invitee_jid, service, node, item_id, name, extra | |
116 ) | |
117 if item_id is None: | |
118 item_id = extra.pop("default_item_id", None) | |
119 | |
120 # we authorize our invitee to see the nodes of interest | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
121 await self._p.set_node_affiliations(client, service, node, {invitee_jid: "member"}) |
3462 | 122 log.debug(f"affiliation set on {service}'s {node!r} node") |
123 | |
124 # now we send the invitation | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
125 self.host.plugins["INVITATION"].send_pubsub_invitation( |
3462 | 126 client, |
127 invitee_jid, | |
128 service, | |
129 node, | |
130 item_id, | |
131 name=name or None, | |
132 extra=extra | |
133 ) | |
134 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
135 async def on_invitation( |
3462 | 136 self, |
137 client: SatXMPPEntity, | |
138 namespace: str, | |
139 name: str, | |
140 extra: dict, | |
141 service: jid.JID, | |
142 node: str, | |
143 item_id: Optional[str], | |
144 item_elt: domish.Element | |
145 ) -> None: | |
146 if extra is None: | |
147 extra = {} | |
148 try: | |
149 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
|
150 preflight = handler.on_invitation_preflight |
3462 | 151 except KeyError: |
152 pass | |
153 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
|
154 log.debug(f"no on_invitation_preflight method found for {namespace!r}") |
3462 | 155 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
156 await utils.as_deferred( |
3462 | 157 preflight, |
158 client, namespace, name, extra, service, node, item_id, item_elt | |
159 ) | |
160 if item_id is None: | |
161 item_id = extra.pop("default_item_id", None) | |
162 creator = extra.pop("creator", False) | |
163 element = extra.pop("element", None) | |
164 if not name: | |
165 name = extra.pop("name", "") | |
166 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4027
diff
changeset
|
167 return await self.host.plugins['LIST_INTEREST'].register_pubsub( |
3462 | 168 client, namespace, service, node, item_id, creator, |
169 name, element, extra) |