feat(onebot): unify market faces to image segments + add send support

Market faces (商城表情) now decode to an OneBot `image` segment carrying
NapCat-style markers (emoji_id/emoji_package_id/key/summary) plus a gxh
gif URL, so clients that don't special-case `mface` still render the
sticker while aware clients can reproduce it.

Adds the previously-missing send path: a dedicated `mface` segment OR an
`image` segment carrying `emoji_id` rebuilds the wire `marketFace`
(faceId=hex(emoji_id), itemType 6 / subType 3 / 300×300 /
pbReserve.field8=1) — byte-matched against NapCat and Lagrange.Core.

- proto-defs: MarketFace.key bytes→string; +MarketFacePbReserve{field8@8}
- protocol: decode faceId→emojiId(hex)/tabId/key; makeMarketFaceElem +
  buildSendElems 'mface' case; format preview keeps the sticker name
- onebot: to-segment mface→image; message-parser mface + image(emoji_id)
- tests: wire decode + full proton round-trip; parse→build; segment shape

Reply/forward round-trips preserved (element keeps type 'mface' + text).
This commit is contained in:
DrSmoothl committed 2026-06-14 20:47:50 +08:00
1 parent 58c02d415f
commit 8dc4ff48f8
10 files changed
+247 -16

No files matched your search

@@ -10,7 +10,10 @@
import { describe, expect, it } from 'vitest';
import { deflateSync } from 'zlib';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import { decodeRichBody } from '../../src/msg-push/rich-body-decoder';
import { buildSendElems } from '../../src/element-builder';
import type { MessageElement } from '../../src/events';
import type { MessageBody } from '@snowluma/proto-defs/message';
function lightAppBytes(json: unknown): Uint8Array {
@@ -121,3 +124,42 @@ describe('decodeRichBody / forward LightApp', () => {
expect(out[0]).toMatchObject({ type: 'forward', resId: 'legacy-res' });
});
});
// Market face (商城表情): decode the wire `marketFace` element into the
// `emoji_id`/`emoji_package_id`/`key` markers, and round-trip an mface element
// back through the real proton codegen (faceId hex bytes + pbReserve) so a
// sticker SnowLuma re-sends decodes identically on the receiver.
describe('decodeRichBody / market face', () => {
const EMOJI_ID = '235a82d9c0acd2e2db6e0b94e1a1c4f3';
it('decodes a wire marketFace into an mface element (emojiId = lowercase hex of faceId)', () => {
const body: MessageBody = {
richText: {
elems: [
{
marketFace: {
faceName: '可爱',
faceId: Buffer.from(EMOJI_ID, 'hex'),
tabId: 12,
key: 'abc',
},
} as any,
],
},
};
const out = decodeRichBody(body, true);
expect(out).toEqual([{
type: 'mface', text: '可爱', emojiId: EMOJI_ID, emojiPackageId: 12, emojiKey: 'abc',
}]);
});
it('round-trips an mface element → wire → element through real proton codegen', async () => {
const el: MessageElement = {
type: 'mface', text: '可爱', emojiId: EMOJI_ID, emojiPackageId: 12, emojiKey: 'abc',
};
const elems = await buildSendElems([el]);
const wire = protobuf_encode<MessageBody>({ richText: { elems: elems as any } });
const decoded = protobuf_decode<MessageBody>(wire);
expect(decodeRichBody(decoded, true)).toEqual([el]);
});
});