diff 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
line wrap: on
line diff
--- a/sat_pubsub/mam.py	Wed Jan 14 10:43:27 2015 +0100
+++ b/sat_pubsub/mam.py	Thu Jan 15 22:00:49 2015 +0100
@@ -10,32 +10,71 @@
 
 from zope.interface import implements
 
+from twisted.words.xish import domish
+
+from sat_pubsub import const
+from sat_pubsub.backend import PubSubResourceFromBackend
+from wokkel.pubsub import NS_PUBSUB_EVENT
+
+from dateutil import parser
+
 # TODO: change this when RSM and MAM are in wokkel
-from sat.tmp.wokkel.rsm import RSMResponse
+from sat.tmp.wokkel.rsm import RSMRequest
 from sat.tmp.wokkel.mam import MAMPrefs, IMAMResource
 
+NS_CLIENT = 'jabber:client'
+
 
 class MAMResource(object):
 
     implements(IMAMResource)
 
-    def onArchiveRequest(self, mam, rsm, requestor):
+    def __init__(self, backend):
+        self.backend = backend
+
+    def onArchiveRequest(self, mam, requestor):
         """
 
         @param mam: The MAM <query/> request.
         @type mam: L{MAMQueryReques<wokkel.mam.MAMQueryRequest>}
 
-        @param rsm: The RSM request.
-        @type rsm: L{RSMRequest<wokkel.rsm.RSMRequest>}
-
         @param requestor: JID of the requestor.
         @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>}
 
         @return: The RSM answer.
         @rtype: L{RSMResponse<wokkel.rsm.RSMResponse>}
         """
-        # TODO: send the archived messages
-        return RSMResponse()
+        ext_data = {}
+        if mam.form:
+            ext_data['filters'] = mam.form.fields.values()
+        if mam.rsm is None:
+            mam.rsm = RSMRequest(const.VAL_RSM_MAX_DEFAULT)
+        ext_data['rsm'] = mam.rsm
+
+        d = self.backend.getItems(mam.node, requestor, mam.rsm.max, None, ext_data)
+
+        def make_message(elt):
+            # XXX: http://xmpp.org/extensions/xep-0297.html#sect-idp629952 (rule 3)
+            message = domish.Element((NS_CLIENT, "message"))
+            event = message.addElement((NS_PUBSUB_EVENT, "event"))
+            items = event.addElement('items')
+            items["node"] = mam.node
+            items.addChild(elt)
+            return message
+
+        def cb(elts):
+            msg_data = []
+            rsm_elt = None
+            for elt in elts:
+                if elt.name == 'set':
+                    rsm_elt = elt
+                elif elt.name == 'item':
+                    date = parser.parse(''.join(elt.entry.published.children))
+                    msg_data.append([elt['id'], make_message(elt), date])
+            return (msg_data, rsm_elt)
+
+        d.addErrback(PubSubResourceFromBackend._mapErrors)
+        return d.addCallback(cb)
 
     def onPrefsGetRequest(self, requestor):
         """