enhancement: Refactor reply handling in XEP-0461; add fallback support and improve message construction

This commit is contained in:
jinyu 2024-11-22 19:44:48 +08:00 committed by mathieui
parent c495eb73fc
commit 5051c60262
3 changed files with 57 additions and 12 deletions

View File

@ -1,3 +1,5 @@
from typing import Optional
from slixmpp.plugins import BasePlugin
from slixmpp.types import JidStr
from slixmpp.xmlstream import StanzaBase
@ -36,13 +38,34 @@ class XEP_0461(BasePlugin):
def _handle_reply_to_message(self, msg: StanzaBase):
self.xmpp.event("message_reply", msg)
def send_reply(self, reply_to: JidStr, reply_id: str, **msg_kwargs):
def make_reply(self, reply_to: JidStr, reply_id: str,
fallback: Optional[str] = None,
quoted_nick: Optional[str] = None, **msg_kwargs):
"""Create a replies message stanza
:param reply_to: Full JID of the quoted author
:param reply_id: ID of the message to reply to
:param fallback: Body of the quoted message
:param quoted_nick: nickname of the quoted participant
:param msg_kwargs: Additional message parameters
"""
msg = self.xmpp.make_message(**msg_kwargs)
msg["reply"]["to"] = reply_to
msg["reply"]["id"] = reply_id
if fallback:
msg["reply"].add_quoted_fallback(fallback, quoted_nick)
return msg
def send_reply(self, reply_to: JidStr, reply_id: str,
fallback: Optional[str] = None,
quoted_nick: Optional[str] = None, **msg_kwargs):
"""
:param reply_to: Full JID of the quoted author
:param reply_id: ID of the message to reply to
:param fallback: Body of the quoted message
:param quoted_nick: nickname of the quoted participant
"""
msg = self.xmpp.make_message(**msg_kwargs)
msg["reply"]["to"] = reply_to
msg["reply"]["id"] = reply_id
msg = self.make_reply(reply_to, reply_id, fallback, quoted_nick, **msg_kwargs)
msg.send()

View File

@ -30,11 +30,11 @@ class Reply(ElementBase):
if nickname:
quoted = "> " + nickname + ":\n" + quoted
msg["body"] = quoted + msg["body"]
fallback = Fallback()
fallback["for"] = NS
fallback["body"]["start"] = 0
fallback["body"]["end"] = len(quoted)
msg.append(fallback)
fallback_elem = Fallback()
fallback_elem["for"] = NS
fallback_elem["body"]["start"] = 0
fallback_elem["body"]["end"] = len(quoted)
msg.append(fallback_elem)
def get_fallback_body(self) -> str:
msg = self.parent()
@ -51,6 +51,23 @@ class Reply(ElementBase):
else:
return ""
def strip_fallback_content(self) -> str:
msg = self.parent()
for fallback in msg["fallbacks"]:
if fallback["for"] == NS:
break
else:
return msg["body"]
start = fallback["body"]["start"]
end = fallback["body"]["end"]
body = msg["body"]
if 0 <= start < end <= len(body):
return body[:start] + body[end:]
else:
return body
def register_plugins():
register_stanza_plugin(Message, Reply)

View File

@ -8,12 +8,14 @@ class TestReply(SlixTest):
self.stream_start(plugins=["xep_0461"])
def testFallBackBody(self):
async def on_reply(msg):
def on_reply(msg):
start = msg["fallback"]["body"]["start"]
end = msg["fallback"]["body"]["end"]
self.xmpp["xep_0461"].send_reply(
reply_to=msg.get_from(),
reply_id=msg.get_id(),
fallback=msg["reply"].strip_fallback_content(),
quoted_nick="res",
mto="test@test.com",
mbody=f"{start} to {end}",
)
@ -26,7 +28,7 @@ class TestReply(SlixTest):
<reply xmlns="urn:xmpp:reply:0" id="some-id" />
<body>&gt; quoted\nsome-body</body>
<fallback xmlns='urn:xmpp:fallback:0' for='urn:xmpp:reply:0'>
<body start="0" end="8" />
<body start="0" end="9" />
</fallback>
</message>
"""
@ -34,8 +36,11 @@ class TestReply(SlixTest):
self.send(
"""
<message xmlns="jabber:client" to="test@test.com" type="normal">
<body>&gt; res:\n&gt; some-body\n0 to 9</body>
<reply xmlns="urn:xmpp:reply:0" id="other-id" to="from@from.com/res" />
<body>0 to 8</body>
<fallback xmlns='urn:xmpp:fallback:0' for='urn:xmpp:reply:0'>
<body start="0" end="19" />
</fallback>
</message>
"""
)