From 9b89401b36a10c8248ff6011232929b39301272f Mon Sep 17 00:00:00 2001 From: nicoco Date: Tue, 22 Nov 2022 10:23:52 +0100 Subject: [PATCH] XEP-0461: add get fallback body helper --- slixmpp/plugins/xep_0461/stanza.py | 10 ++++++++++ tests/test_stanza_xep_0461.py | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/slixmpp/plugins/xep_0461/stanza.py b/slixmpp/plugins/xep_0461/stanza.py index 37021c00..09a25f97 100644 --- a/slixmpp/plugins/xep_0461/stanza.py +++ b/slixmpp/plugins/xep_0461/stanza.py @@ -18,6 +18,16 @@ class FeatureFallBack(ElementBase): plugin_attrib = "feature_fallback" interfaces = {"for"} + def get_fallback_body(self): + # only works for a single fallback_body attrib + start = self["fallback_body"]["start"] + end = self["fallback_body"]["end"] + body = self.parent()["body"] + if start < end < len(body): + return body[start:end] + else: + return "" + def get_stripped_body(self): # only works for a single fallback_body attrib start = self["fallback_body"]["start"] diff --git a/tests/test_stanza_xep_0461.py b/tests/test_stanza_xep_0461.py index 3eb6a3ac..76f07c80 100644 --- a/tests/test_stanza_xep_0461.py +++ b/tests/test_stanza_xep_0461.py @@ -61,5 +61,15 @@ class TestReply(SlixTest): """ ) + def testGetFallBackBody(self): + body = "Anna wrote:\nHi, how are you?" + quoted = "> Anna wrote:\n> Hi, how are you?\n" + + msg = Message() + msg["body"] = "Great" + msg["feature_fallback"].add_quoted_fallback(body) + body2 = msg["feature_fallback"].get_fallback_body() + self.assertTrue(body2 == quoted, body2) + suite = unittest.TestLoader().loadTestsFromTestCase(TestReply)