Mercurial > libervia-pubsub
annotate sat_pubsub/mam.py @ 489:fa0d2a4783fa default tip
install: use PEP-440 compatible version in `setup`:
setuptools from version v66 doesn't accept anymore non PEP-440 compatible version.
This is a Q&D fix, move to `pyproject.toml` with proper versioning as for Libervia
backend should be done sooner than later.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 12 Jan 2024 23:46:24 +0100 |
parents | c41f37f1b51c |
children |
rev | line source |
---|---|
414 | 1 #!/usr/bin/env python3 |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
2 #-*- coding: utf-8 -*- |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
3 |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
4 # Copyright (c) 2016 Jérôme Poisson |
311 | 5 # Copyright (c) 2015-2016 Adrien Cossa |
6 # | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 # | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 # | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
20 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
21 XMPP Message Archive Management protocol. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
22 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
23 This protocol is specified in |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
24 U{XEP-0313<http://xmpp.org/extensions/xep-0313.html>}. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
25 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
26 |
311 | 27 |
414 | 28 from zope.interface import implementer |
281
30895c49ebd2
fixes the imports from sat.tmp
souliane <souliane@mailoo.org>
parents:
280
diff
changeset
|
29 |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
30 from twisted.words.xish import domish |
322 | 31 from twisted.python import log |
431
5e8b8ef5c862
implentation of XEP-0346 (Form Discovery and Publishing):
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
32 from twisted.internet import defer |
322 | 33 from twisted.words.protocols.jabber import error |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
34 |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
35 from sat_pubsub import const |
322 | 36 from sat_pubsub import backend |
37 from wokkel import pubsub | |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
38 |
322 | 39 from wokkel import rsm |
40 from wokkel import mam | |
41 from wokkel import delay | |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
42 |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
43 |
414 | 44 @implementer(mam.IMAMResource) |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
45 class MAMResource(object): |
322 | 46 _errorMap = backend.PubSubResourceFromBackend._errorMap |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
47 |
322 | 48 def __init__(self, backend_): |
49 self.backend = backend_ | |
50 | |
51 def _mapErrors(self, failure): | |
52 # XXX: come from backend.PubsubResourceFromBackend | |
414 | 53 e = failure.trap(*list(self._errorMap.keys())) |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
54 |
322 | 55 condition, pubsubCondition, feature = self._errorMap[e] |
56 msg = failure.value.msg | |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
57 |
322 | 58 if pubsubCondition: |
59 exc = pubsub.PubSubError(condition, pubsubCondition, feature, msg) | |
60 else: | |
61 exc = error.StanzaError(condition, text=msg) | |
62 | |
63 raise exc | |
64 | |
325
8d939378f023
mam: removed requestor in favor of mam_request.sender and recipient + fixed bad use of requestor instead of recipient in getItemsData
Goffi <goffi@goffi.org>
parents:
322
diff
changeset
|
65 def onArchiveRequest(self, mam_request): |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
66 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
67 |
322 | 68 @param mam_request: The MAM archive request. |
69 @type mam_request: L{MAMQueryReques<wokkel.mam.MAMRequest>} | |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
70 |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
71 @return: A tuple with list of message data (id, element, data) and RSM element |
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
72 @rtype: C{tuple} |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
73 """ |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
74 # FIXME: bad result ordering |
322 | 75 try: |
76 pep = mam_request.delegated | |
77 except AttributeError: | |
78 pep = False | |
79 ext_data = {'pep': pep} | |
80 if mam_request.form: | |
414 | 81 ext_data['filters'] = list(mam_request.form.fields.values()) |
322 | 82 if mam_request.rsm is None: |
83 if const.VAL_RSM_MAX_DEFAULT != None: | |
84 log.msg("MAM request without RSM limited to {}".format(const.VAL_RSM_MAX_DEFAULT)) | |
85 ext_data['rsm'] = rsm.RSMRequest(const.VAL_RSM_MAX_DEFAULT) | |
86 else: | |
87 ext_data['rsm'] = mam_request.rsm | |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
88 |
375
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
89 if mam_request.orderBy: |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
90 ext_data['order_by'] = mam_request.orderBy |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
91 |
431
5e8b8ef5c862
implentation of XEP-0346 (Form Discovery and Publishing):
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
92 d = defer.ensureDeferred( |
5e8b8ef5c862
implentation of XEP-0346 (Form Discovery and Publishing):
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
93 self.backend.getItemsData(mam_request.node, mam_request.sender, |
398
845ed0f71dd6
mam: don't crash on missing <first> element in RSM on archive request
Goffi <goffi@goffi.org>
parents:
379
diff
changeset
|
94 mam_request.recipient, None, None, ext_data) |
431
5e8b8ef5c862
implentation of XEP-0346 (Form Discovery and Publishing):
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
95 ) |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
96 |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
97 def make_message(elt): |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
98 # XXX: http://xmpp.org/extensions/xep-0297.html#sect-idp629952 (rule 3) |
322 | 99 message = domish.Element((const.NS_CLIENT, "message")) |
100 event = message.addElement((pubsub.NS_PUBSUB_EVENT, "event")) | |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
101 items = event.addElement('items') |
322 | 102 items["node"] = mam_request.node |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
103 items.addChild(elt) |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
104 return message |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
105 |
487
7a23db86ee65
mam: fix message sending in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
469
diff
changeset
|
106 def cb(items_data_tuple): |
7a23db86ee65
mam: fix message sending in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
469
diff
changeset
|
107 items_data, rsm_response = items_data_tuple |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
108 msg_data = [] |
379
66fbf026ed49
mam: set "complete" attribute when suitable
Goffi <goffi@goffi.org>
parents:
375
diff
changeset
|
109 attributes = {} |
488
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
110 if rsm_response: |
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
111 rsm_elt = rsm_response.toElement() |
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
112 # XXX: we check if it is the last page using initial request data |
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
113 # and RSM element data. In this case, we must have the "complete" |
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
114 # attribute set to "true". |
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
115 page_max = rsm_response.index + mam_request.rsm.max |
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
116 if page_max >= rsm_response.count: |
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
117 # the maximum items which can be displayed is equal to or above the |
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
118 # total number of items, which means we are complete |
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
119 attributes['complete'] = "true" |
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
120 else: |
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
121 rsm_elt = None |
322 | 122 for item_data in items_data: |
488
c41f37f1b51c
mam: fix `complete` attribute setting in `onArchiveRequest`
Goffi <goffi@goffi.org>
parents:
487
diff
changeset
|
123 if item_data.item.name == 'item': |
398
845ed0f71dd6
mam: don't crash on missing <first> element in RSM on archive request
Goffi <goffi@goffi.org>
parents:
379
diff
changeset
|
124 msg_data.append([item_data.item['id'], make_message(item_data.item), |
845ed0f71dd6
mam: don't crash on missing <first> element in RSM on archive request
Goffi <goffi@goffi.org>
parents:
379
diff
changeset
|
125 item_data.created]) |
322 | 126 else: |
127 log.msg("WARNING: unknown element: {}".format(item_data.item.name)) | |
128 if pep: | |
129 # we need to send privileged message | |
130 # so me manage the sending ourself, and return | |
131 # an empty msg_data list to avoid double sending | |
132 for data in msg_data: | |
325
8d939378f023
mam: removed requestor in favor of mam_request.sender and recipient + fixed bad use of requestor instead of recipient in getItemsData
Goffi <goffi@goffi.org>
parents:
322
diff
changeset
|
133 self.forwardPEPMessage(mam_request, *data) |
322 | 134 msg_data = [] |
379
66fbf026ed49
mam: set "complete" attribute when suitable
Goffi <goffi@goffi.org>
parents:
375
diff
changeset
|
135 return (msg_data, rsm_elt, attributes) |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
136 |
322 | 137 d.addErrback(self._mapErrors) |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
138 d.addCallback(cb) |
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
139 return d |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
140 |
367
a772f7dac930
backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents:
335
diff
changeset
|
141 def forwardPEPMessage(self, mam_request, id_, elt, created): |
322 | 142 msg = domish.Element((None, 'message')) |
469
bcbd2399bfee
mam: `server_jid` must now be used from backend
Goffi <goffi@goffi.org>
parents:
449
diff
changeset
|
143 msg['from'] = self.backend.server_jid.full() |
325
8d939378f023
mam: removed requestor in favor of mam_request.sender and recipient + fixed bad use of requestor instead of recipient in getItemsData
Goffi <goffi@goffi.org>
parents:
322
diff
changeset
|
144 msg['to'] = mam_request.sender.full() |
322 | 145 result = msg.addElement((mam.NS_MAM, 'result')) |
146 if mam_request.query_id is not None: | |
147 result['queryid'] = mam_request.query_id | |
148 result['id'] = id_ | |
149 forward = result.addElement((const.NS_FORWARD, 'forwarded')) | |
367
a772f7dac930
backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents:
335
diff
changeset
|
150 forward.addChild(delay.Delay(created).toElement()) |
322 | 151 forward.addChild(elt) |
152 self.backend.privilege.sendMessage(msg) | |
153 | |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
154 def onPrefsGetRequest(self, requestor): |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
155 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
156 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
157 @param requestor: JID of the requestor. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
158 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
159 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
160 @return: The current settings. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
161 @rtype: L{wokkel.mam.MAMPrefs} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
162 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
163 # TODO: return the actual current settings |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
164 return mam.MAMPrefs() |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
165 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
166 def onPrefsSetRequest(self, prefs, requestor): |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
167 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
168 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
169 @param prefs: The new settings to set. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
170 @type prefs: L{wokkel.mam.MAMPrefs} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
171 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
172 @param requestor: JID of the requestor. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
173 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
174 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
175 @return: The settings that have actually been set. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
176 @rtype: L{wokkel.mam.MAMPrefs} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
177 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
178 # TODO: set the new settings and return them |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
179 return mam.MAMPrefs() |