fix: 对齐 OneBot schema 与合并转发消息处理行为 (#1694)
* fix: make defaulted request fields optional * fix: align onebot schemas with runtime behavior * fix: tighten schema and unsupported action metadata * fix: reject empty forward reference ids * fix: update GoCQHTTPSendForwardMsg to handle messages correctly and adjust payload schema * fix: refactor message handling in GoCQHTTPSendForwardMsg and add test for message normalization * fix: update compatibility actions to return appropriate responses and align schemas with runtime behavior * fix: ensure payload properties are non-null with default values in action handlers * fix: update StreamStatus comments for clarity and enforce nickname as required in message schema * fix: integrate sleep function in GetOnlineClient and update tests to verify API call * fix: add production test checklist for OneBot schema alignment and message handling * fix: remove outdated production test checklist for OneBot schema alignment
This commit is contained in:
54 files changed
+1078
-195
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import {
|
||||
OB11MessageData,
|
||||
OB11MessageDataType,
|
||||
} from '@/napcat-onebot/types';
|
||||
import type {
|
||||
OB11MessageData,
|
||||
OB11MessageMixType,
|
||||
OB11MessageNode,
|
||||
OB11PostContext,
|
||||
@@ -8,16 +10,17 @@ import {
|
||||
import { ActionName, BaseCheckResult } from '@/napcat-onebot/action/router';
|
||||
import { decodeCQCode } from '@/napcat-onebot/helper/cqcode';
|
||||
import { MessageUnique } from 'napcat-common/src/message-unique';
|
||||
import { ChatType, ElementType, NapCatCore, Peer, RawMessage, SendArkElement, SendMessageElement } from 'napcat-core';
|
||||
import { ChatType, ElementType } from 'napcat-core';
|
||||
import type { NapCatCore, Peer, RawMessage, SendArkElement, SendMessageElement } from 'napcat-core';
|
||||
import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
|
||||
import { ForwardMsgBuilder } from '@/napcat-core/helper/forward-msg-builder';
|
||||
import { stringifyWithBigInt } from 'napcat-common/src/helper';
|
||||
import { PacketMsg } from 'napcat-core/packet/message/message';
|
||||
import { rawMsgWithSendMsg } from 'napcat-core/packet/message/converter';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
import type { PacketMsg } from 'napcat-core/packet/message/message';
|
||||
import type { rawMsgWithSendMsg } from 'napcat-core/packet/message/converter';
|
||||
import { Static, TSchema, Type } from '@sinclair/typebox';
|
||||
import { MsgActionsExamples } from '@/napcat-onebot/action/msg/examples';
|
||||
import { OB11MessageMixTypeSchema } from '@/napcat-onebot/types/message';
|
||||
import { UploadForwardMsgParams } from '@/napcat-core/packet/transformer/message/UploadForwardMsgV2';
|
||||
import type { UploadForwardMsgParams } from '@/napcat-core/packet/transformer/message/UploadForwardMsgV2';
|
||||
|
||||
export const SendMsgPayloadSchema = Type.Object({
|
||||
message_type: Type.Optional(Type.Union([Type.Literal('private'), Type.Literal('group')], { description: '消息类型 (private/group)' })),
|
||||
@@ -48,15 +51,43 @@ export enum ContextMode {
|
||||
Group = 2,
|
||||
}
|
||||
|
||||
function normalizeMessageSegments (messages: OB11MessageData[]): OB11MessageData[] {
|
||||
return messages.map((message) => {
|
||||
if (message.type !== OB11MessageDataType.node) {
|
||||
return message;
|
||||
}
|
||||
|
||||
if ('id' in message.data) {
|
||||
return {
|
||||
...message,
|
||||
data: {
|
||||
...message.data,
|
||||
id: message.data.id.toString(),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...message,
|
||||
data: {
|
||||
...message.data,
|
||||
content: normalize((message.data.content ?? []) as OB11MessageMixType),
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// Normalizes a mixed type (CQCode/a single segment/segment array) into a segment array.
|
||||
export function normalize (message: OB11MessageMixType, autoEscape = false): OB11MessageData[] {
|
||||
return typeof message === 'string'
|
||||
const normalized: OB11MessageData[] = typeof message === 'string'
|
||||
? (
|
||||
autoEscape
|
||||
? [{ type: OB11MessageDataType.text, data: { text: message } }]
|
||||
? [{ type: OB11MessageDataType.text, data: { text: message } } as OB11MessageData]
|
||||
: decodeCQCode(message)
|
||||
)
|
||||
: Array.isArray(message) ? message : [message];
|
||||
|
||||
return normalizeMessageSegments(normalized);
|
||||
}
|
||||
|
||||
export async function createContext (core: NapCatCore, payload: OB11PostContext | undefined, contextMode: ContextMode = ContextMode.Normal): Promise<Peer> {
|
||||
@@ -127,12 +158,26 @@ function isNode (msg: OB11MessageData): msg is OB11MessageNode {
|
||||
return msg.type === OB11MessageDataType.node;
|
||||
}
|
||||
|
||||
function hasNodeReferenceId (msg: OB11MessageNode): msg is OB11MessageNode & { data: { id: string; }; } {
|
||||
return 'id' in msg.data && typeof msg.data.id === 'string' && msg.data.id.length > 0;
|
||||
}
|
||||
|
||||
export class SendMsgBase extends OneBotAction<SendMsgPayload, ReturnDataType> {
|
||||
override payloadSchema = SendMsgPayloadSchema;
|
||||
override returnSchema = SendMsgReturnSchema;
|
||||
override payloadSchema: TSchema = SendMsgPayloadSchema;
|
||||
override returnSchema: TSchema = SendMsgReturnSchema;
|
||||
override actionTags = ['消息接口'];
|
||||
|
||||
protected getMessageForCheck (payload: SendMsgPayload): OB11MessageMixType {
|
||||
return payload.message;
|
||||
}
|
||||
|
||||
protected override async check (payload: SendMsgPayload): Promise<BaseCheckResult> {
|
||||
const base = await super.check(payload);
|
||||
if (!base.valid) {
|
||||
return base;
|
||||
}
|
||||
|
||||
payload.message = this.getMessageForCheck(payload);
|
||||
const messages = normalize(payload.message);
|
||||
const nodeElementLength = getSpecialMsgNum(messages, OB11MessageDataType.node);
|
||||
if (nodeElementLength > 0 && nodeElementLength !== messages.length) {
|
||||
@@ -225,8 +270,8 @@ export class SendMsgBase extends OneBotAction<SendMsgPayload, ReturnDataType> {
|
||||
this.core.context.logger.logWarn('转发消息深度超过3层,将停止解析!');
|
||||
break;
|
||||
}
|
||||
if (!node.data.id) {
|
||||
const OB11Data = normalize(node.type === OB11MessageDataType.node ? node.data.content : node);
|
||||
if (!hasNodeReferenceId(node)) {
|
||||
const OB11Data = normalize(node.data.content);
|
||||
let sendElements: SendMessageElement[];
|
||||
|
||||
const subNodeMessages = OB11Data.filter(isNode);
|
||||
@@ -260,7 +305,7 @@ export class SendMsgBase extends OneBotAction<SendMsgPayload, ReturnDataType> {
|
||||
const transformedMsg = this.core.apis.PacketApi.pkt.msgConverter.rawMsgWithSendMsgToPacketMsg(packetMsgElements);
|
||||
this.core.context.logger.logDebug(`handleForwardedNodesPacket[SendRaw] 转换为 ${stringifyWithBigInt(transformedMsg)}`);
|
||||
packetMsg.push(transformedMsg);
|
||||
} else if (node.data.id) {
|
||||
} else {
|
||||
const id = node.data.id;
|
||||
const nodeMsg = MessageUnique.getMsgIdAndPeerByShortId(+id) || MessageUnique.getPeerByMsgId(id);
|
||||
if (!nodeMsg) {
|
||||
@@ -307,8 +352,6 @@ export class SendMsgBase extends OneBotAction<SendMsgPayload, ReturnDataType> {
|
||||
this.core.context.logger.logDebug(`handleForwardedNodesPacket[PureRaw] 转换为 ${stringifyWithBigInt(transformedMsg)}`);
|
||||
packetMsg.push(transformedMsg);
|
||||
}
|
||||
} else {
|
||||
this.core.context.logger.logDebug(`handleForwardedNodesPacket 跳过元素 ${stringifyWithBigInt(node)}`);
|
||||
}
|
||||
}
|
||||
if (packetMsg.length === 0) {
|
||||
@@ -369,8 +412,8 @@ export class SendMsgBase extends OneBotAction<SendMsgPayload, ReturnDataType> {
|
||||
};
|
||||
let nodeMsgIds: string[] = [];
|
||||
for (const messageNode of messageNodes) {
|
||||
const nodeId = messageNode.data.id;
|
||||
if (nodeId) {
|
||||
if (hasNodeReferenceId(messageNode)) {
|
||||
const nodeId = messageNode.data.id;
|
||||
// 对Msgid和OB11ID混用情况兜底
|
||||
const nodeMsg = MessageUnique.getMsgIdAndPeerByShortId(parseInt(nodeId)) || MessageUnique.getPeerByMsgId(nodeId);
|
||||
if (!nodeMsg) {
|
||||
|
||||
Reference in new issue
Block a user