changeset 2702:6555e9835ff8

plugin XEP-0060: use new parseExtra methods from MAM and RSM
author Goffi <goffi@goffi.org>
date Sat, 01 Dec 2018 10:34:42 +0100
parents 2ea2369ae7de
children 3ba53b1cd1e6
files sat/plugins/plugin_xep_0060.py
diffstat 1 files changed, 19 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0060.py	Sat Dec 01 10:33:43 2018 +0100
+++ b/sat/plugins/plugin_xep_0060.py	Sat Dec 01 10:34:42 2018 +0100
@@ -32,8 +32,6 @@
 from zope.interface import implements
 from collections import namedtuple
 import urllib
-import datetime
-from dateutil import tz
 
 # XXX: sat_tmp.wokkel.pubsub is actually use instead of wokkel version
 #      mam and rsm come from sat_tmp.wokkel too
@@ -43,19 +41,18 @@
 
 
 PLUGIN_INFO = {
-    C.PI_NAME: "Publish-Subscribe",
-    C.PI_IMPORT_NAME: "XEP-0060",
-    C.PI_TYPE: "XEP",
-    C.PI_PROTOCOLS: ["XEP-0060"],
+    C.PI_NAME: u"Publish-Subscribe",
+    C.PI_IMPORT_NAME: u"XEP-0060",
+    C.PI_TYPE: u"XEP",
+    C.PI_PROTOCOLS: [u"XEP-0060"],
     C.PI_DEPENDENCIES: [],
-    C.PI_RECOMMENDATIONS: ["XEP-0313"],
-    C.PI_MAIN: "XEP_0060",
-    C.PI_HANDLER: "yes",
-    C.PI_DESCRIPTION: _("""Implementation of PubSub Protocol"""),
+    C.PI_RECOMMENDATIONS: [u"XEP-0059", u"XEP-0313"],
+    C.PI_MAIN: u"XEP_0060",
+    C.PI_HANDLER: u"yes",
+    C.PI_DESCRIPTION: _(u"""Implementation of PubSub Protocol"""),
 }
 
 UNSPECIFIED = "unspecified error"
-MAM_FILTER = "mam_filter_"
 
 
 Extra = namedtuple("Extra", ("rsm_request", "extra"))
@@ -85,6 +82,7 @@
     def __init__(self, host):
         log.info(_(u"PubSub plugin initialization"))
         self.host = host
+        self._rsm = host.plugins.get("XEP-0059")
         self._mam = host.plugins.get("XEP-0313")
         self._node_cb = {}  # dictionnary of callbacks for node (key: node, value: list of callbacks)
         self.rt_sessions = sat_defer.RTDeferredSessions()
@@ -324,47 +322,20 @@
             extra = {}
         else:
             # rsm
-            rsm_args = {}
-            for arg in ("max", "after", "before", "index"):
-                try:
-                    argname = "max_" if arg == "max" else arg
-                    rsm_args[argname] = extra.pop("rsm_{}".format(arg))
-                except KeyError:
-                    continue
-
-            if rsm_args:
-                rsm_request = rsm.RSMRequest(**rsm_args)
+            if self._rsm is None:
+                rsm_request = None
             else:
-                rsm_request = None
+                rsm_request = self._rsm.parseExtra(extra)
 
             # mam
-            mam_args = {}
-            for arg in ("start", "end"):
-                try:
-                    mam_args[arg] = datetime.datetime.fromtimestamp(
-                        int(extra.pop("{}{}".format(MAM_FILTER, arg))), tz.tzutc()
-                    )
-                except (TypeError, ValueError):
-                    log.warning(u"Bad value for {} filter".format(arg))
-                except KeyError:
-                    continue
+            if self._mam is None:
+                mam_request = None
+            else:
+                mam_request = self._mam.parseExtra(extra, with_rsm=False)
 
-            try:
-                mam_args["with_jid"] = jid.JID(extra.pop("{}jid".format(MAM_FILTER)))
-            except (jid.InvalidFormat):
-                log.warning(u"Bad value for jid filter")
-            except KeyError:
-                pass
-
-            for name, value in extra.iteritems():
-                if name.startswith(MAM_FILTER):
-                    var = name[len(MAM_FILTER) :]
-                    extra_fields = mam_args.setdefault("extra_fields", [])
-                    extra_fields.append(data_form.Field(var=var, value=value))
-
-            if mam_args:
-                assert "mam" not in extra
-                extra["mam"] = mam.MAMRequest(mam.buildForm(**mam_args))
+            if mam_request is not None:
+                assert u"mam" not in extra
+                extra[u"mam"] = mam_request
         return Extra(rsm_request, extra)
 
     def addManagedNode(self, node, **kwargs):