comparison sat_pubsub/mam.py @ 282:7d54ff2eeaf2

actually retrieve the MAM messages
author souliane <souliane@mailoo.org>
date Thu, 15 Jan 2015 22:00:49 +0100
parents 30895c49ebd2
children a776544d84e5
comparison
equal deleted inserted replaced
281:30895c49ebd2 282:7d54ff2eeaf2
8 U{XEP-0313<http://xmpp.org/extensions/xep-0313.html>}. 8 U{XEP-0313<http://xmpp.org/extensions/xep-0313.html>}.
9 """ 9 """
10 10
11 from zope.interface import implements 11 from zope.interface import implements
12 12
13 from twisted.words.xish import domish
14
15 from sat_pubsub import const
16 from sat_pubsub.backend import PubSubResourceFromBackend
17 from wokkel.pubsub import NS_PUBSUB_EVENT
18
19 from dateutil import parser
20
13 # TODO: change this when RSM and MAM are in wokkel 21 # TODO: change this when RSM and MAM are in wokkel
14 from sat.tmp.wokkel.rsm import RSMResponse 22 from sat.tmp.wokkel.rsm import RSMRequest
15 from sat.tmp.wokkel.mam import MAMPrefs, IMAMResource 23 from sat.tmp.wokkel.mam import MAMPrefs, IMAMResource
24
25 NS_CLIENT = 'jabber:client'
16 26
17 27
18 class MAMResource(object): 28 class MAMResource(object):
19 29
20 implements(IMAMResource) 30 implements(IMAMResource)
21 31
22 def onArchiveRequest(self, mam, rsm, requestor): 32 def __init__(self, backend):
33 self.backend = backend
34
35 def onArchiveRequest(self, mam, requestor):
23 """ 36 """
24 37
25 @param mam: The MAM <query/> request. 38 @param mam: The MAM <query/> request.
26 @type mam: L{MAMQueryReques<wokkel.mam.MAMQueryRequest>} 39 @type mam: L{MAMQueryReques<wokkel.mam.MAMQueryRequest>}
27
28 @param rsm: The RSM request.
29 @type rsm: L{RSMRequest<wokkel.rsm.RSMRequest>}
30 40
31 @param requestor: JID of the requestor. 41 @param requestor: JID of the requestor.
32 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>} 42 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>}
33 43
34 @return: The RSM answer. 44 @return: The RSM answer.
35 @rtype: L{RSMResponse<wokkel.rsm.RSMResponse>} 45 @rtype: L{RSMResponse<wokkel.rsm.RSMResponse>}
36 """ 46 """
37 # TODO: send the archived messages 47 ext_data = {}
38 return RSMResponse() 48 if mam.form:
49 ext_data['filters'] = mam.form.fields.values()
50 if mam.rsm is None:
51 mam.rsm = RSMRequest(const.VAL_RSM_MAX_DEFAULT)
52 ext_data['rsm'] = mam.rsm
53
54 d = self.backend.getItems(mam.node, requestor, mam.rsm.max, None, ext_data)
55
56 def make_message(elt):
57 # XXX: http://xmpp.org/extensions/xep-0297.html#sect-idp629952 (rule 3)
58 message = domish.Element((NS_CLIENT, "message"))
59 event = message.addElement((NS_PUBSUB_EVENT, "event"))
60 items = event.addElement('items')
61 items["node"] = mam.node
62 items.addChild(elt)
63 return message
64
65 def cb(elts):
66 msg_data = []
67 rsm_elt = None
68 for elt in elts:
69 if elt.name == 'set':
70 rsm_elt = elt
71 elif elt.name == 'item':
72 date = parser.parse(''.join(elt.entry.published.children))
73 msg_data.append([elt['id'], make_message(elt), date])
74 return (msg_data, rsm_elt)
75
76 d.addErrback(PubSubResourceFromBackend._mapErrors)
77 return d.addCallback(cb)
39 78
40 def onPrefsGetRequest(self, requestor): 79 def onPrefsGetRequest(self, requestor):
41 """ 80 """
42 81
43 @param requestor: JID of the requestor. 82 @param requestor: JID of the requestor.