changeset 3062:93e8793a735a

plugin XEP-0060: fixed bad MAM unwrapping after Python 3 port
author Goffi <goffi@goffi.org>
date Sun, 20 Oct 2019 19:34:26 +0200
parents 948833e3b542
children 84bb63e1e4c4
files sat/plugins/plugin_xep_0060.py
diffstat 1 files changed, 22 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0060.py	Fri Oct 18 17:51:44 2019 +0200
+++ b/sat/plugins/plugin_xep_0060.py	Sun Oct 20 19:34:26 2019 +0200
@@ -17,23 +17,24 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+
+from collections import namedtuple
+import urllib.request, urllib.parse, urllib.error
+from functools import reduce
+
 from sat.core.i18n import _
 from sat.core.constants import Const as C
 from sat.core.log import getLogger
 
-log = getLogger(__name__)
 from sat.core import exceptions
 from sat.tools import sat_defer
 from sat.tools.common import data_format
 
-from twisted.words.protocols.jabber import jid, error
-from twisted.internet import reactor, defer
+from zope.interface import implementer
+
 from wokkel import disco
 from wokkel import data_form
 from wokkel import generic
-from zope.interface import implementer
-from collections import namedtuple
-import urllib.request, urllib.parse, urllib.error
 
 # XXX: sat_tmp.wokkel.pubsub is actually use instead of wokkel version
 #      mam and rsm come from sat_tmp.wokkel too
@@ -41,6 +42,11 @@
 from wokkel import rsm
 from wokkel import mam
 
+from twisted.words.protocols.jabber import jid, error
+from twisted.internet import reactor, defer
+
+
+log = getLogger(__name__)
 
 PLUGIN_INFO = {
     C.PI_NAME: "Publish-Subscribe",
@@ -537,14 +543,16 @@
 
     def _unwrapMAMMessage(self, message_elt):
         try:
-            item_elt = (
-                next(message_elt.elements(mam.NS_MAM, "result").next()
-                .elements(C.NS_FORWARD, "forwarded").next()
-                .elements(C.NS_CLIENT, "message").next()
-                .elements("http://jabber.org/protocol/pubsub#event", "event").next()
-                .elements("http://jabber.org/protocol/pubsub#event", "items").next()
-                .elements("http://jabber.org/protocol/pubsub#event", "item"))
-            )
+            item_elt = reduce(
+                lambda elt, ns_name: next(elt.elements(*ns_name)),
+                (message_elt,
+                 (mam.NS_MAM, "result"),
+                 (C.NS_FORWARD, "forwarded"),
+                 (C.NS_CLIENT, "message"),
+                 ("http://jabber.org/protocol/pubsub#event", "event"),
+                 ("http://jabber.org/protocol/pubsub#event", "items"),
+                 ("http://jabber.org/protocol/pubsub#event", "item"),
+                ))
         except StopIteration:
             raise exceptions.DataError("Can't find Item in MAM message element")
         return item_elt