Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1009,14 +1009,19 @@ async def _parse_from_qqofficial(
quoted_message_str = QQOfficialPlatformAdapter._parse_face_message(
str(quoted_content or "").strip()
)
# Content alone cannot distinguish attachment metadata from quoted prose.
if quoted_message_str:
quoted_chain.append(Plain(quoted_message_str))
if isinstance(quoted_attachments, list):
await QQOfficialPlatformAdapter._append_attachments(
quoted_chain,
quoted_attachments,
)
if quoted_message_id or quoted_element_message_id or quoted_chain:
# QQ tags a passive reply with message_type 103 and stuffs the previous
# message into msg_elements even when the user quoted nothing, so a
# message id is the only thing that marks a real quote. Building a Reply
# out of the echo makes downstream plugins verify the wrong message.
if quoted_message_id or quoted_element_message_id:
msg.append(
Reply(
id=str(quoted_message_id or quoted_element_message_id or ""),
Expand Down
119 changes: 117 additions & 2 deletions tests/test_qqofficial_group_message_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,113 @@ async def test_webhook_send_by_session_use_markdown_config_false_sends_content()
assert "msg_type" not in kwargs


@pytest.mark.asyncio
async def test_passive_reply_echo_builds_no_reply_component():
"""A `message_type=103` echo with no message id is not a user quote.

QQ tags a passive reply with 103 and puts the previous message in
`msg_elements`, so an ordinary `@bot` after a reply arrives looking like a
quote. Downstream plugins then verify the wrong message.
"""
_, message = _dispatch_group_message(
_make_group_payload(
content="is the earth flat?",
message_type=103,
message_reference=None,
msg_elements=[
{
"content": "the previous bot answer",
"attachments": [],
}
],
)
)

abm = await QQOfficialPlatformAdapter._parse_from_qqofficial(
message,
MessageType.GROUP_MESSAGE,
)

assert not any(isinstance(component, Reply) for component in abm.message)
assert [
component.text for component in abm.message if isinstance(component, Plain)
] == ["is the earth flat?"]


@pytest.mark.asyncio
async def test_quoted_element_id_still_builds_reply_without_reference():
"""A quoted element carrying its own id is a real quote even without `message_reference`."""
_, message = _dispatch_group_message(
_make_group_payload(
content="answer",
message_type=103,
message_reference=None,
msg_elements=[
{
"id": "element-7",
"content": "quoted text",
"attachments": [],
}
],
)
)

abm = await QQOfficialPlatformAdapter._parse_from_qqofficial(
message,
MessageType.GROUP_MESSAGE,
)

reply = abm.message[0]
assert isinstance(reply, Reply)
assert reply.id == "element-7"
assert reply.message_str == "quoted text"


@pytest.mark.parametrize(
"quoted_content",
[
"[附件1] 类型:图片 URL:https://multimedia.example.com/chart.png",
(
"=== 消息 1 ===\n"
"[消息类型] 引用消息\n"
"[附件1] 类型:图片 文件名:chart.png 尺寸:2117x287 大小:57.3KB "
"URL:https://multimedia.example.com/chart.png\n"
"\n"
"=== 消息 2 ===\n"
"[消息内容] the old message text"
),
],
)
@pytest.mark.asyncio
async def test_quoted_attachment_like_text_stays_literal(quoted_content: str):
"""Text without structured attachment provenance must not become an image."""
_, message = _dispatch_group_message(
_make_group_payload(
content="answer",
message_type=103,
message_reference={"message_id": "quoted-1"},
msg_elements=[{"content": quoted_content, "attachments": []}],
)
)

abm = await QQOfficialPlatformAdapter._parse_from_qqofficial(
message,
MessageType.GROUP_MESSAGE,
)

reply = abm.message[0]
assert isinstance(reply, Reply)
assert reply.id == "quoted-1"
assert reply.message_str == quoted_content
assert not any(isinstance(component, Image) for component in reply.chain)
assert (
"".join(
component.text for component in reply.chain if isinstance(component, Plain)
)
== quoted_content
)


@pytest.mark.parametrize("platform", ["qq_official", "qq_official_webhook"])
@pytest.mark.asyncio
async def test_group_message_without_tag_keeps_plain_text(platform):
Expand All @@ -1126,7 +1233,11 @@ async def test_group_message_without_tag_keeps_plain_text(platform):
assert isinstance(abm.message[0], Plain)
assert abm.message[0].text == "plain group text"
event = QQOfficialMessageEvent(
abm.message_str, abm, SimpleNamespace(name=platform, id=platform), "group-1", None
abm.message_str,
abm,
SimpleNamespace(name=platform, id=platform),
"group-1",
None,
)
assert follow_up_text(event) == ("plain group text", None)

Expand All @@ -1149,6 +1260,10 @@ async def test_guild_channel_does_not_generate_placeholder_mention():
)
assert abm.message == [Plain("channel text")]
event = QQOfficialMessageEvent(
abm.message_str, abm, SimpleNamespace(name="qq_official", id="qq"), "channel-1", None
abm.message_str,
abm,
SimpleNamespace(name="qq_official", id="qq"),
"channel-1",
None,
)
assert follow_up_text(event) == ("channel text", None)
Loading