Mercurial > libervia-backend
annotate sat/plugins/plugin_exp_pubsub_admin.py @ 2846:58ea675d0f05
quick frontend (app): added AUTO_RESYNC attribute in QuickApp, when set (default) the resync method is called on all widgets automatically:
if AUTO_RESYNC is False, frontend must call itself the resync method.
This is useful to improve performance, resynchronising all widgets at once may be resource intensive, and it may be more efficient to resync a widget only when it is visible.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 10 Mar 2019 18:02:42 +0100 |
parents | 838f53730ce4 |
children | ab2696e34d29 |
rev | line source |
---|---|
2776
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
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 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
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 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.i18n import _ |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core import exceptions |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.constants import Const as C |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.core.log import getLogger |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from sat.tools.common import data_format |
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 = { |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 C.PI_NAME: u"Pubsub Administrator", |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 C.PI_IMPORT_NAME: u"PUBSUB_ADMIN", |
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: [], |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 C.PI_MAIN: u"PubsubAdmin", |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 C.PI_HANDLER: u"no", |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 C.PI_DESCRIPTION: _(u"""\Implementation of Pubsub Administrator |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 This allows a pubsub administrator to overwrite completly items, including publisher. |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 Specially useful when importing a node."""), |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 } |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 NS_PUBSUB_ADMIN = u"https://salut-a-toi.org/spec/pubsub_admin:0" |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 class PubsubAdmin(object): |
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 def __init__(self, host): |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 self.host = host |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 host.bridge.addMethod( |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 "psAdminItemsSend", |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 ".plugin", |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 in_sign="ssasss", |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 out_sign="as", |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 method=self._publish, |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 async=True, |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 ) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 def _publish(self, service, nodeIdentifier, items, extra=None, |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 profile_key=C.PROF_KEY_NONE): |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 client = self.host.getClient(profile_key) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 service = None if not service else jid.JID(service) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 extra = data_format.deserialise(extra) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 items = [generic.parseXml(i.encode('utf-8')) for i in items] |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 return self.publish( |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 client, service, nodeIdentifier, items, extra |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 ) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 def _sendCb(self, iq_result): |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 publish_elt = iq_result.admin.pubsub.publish |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 ids = [] |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 for item_elt in publish_elt.elements(pubsub.NS_PUBSUB, u'item'): |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 ids.append(item_elt[u'id']) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 return ids |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 def publish(self, client, service, nodeIdentifier, items, extra=None): |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 for item in items: |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 if item.name != u'item' or item.uri != pubsub.NS_PUBSUB: |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 raise exceptions.DataError( |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 u'Invalid element, a pubsub item is expected: {xml}'.format( |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 xml=item.toXml())) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 iq_elt = client.IQ() |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 iq_elt['to'] = service.full() if service else client.jid.userhost() |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 admin_elt = iq_elt.addElement((NS_PUBSUB_ADMIN, u'admin')) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 pubsub_elt = admin_elt.addElement((pubsub.NS_PUBSUB, u'pubsub')) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 publish_elt = pubsub_elt.addElement('publish') |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 publish_elt[u'node'] = nodeIdentifier |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 for item in items: |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 publish_elt.addChild(item) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 d = iq_elt.send() |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 d.addCallback(self._sendCb) |
838f53730ce4
plugin pubsub admin: pubsub administrator first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 return d |