Merge branch 'xep0461-fallback-helper' into 'master'
XEP-0461 (replies) improvements See merge request poezio/slixmpp!224
This commit is contained in:
commit
5eb17e7633
3 changed files with 70 additions and 9 deletions
|
@ -113,4 +113,5 @@ __all__ = [
|
|||
'xep_0439', # Quick Response
|
||||
'xep_0441', # Message Archive Management Preferences
|
||||
'xep_0444', # Message Reactions
|
||||
'xep_0461', # Message Replies
|
||||
]
|
||||
|
|
|
@ -18,18 +18,33 @@ 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:
|
||||
return body[start:end+1]
|
||||
else:
|
||||
return ""
|
||||
|
||||
def get_stripped_body(self):
|
||||
# only works for a single fallback_body attrib
|
||||
start = self["fallback_body"]["start"]
|
||||
end = self["fallback_body"]["end"]
|
||||
body = self.parent()["body"]
|
||||
try:
|
||||
start = int(start)
|
||||
end = int(end)
|
||||
except ValueError:
|
||||
return body
|
||||
else:
|
||||
if start <= end < len(body):
|
||||
return body[:start] + body[end:]
|
||||
else:
|
||||
return body
|
||||
|
||||
def add_quoted_fallback(self, fallback: str):
|
||||
msg = self.parent()
|
||||
quoted = "\n".join("> " + x.strip() for x in fallback.split("\n")) + "\n"
|
||||
msg["body"] = quoted + msg["body"]
|
||||
msg["feature_fallback"]["for"] = NS
|
||||
msg["feature_fallback"]["fallback_body"]["start"] = 0
|
||||
msg["feature_fallback"]["fallback_body"]["end"] = len(quoted) - 1
|
||||
|
||||
|
||||
class FallBackBody(ElementBase):
|
||||
|
@ -40,6 +55,24 @@ class FallBackBody(ElementBase):
|
|||
plugin_attrib = "fallback_body"
|
||||
interfaces = {"start", "end"}
|
||||
|
||||
def set_start(self, v: int):
|
||||
self._set_attr("start", str(v))
|
||||
|
||||
def get_start(self):
|
||||
try:
|
||||
return int(self._get_attr("start"))
|
||||
except ValueError:
|
||||
return 0
|
||||
|
||||
def set_end(self, v: int):
|
||||
self._set_attr("end", str(v))
|
||||
|
||||
def get_end(self):
|
||||
try:
|
||||
return int(self._get_attr("end"))
|
||||
except ValueError:
|
||||
return 0
|
||||
|
||||
|
||||
def register_plugins():
|
||||
register_stanza_plugin(Message, Reply)
|
||||
|
|
|
@ -27,13 +27,13 @@ class TestReply(SlixTest):
|
|||
message = Message()
|
||||
message["body"] = "12345\nrealbody"
|
||||
message["feature_fallback"]["for"] = "NS"
|
||||
message["feature_fallback"]["fallback_body"]["start"] = "0"
|
||||
message["feature_fallback"]["fallback_body"]["end"] = "6"
|
||||
message["feature_fallback"]["fallback_body"]["start"] = 0
|
||||
message["feature_fallback"]["fallback_body"]["end"] = 6
|
||||
|
||||
self.check(
|
||||
message,
|
||||
"""
|
||||
<message xmlns="jabber:client">
|
||||
<message xmlns="jabber:client">M
|
||||
<body>12345\nrealbody</body>
|
||||
<fallback xmlns='urn:xmpp:feature-fallback:0' for='NS'>
|
||||
<body start="0" end="6" />
|
||||
|
@ -44,5 +44,32 @@ class TestReply(SlixTest):
|
|||
|
||||
assert message["feature_fallback"].get_stripped_body() == "realbody"
|
||||
|
||||
def testAddFallBackHelper(self):
|
||||
msg = Message()
|
||||
msg["body"] = "Great"
|
||||
msg["feature_fallback"].add_quoted_fallback("Anna wrote:\nHi, how are you?")
|
||||
# ugly dedent but the test does not pass without it
|
||||
self.check(
|
||||
msg,
|
||||
"""
|
||||
<message xmlns="jabber:client" type="normal">
|
||||
<body>> Anna wrote:\n> Hi, how are you?\nGreat</body>
|
||||
<fallback xmlns="urn:xmpp:feature-fallback:0" for="urn:xmpp:reply:0">
|
||||
<body start='0' end='32' />
|
||||
</fallback>
|
||||
</message>
|
||||
"""
|
||||
)
|
||||
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue