Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_exp_pubsub_admin.py @ 4271:a5d27f69eedb
plugin misc list: Comments node creation can now be specified with `comments` field in `extra`.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 20 Jun 2024 14:36:09 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin to send pubsub requests with administrator privilege |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
20 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
21 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
22 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
|
23 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
24 from libervia.backend.tools.common import data_format |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.words.protocols.jabber import jid |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from wokkel import pubsub |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from wokkel import generic |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 log = getLogger(__name__) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 PLUGIN_INFO = { |
3028 | 32 C.PI_NAME: "Pubsub Administrator", |
33 C.PI_IMPORT_NAME: "PUBSUB_ADMIN", | |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 C.PI_TYPE: C.PLUG_TYPE_EXP, |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 C.PI_PROTOCOLS: [], |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 C.PI_DEPENDENCIES: [], |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 C.PI_RECOMMENDATIONS: [], |
3028 | 38 C.PI_MAIN: "PubsubAdmin", |
39 C.PI_HANDLER: "no", | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
40 C.PI_DESCRIPTION: _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
41 """\Implementation of Pubsub Administrator |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 This allows a pubsub administrator to overwrite completly items, including publisher. |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
43 Specially useful when importing a node.""" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
44 ), |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 } |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
3028 | 47 NS_PUBSUB_ADMIN = "https://salut-a-toi.org/spec/pubsub_admin:0" |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 class PubsubAdmin(object): |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 def __init__(self, host): |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 self.host = host |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
54 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
55 "ps_admin_items_send", |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 ".plugin", |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 in_sign="ssasss", |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 out_sign="as", |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 method=self._publish, |
3028 | 60 async_=True, |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 ) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
63 def _publish( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
64 self, service, nodeIdentifier, items, extra=None, profile_key=C.PROF_KEY_NONE |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
65 ): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
66 client = self.host.get_client(profile_key) |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 service = None if not service else jid.JID(service) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 extra = data_format.deserialise(extra) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
69 items = [generic.parseXml(i.encode("utf-8")) for i in items] |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
70 return self.publish(client, service, nodeIdentifier, items, extra) |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
72 def _send_cb(self, iq_result): |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 publish_elt = iq_result.admin.pubsub.publish |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 ids = [] |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
75 for item_elt in publish_elt.elements(pubsub.NS_PUBSUB, "item"): |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
76 ids.append(item_elt["id"]) |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 return ids |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 def publish(self, client, service, nodeIdentifier, items, extra=None): |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 for item in items: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
81 if item.name != "item" or item.uri != pubsub.NS_PUBSUB: |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 raise exceptions.DataError( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
83 "Invalid element, a pubsub item is expected: {xml}".format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
84 xml=item.toXml() |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
85 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
86 ) |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 iq_elt = client.IQ() |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
88 iq_elt["to"] = service.full() if service else client.jid.userhost() |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
89 admin_elt = iq_elt.addElement((NS_PUBSUB_ADMIN, "admin")) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
90 pubsub_elt = admin_elt.addElement((pubsub.NS_PUBSUB, "pubsub")) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
91 publish_elt = pubsub_elt.addElement("publish") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
92 publish_elt["node"] = nodeIdentifier |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 for item in items: |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 publish_elt.addChild(item) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 d = iq_elt.send() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
96 d.addCallback(self._send_cb) |
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 return d |