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:
4 files changed
+145
-6
No files matched your search
@@ -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);
|
||||
|
||||
@@ -1120,6 +1120,44 @@ describe('buildApiContext send and forward dispatch', () => {
|
||||
expect(dispatchEvent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('send_private_msg with an exclusive node list uploads a forward (#415)', async () => {
|
||||
const { api, dispatchEvent, ref } = makeRef();
|
||||
const nodes = [{
|
||||
type: 'node',
|
||||
data: {
|
||||
user_id: SELF_ID,
|
||||
nickname: 'SnowLuma',
|
||||
content: [{ type: 'text', data: { text: 'forwarded' } }],
|
||||
},
|
||||
}];
|
||||
|
||||
const result = await api.sendPrivateMessage(PEER_ID, nodes, false);
|
||||
|
||||
expect(result.messageId).toBe(hashMessageIdInt32(77, PEER_ID, PRIVATE_NT_MESSAGE_EVENT));
|
||||
expect(ref.bridge.apis.forward.upload).toHaveBeenCalledOnce();
|
||||
expect(ref.bridge.apis.message.sendPrivate).toHaveBeenCalledOnce();
|
||||
expect(dispatchEvent).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('send_group_msg with an exclusive node list uploads a forward (#415)', async () => {
|
||||
const { api, dispatchEvent, ref } = makeRef();
|
||||
const nodes = [{
|
||||
type: 'node',
|
||||
data: {
|
||||
user_id: SELF_ID,
|
||||
nickname: 'SnowLuma',
|
||||
content: [{ type: 'text', data: { text: 'pack' } }],
|
||||
},
|
||||
}];
|
||||
|
||||
const result = await api.sendGroupMessage(GROUP_ID, nodes, false);
|
||||
|
||||
expect(result.messageId).toBe(hashMessageIdInt32(77, GROUP_ID, GROUP_MESSAGE_EVENT));
|
||||
expect(ref.bridge.apis.forward.upload).toHaveBeenCalledOnce();
|
||||
expect(ref.bridge.apis.message.sendGroup).toHaveBeenCalledOnce();
|
||||
expect(dispatchEvent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('dispatches a private forward send', async () => {
|
||||
const { api, dispatchEvent, ref } = makeRef();
|
||||
|
||||
|
||||
@@ -3,7 +3,11 @@ import os from 'os';
|
||||
import path from 'path';
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import { MessageElementValidationError, parseMessage } from '../src/message-parser';
|
||||
import {
|
||||
exclusiveForwardNodeList,
|
||||
MessageElementValidationError,
|
||||
parseMessage,
|
||||
} from '../src/message-parser';
|
||||
import { buildSendElems } from '@snowluma/protocol/element-builder';
|
||||
import type { MentionExtraSend } from '@snowluma/proto-defs/action';
|
||||
import type { MarketFacePbReserve } from '@snowluma/proto-defs/element';
|
||||
@@ -91,6 +95,20 @@ describe('parseMessage', () => {
|
||||
expect(result).toEqual([{ type: 'text', text: 'hello' }]);
|
||||
});
|
||||
|
||||
it('rejects a node mixed into a normal message', async () => {
|
||||
await expect(parseMessage(
|
||||
[
|
||||
{ type: 'text', data: { text: 'hi' } },
|
||||
{ type: 'node', data: { user_id: 1, content: [{ type: 'text', data: { text: 'x' } }] } },
|
||||
] as any,
|
||||
false,
|
||||
)).rejects.toMatchObject({
|
||||
code: 'UNSENDABLE_TYPE',
|
||||
elementType: 'node',
|
||||
message: 'message segment "node" is only valid inside a forward node list',
|
||||
});
|
||||
});
|
||||
|
||||
it('ignores explicit empty text placeholders between sendable segments (issue #273)', async () => {
|
||||
const result = await parseMessage(
|
||||
[
|
||||
@@ -869,6 +887,34 @@ describe('parseMessage', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('exclusiveForwardNodeList', () => {
|
||||
const node = {
|
||||
type: 'node',
|
||||
data: { user_id: 1, content: [{ type: 'text', data: { text: 'x' } }] },
|
||||
};
|
||||
|
||||
it('accepts a node array and a single node object', () => {
|
||||
expect(exclusiveForwardNodeList([node], false)).toEqual([node]);
|
||||
expect(exclusiveForwardNodeList(node, false)).toEqual([node]);
|
||||
});
|
||||
|
||||
it('strips explicit empty text placeholders around nodes', () => {
|
||||
expect(exclusiveForwardNodeList([
|
||||
{ type: 'text', data: { text: '' } },
|
||||
node,
|
||||
{ type: 'text', data: { text: '' } },
|
||||
], false)).toEqual([node]);
|
||||
});
|
||||
|
||||
it('rejects mixed node+text and auto-escaped strings', () => {
|
||||
expect(exclusiveForwardNodeList([
|
||||
node,
|
||||
{ type: 'text', data: { text: 'hi' } },
|
||||
], false)).toBeNull();
|
||||
expect(exclusiveForwardNodeList('hello', true)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('single segment object', () => {
|
||||
it('parses single segment object', async () => {
|
||||
const result = await parseMessage(
|
||||
|
||||
Reference in new issue
Block a user