From bbd0d2aca3d470c20b33b1d4f95123335633bf26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Mon, 12 Oct 2026 12:26:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(message):=20=E4=BF=AE=E5=A4=8D=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E8=BD=AC=E5=8F=91=E4=B8=AD=E5=8A=A8=E7=94=BB=E8=A1=A8?= =?UTF-8?q?=E6=83=85=E5=90=9E=E6=8E=89=E5=90=8E=E7=BB=AD=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E6=AE=B5=20(#2097)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../napcat-core/packet/message/element.ts | 8 ++ packages/napcat-test/forwardFace.test.ts | 130 ++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 packages/napcat-test/forwardFace.test.ts diff --git a/packages/napcat-core/packet/message/element.ts b/packages/napcat-core/packet/message/element.ts index 96c8ee4fe..b89a50a0b 100644 --- a/packages/napcat-core/packet/message/element.ts +++ b/packages/napcat-core/packet/message/element.ts @@ -211,12 +211,14 @@ export class PacketMsgReplyElement extends IPacketMsgElement { export class PacketMsgFaceElement extends IPacketMsgElement { faceId: number; + faceText: string; isLargeFace: boolean; resultId?: string; constructor (element: SendFaceElement) { super(element); this.faceId = element.faceElement.faceIndex; + this.faceText = element.faceElement.faceText || '[表情]'; this.resultId = element.faceElement.resultId; this.isLargeFace = element.faceElement.faceType === FaceType.AniSticke; } @@ -237,6 +239,12 @@ export class PacketMsgFaceElement extends IPacketMsgElement { }), businessType: 1, }, + }, { + // QQ consumes the element after an animated face as compatibility text. + // Without it, the next user-supplied message segment is discarded. + text: { + str: this.faceText, + }, }]; } else if (this.faceId < 260) { return [{ diff --git a/packages/napcat-test/forwardFace.test.ts b/packages/napcat-test/forwardFace.test.ts new file mode 100644 index 000000000..45cad010a --- /dev/null +++ b/packages/napcat-test/forwardFace.test.ts @@ -0,0 +1,130 @@ +import { gunzipSync, inflateSync } from 'node:zlib'; +import { describe, expect, it, vi } from 'vitest'; +import { NapProtoMsg } from 'napcat-protobuf'; +import { ChatType, ElementType, FaceType } from '@/napcat-core/types/msg'; +import { PacketMsgConverter } from '@/napcat-core/packet/message/converter'; +import { PacketMsgFaceElement } from '@/napcat-core/packet/message/element'; +import { LongMsgResult, QBigFaceExtra, SendLongMsgReq } from '@/napcat-core/packet/transformer/proto'; +import UploadForwardMsgV2 from '@/napcat-core/packet/transformer/message/UploadForwardMsgV2'; +import { OneBotMsgApi } from '@/napcat-onebot/api/msg'; +import { OB11MessageData, OB11MessageDataType } from '@/napcat-onebot/types/message'; + +// Keep the real message types without starting the adapter's WebUI/network modules. +vi.mock('@/napcat-onebot/index', async () => await import('@/napcat-onebot/types')); +vi.mock('napcat-core/packet/transformer/proto', async () => await import('@/napcat-core/packet/transformer/proto')); + +const text = (value: string): OB11MessageData => ({ + type: OB11MessageDataType.text, + data: { text: value }, +}); +const face = (id: string): OB11MessageData => ({ + type: OB11MessageDataType.face, + data: { id }, +}); + +async function buildForwardElements (content: OB11MessageData[], groupUin = 0) { + const api = new OneBotMsgApi( + {} as ConstructorParameters[0], + {} as ConstructorParameters[1] + ); + const { sendElements } = await api.createSendElements(content, { + chatType: groupUin ? ChatType.KCHATTYPEGROUP : ChatType.KCHATTYPEC2C, + peerUid: groupUin ? String(groupUin) : 'u_friend', + guildId: '', + }); + const msg = new PacketMsgConverter().rawMsgWithSendMsgToPacketMsg({ + senderUin: 123456, + senderName: '测试', + groupId: groupUin, + time: 1700000000, + msg: sendElements, + }); + const packet = UploadForwardMsgV2.build('u_self', [{ actionCommand: 'MultiMsg', actionMsg: [msg] }], groupUin); + const request = new NapProtoMsg(SendLongMsgReq).decode(packet.data); + const payload = new NapProtoMsg(LongMsgResult).decode(gunzipSync(request.info.payload!)); + return payload.action[0]!.actionData.msgBody[0]!.body!.richText!.elems; +} + +describe.each([ + { name: 'private', groupUin: 0 }, + { name: 'group', groupUin: 654321 }, +])('$name forward face encoding', ({ groupUin }) => { + it('keeps the text immediately after the animated face in issue #2097', async () => { + const elems = await buildForwardElements([text('1234'), face('317'), text('5678'), text('在')], groupUin); + + expect(elems).toMatchObject([ + { text: { str: '1234' } }, + { commonElem: { serviceType: 37 } }, + { text: { str: '/菜汪' } }, + { text: { str: '5678' } }, + { text: { str: '在' } }, + ]); + expect(new NapProtoMsg(QBigFaceExtra).decode(elems[1]!.commonElem!.pbElem).faceId).toBe(317); + }); + + it('gives each consecutive animated face its own compatibility text', async () => { + const elems = await buildForwardElements([face('317'), face('318'), text('after')], groupUin); + + expect(elems).toMatchObject([ + { commonElem: { serviceType: 37 } }, + { text: { str: '/菜汪' } }, + { commonElem: { serviceType: 37 } }, + { text: { str: '/崇拜' } }, + { text: { str: 'after' } }, + ]); + }); + + it('includes compatibility text when the animated face ends the node', async () => { + const elems = await buildForwardElements([face('317')], groupUin); + + expect(elems).toMatchObject([ + { commonElem: { serviceType: 37 } }, + { text: { str: '/菜汪' } }, + ]); + }); + + it('keeps a non-text segment immediately after an animated face', async () => { + const json = JSON.stringify({ app: 'test', prompt: 'card' }); + const elems = await buildForwardElements([ + face('317'), + { type: OB11MessageDataType.json, data: { data: json } }, + text('after'), + ], groupUin); + + expect(elems).toMatchObject([ + { commonElem: { serviceType: 37 } }, + { text: { str: '/菜汪' } }, + { lightAppElem: {} }, + { text: { str: 'after' } }, + ]); + expect(inflateSync(elems[2]!.lightAppElem!.data.subarray(1)).toString()).toBe(json); + }); + + it('does not insert compatibility text after legacy or small faces', async () => { + const elems = await buildForwardElements([face('14'), text('legacy'), face('264'), text('small')], groupUin); + + expect(elems).toMatchObject([ + { face: { index: 14 } }, + { text: { str: 'legacy' } }, + { commonElem: { serviceType: 33 } }, + { text: { str: 'small' } }, + ]); + }); +}); + +it.each([undefined, ''])('provides non-empty compatibility text when faceText is %s', (faceText) => { + const face = new PacketMsgFaceElement({ + elementType: ElementType.FACE, + elementId: '', + faceElement: { + faceIndex: 317, + faceType: FaceType.AniSticke, + faceText, + }, + }); + + expect(face.buildElement()).toMatchObject([ + { commonElem: { serviceType: 37 } }, + { text: { str: '[表情]' } }, + ]); +});