fix(onebot): send merged forwards through send_msg

Exclusive node lists on send_msg, send_group_msg, and send_private_msg
now take the same forward path as send_forward_msg.

Fixes #415
This commit is contained in:
DrSmoothl committed 2026-08-26 23:13:03 +08:00
1 parent 5ba7c21c4b
commit 4457dd4b4a
4 files changed
+145 -6

No files matched your search

+43 -5
View File
@@ -79,6 +79,44 @@ function isExplicitEmptyTextPlaceholder(segment: MessageSegment): boolean {
&& segmentPayload(segment).text === '';
}
/**
* True when the OneBot payload is a forward: every effective segment is
* `node` (explicit empty-text placeholders ignored). `send_msg` routes
* this list through the forward upload path instead of parseMessage.
*/
export function isExclusiveForwardNodeMessage(
message: JsonValue,
autoEscape: boolean,
): boolean {
return exclusiveForwardNodeList(message, autoEscape) !== null;
}
/** Node segments from an exclusive forward payload, or null if it is not one. */
export function exclusiveForwardNodeList(
message: JsonValue,
autoEscape: boolean,
): JsonValue[] | null {
if (autoEscape && typeof message === 'string') return null;
if (typeof message === 'object' && message !== null && !Array.isArray(message)) {
const type = (message as { type?: unknown }).type;
return typeof type === 'string' && normalizeSegmentType(type) === 'node'
? [message]
: null;
}
if (!Array.isArray(message) || message.length === 0) return null;
const nodes: JsonValue[] = [];
for (const item of message) {
if (typeof item !== 'object' || item === null || Array.isArray(item)) return null;
const type = (item as { type?: unknown }).type;
if (typeof type !== 'string' || type.trim().length === 0) return null;
const segment = item as MessageSegment;
if (isExplicitEmptyTextPlaceholder(segment)) continue;
if (normalizeSegmentType(type) !== 'node') return null;
nodes.push(item);
}
return nodes.length > 0 ? nodes : null;
}
function outboundInputSegmentSummary(
message: JsonValue,
autoEscape: boolean,
@@ -222,13 +260,13 @@ export async function segmentToElement(type: string, data: Record<string, unknow
assertScalarSegmentData(normalizedType, data);
}
// 纯 OneBot 输入词:可执行的塌缩成 json/face/poke;没有合法发送语义的 node /
// anonymous 在这里明确拒绝。它们无收侧对应、无专属 wire 形态,故不进 codec 表。
// 真实元素(收发同名)走下方的 ELEMENT_CODECS。
// 纯 OneBot 输入词:可执行的塌缩成 json/face/poke。整条消息都是 node 时由
// send 路径交给 parseForwardNodes;混进普通消息的 node、以及 anonymous,
// 在这里明确拒绝。真实元素(收发同名)走下方的 ELEMENT_CODECS。
switch (normalizedType) {
case 'node': {
// Forward-node arrays are consumed by parseForwardNodes before reaching
// this function. A node mixed into a normal message is not sendable.
// Exclusive node arrays never reach this function. A node mixed into a
// normal message is not sendable.
throw new MessageElementValidationError(
'UNSENDABLE_TYPE',
'message segment "node" is only valid inside a forward node list',
@@ -15,6 +15,7 @@ import {
} from '../message-id';
import {
assertOutboundMessageInput,
exclusiveForwardNodeList,
MessageElementValidationError,
parseMessage,
} from '../message-parser';
@@ -802,6 +803,17 @@ export async function sendPrivateMessage(
autoEscape,
tempGroupId === undefined ? 'direct-private' : 'group-temp',
);
const privateForwardNodes = exclusiveForwardNodeList(message, autoEscape);
if (privateForwardNodes) {
if (tempGroupId !== undefined) {
throw new MessageElementValidationError(
'UNSENDABLE_TYPE',
'message segment "node" cannot be sent in a temp session',
'node',
);
}
return sendPrivateForwardMessage(ref, userId, privateForwardNodes, undefined, onSelfSent);
}
const elements = await parseMessage(message, autoEscape, {
resolveReplySequence: (replyMessageId) => {
return ref.messageStore.resolveReplySequence(false, userId, replyMessageId);
@@ -1035,6 +1047,11 @@ export async function sendGroupMessage(
autoEscape: boolean,
): Promise<MessageSendResult> {
assertOutboundMessageInput(message, autoEscape, 'group');
const groupForwardNodes = exclusiveForwardNodeList(message, autoEscape);
if (groupForwardNodes) {
const result = await sendGroupForwardMessage(ref, groupId, groupForwardNodes);
return { messageId: result.messageId };
}
const elements = await parseMessage(message, autoEscape, {
resolveReplySequence: (replyMessageId) => {
return ref.messageStore.resolveReplySequence(true, groupId, replyMessageId);